Add order items and split views
This commit is contained in:
parent
070dc241a9
commit
b34dc7fccc
5 changed files with 80 additions and 40 deletions
|
@ -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
|
||||
|
||||
|
|
13
app/forms.py
13
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]
|
|
@ -4,6 +4,6 @@
|
|||
{% import "bootstrap/wtf.html" as wtf %}
|
||||
{% block container %}
|
||||
<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>
|
||||
{% endblock %}
|
|
@ -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/<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
|
||||
@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)
|
||||
|
61
app/views/order.py
Normal file
61
app/views/order.py
Normal 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)
|
||||
|
Loading…
Reference in a new issue