From b34dc7fccc3e52837b0a203505310ea4cd8bae7a Mon Sep 17 00:00:00 2001 From: Feliciaan De Palmenaer Date: Sat, 28 Mar 2015 00:02:12 +0100 Subject: [PATCH] Add order items and split views --- app/create_database.py | 1 + app/forms.py | 13 ++++-- app/templates/order_form.html | 2 +- app/{views.py => views/__init__.py} | 43 ++++---------------- app/views/order.py | 61 +++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 40 deletions(-) rename app/{views.py => views/__init__.py} (55%) create mode 100644 app/views/order.py diff --git a/app/create_database.py b/app/create_database.py index 6f15e18..d12df23 100644 --- a/app/create_database.py +++ b/app/create_database.py @@ -10,6 +10,7 @@ db.session.add(feli) wout = User() wout.configure('wout', True, 0) +db.session.add(wout) # To future developers, add yourself here diff --git a/app/forms.py b/app/forms.py index 5e21f2f..8aa1a20 100644 --- a/app/forms.py +++ b/app/forms.py @@ -1,17 +1,18 @@ from datetime import datetime, timedelta 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 utils import euro __author__ = 'feliciaan' class OrderForm(Form): 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) stoptime = DateTimeField('Stoptime') - submit_button = SubmitField('Submit Form') + submit_button = SubmitField('Submit') def populate(self): self.courrier_id.choices = [(0, None)] + \ @@ -22,3 +23,9 @@ class OrderForm(Form): 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] \ No newline at end of file diff --git a/app/templates/order_form.html b/app/templates/order_form.html index 178b34d..a5b5068 100644 --- a/app/templates/order_form.html +++ b/app/templates/order_form.html @@ -4,6 +4,6 @@ {% import "bootstrap/wtf.html" as wtf %} {% block container %}
- {{ 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'}) }}
{% endblock %} \ No newline at end of file diff --git a/app/views.py b/app/views/__init__.py similarity index 55% rename from app/views.py rename to app/views/__init__.py index 78a573b..f68989f 100644 --- a/app/views.py +++ b/app/views/__init__.py @@ -1,12 +1,14 @@ +__author__ = 'feliciaan' + from flask import url_for, render_template, abort, redirect, request from flask.ext.login import current_user, login_required from datetime import datetime from app import app, db -from forms import OrderForm -from models import Order - +from models import Order, OrderItem +# import views +import views.order @app.route('/') def home(): @@ -27,37 +29,6 @@ def stats(): return render_template('stats.html') -@app.route('/order/') -@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 @app.route('/routes') @login_required @@ -68,10 +39,10 @@ if app.debug: # add route information options = {} for arg in rule.arguments: options[arg] = "[{0}]".format(arg) - + print(rule.endpoint) methods = ','.join(rule.methods) url = url_for(rule.endpoint, **options) - line = urllib.unquote( + line = urllib.parse.unquote( "{:50s} {:20s} {}".format(rule.endpoint, methods, url)) output.append(line) diff --git a/app/views/order.py b/app/views/order.py new file mode 100644 index 0000000..e3f094c --- /dev/null +++ b/app/views/order.py @@ -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/') +@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_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) +