Add order items and split views

This commit is contained in:
Feliciaan De Palmenaer 2015-03-28 00:02:12 +01:00
parent 070dc241a9
commit b34dc7fccc
5 changed files with 80 additions and 40 deletions

View file

@ -10,6 +10,7 @@ db.session.add(feli)
wout = User() wout = User()
wout.configure('wout', True, 0) wout.configure('wout', True, 0)
db.session.add(wout)
# To future developers, add yourself here # To future developers, add yourself here

View file

@ -1,17 +1,18 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from flask_wtf import Form from flask_wtf import Form
from wtforms import SelectField, DateTimeField, validators, SubmitField from wtforms import SelectField, DateTimeField, validators, SubmitField, HiddenField
from models import User, Location from models import User, Location
from utils import euro
__author__ = 'feliciaan' __author__ = 'feliciaan'
class OrderForm(Form): class OrderForm(Form):
courrier_id = SelectField('Courrier', coerce=int) courrier_id = SelectField('Courrier', coerce=int)
location_id = SelectField('Location', coerce=int, validators=[validators.optional()]) location_id = SelectField('Location', coerce=int, validators=[validators.required()])
starttime = DateTimeField('Starttime', default=datetime.now) starttime = DateTimeField('Starttime', default=datetime.now)
stoptime = DateTimeField('Stoptime') stoptime = DateTimeField('Stoptime')
submit_button = SubmitField('Submit Form') submit_button = SubmitField('Submit')
def populate(self): def populate(self):
self.courrier_id.choices = [(0, None)] + \ self.courrier_id.choices = [(0, None)] + \
@ -22,3 +23,9 @@ class OrderForm(Form):
self.stoptime.data = datetime.now() + timedelta(hours=1) self.stoptime.data = datetime.now() + timedelta(hours=1)
class OrderItemForm(Form):
food_id = SelectField('Item', coerce=int)
submit_button = SubmitField('Submit')
def populate(self, location):
self.food_id.choices = [(i.id, (i.name + ": " + euro(i.price))) for i in location.food]

View file

@ -4,6 +4,6 @@
{% import "bootstrap/wtf.html" as wtf %} {% import "bootstrap/wtf.html" as wtf %}
{% block container %} {% block container %}
<div class="row"> <div class="row">
{{ wtf.quick_form(form, action=url_for('order_create'), button_map={'submit_button': 'primary'}) }} {{ wtf.quick_form(form, action=url, button_map={'submit_button': 'primary'}) }}
</div> </div>
{% endblock %} {% endblock %}

View file

@ -1,12 +1,14 @@
__author__ = 'feliciaan'
from flask import url_for, render_template, abort, redirect, request from flask import url_for, render_template, abort, redirect, request
from flask.ext.login import current_user, login_required from flask.ext.login import current_user, login_required
from datetime import datetime from datetime import datetime
from app import app, db from app import app, db
from forms import OrderForm from models import Order, OrderItem
from models import Order
# import views
import views.order
@app.route('/') @app.route('/')
def home(): def home():
@ -27,37 +29,6 @@ def stats():
return render_template('stats.html') return render_template('stats.html')
@app.route('/order/<int:id>')
@login_required
def order(id):
order = Order.query.filter(Order.id == id).first()
if order is not None:
return render_template('order.html', order=order)
return abort(404)
@app.route('/order/create', methods=['GET', 'POST'])
@login_required
def order_create():
orderForm = OrderForm()
orderForm.populate()
if orderForm.validate_on_submit():
order = Order()
orderForm.populate_obj(order)
db.session.add(order)
db.session.commit()
return redirect(url_for('order', id=order.id))
return render_template('order_form.html', form=orderForm)
@app.route('/order')
@login_required
def orders():
orders = Order.query.filter((Order.stoptime > datetime.now()) | (Order.stoptime == None)).all()
orderForm = OrderForm()
orderForm.populate()
return render_template('orders.html', orders=orders, form=orderForm)
if app.debug: # add route information if app.debug: # add route information
@app.route('/routes') @app.route('/routes')
@login_required @login_required
@ -68,10 +39,10 @@ if app.debug: # add route information
options = {} options = {}
for arg in rule.arguments: for arg in rule.arguments:
options[arg] = "[{0}]".format(arg) options[arg] = "[{0}]".format(arg)
print(rule.endpoint)
methods = ','.join(rule.methods) methods = ','.join(rule.methods)
url = url_for(rule.endpoint, **options) url = url_for(rule.endpoint, **options)
line = urllib.unquote( line = urllib.parse.unquote(
"{:50s} {:20s} {}".format(rule.endpoint, methods, url)) "{:50s} {:20s} {}".format(rule.endpoint, methods, url))
output.append(line) output.append(line)

61
app/views/order.py Normal file
View file

@ -0,0 +1,61 @@
__author__ = 'feliciaan'
from flask import url_for, render_template, abort, redirect
from flask.ext.login import current_user, login_required
from datetime import datetime
from app import app, db
from models import Order, OrderItem
from forms import OrderItemForm, OrderForm
@app.route('/order/<id>')
@login_required
def order(id):
order = Order.query.filter(Order.id == id).first()
if order is not None:
return render_template('order.html', order=order)
return abort(404)
@app.route('/order/<id>/create', methods=['GET', 'POST'])
@login_required
def order_item_create(id):
order = Order.query.filter(Order.id == id).first()
if order is not None:
form = OrderItemForm()
form.populate(order.location)
if form.validate_on_submit():
item = OrderItem()
form.populate_obj(item)
item.order_id = id
item.user_id = current_user.id
db.session.add(item)
db.session.commit()
return redirect(url_for('order', id=id))
return render_template('order_form.html', form=form, url=url_for("order_item_create", id=id))
return abort(404)
@app.route('/order/create', methods=['GET', 'POST'])
@login_required
def order_create():
orderForm = OrderForm()
orderForm.populate()
if orderForm.validate_on_submit():
order = Order()
orderForm.populate_obj(order)
db.session.add(order)
db.session.commit()
return redirect(url_for('order', id=order.id))
return render_template('order_form.html', form=orderForm, url=url_for("order_create"))
@app.route('/order')
@login_required
def orders():
orders = Order.query.filter((Order.stoptime > datetime.now()) | (Order.stoptime == None)).all()
orderForm = OrderForm()
orderForm.populate()
return render_template('orders.html', orders=orders, form=orderForm)