From c160002a940431831c44e05dd26c038f898dc336 Mon Sep 17 00:00:00 2001 From: Feliciaan De Palmenaer Date: Sat, 28 Mar 2015 00:44:58 +0100 Subject: [PATCH 1/5] Delete items, with fancy icon --- app/models.py | 10 +++++ app/templates/home_loggedin.html | 2 +- app/templates/layout.html | 2 +- app/templates/order.html | 11 ++++- app/templates/orders.html | 4 +- app/views/order.py | 69 +++++++++++++++++++------------- 6 files changed, 65 insertions(+), 33 deletions(-) diff --git a/app/models.py b/app/models.py index 86ca9e5..418d4d2 100644 --- a/app/models.py +++ b/app/models.py @@ -1,3 +1,4 @@ +from datetime import datetime from app import db @@ -103,3 +104,12 @@ class OrderItem(db.Model): def __repr__(self): return 'OrderItem' + + def can_delete(self, order_id, user_id): + if self.user_id != user_id: + return False + if int(self.order_id) != int(order_id): + return False + if self.order.stoptime and self.order.stoptime < datetime.now(): + return False + return True diff --git a/app/templates/home_loggedin.html b/app/templates/home_loggedin.html index 8207d05..51b3f32 100644 --- a/app/templates/home_loggedin.html +++ b/app/templates/home_loggedin.html @@ -6,7 +6,7 @@

Open orders:

diff --git a/app/templates/layout.html b/app/templates/layout.html index fd0ca1d..8f4c712 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -2,7 +2,7 @@ {% set navbar = [ ('home', 'Home'), - ('orders', 'Orders'), + ('order_bp.orders', 'Orders'), ('about', 'About'), ('stats', 'Stats'), ] -%} diff --git a/app/templates/order.html b/app/templates/order.html index 7485461..39e13b0 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -1,18 +1,25 @@ {% extends "layout.html" %} {% set active_page = "orders" -%} +{% import "bootstrap/wtf.html" as wtf %} {% block container %}
-
+

Order {{ order.id }}

Courrier: {{ order.courrier.username }}
Location: {{ order.location.name }}
Starttime: {{ order.starttime }}
Stoptime: {{ order.stoptime }}
+ Total price: {{ total_price|euro }}

Orders

{% for item in order.orders %} - {{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }}
+ {{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }} + {% if item.can_delete(order.id, current_user.id) -%}{%- endif %}
{% endfor %}
+
+

Order:

+ {{ wtf.quick_form(form, action=url_for('.order_create'), button_map={'submit_button': 'primary'}, form_type='horizontal') }} +
{% endblock %} \ No newline at end of file diff --git a/app/templates/orders.html b/app/templates/orders.html index 4673a78..abae873 100644 --- a/app/templates/orders.html +++ b/app/templates/orders.html @@ -8,12 +8,12 @@

Open orders:

- {{ wtf.quick_form(form, action=url_for('order_create'), button_map={'submit_button': 'primary'}, form_type='horizontal') }} + {{ wtf.quick_form(form, action=url_for('.order_create'), button_map={'submit_button': 'primary'}, form_type='horizontal') }}
diff --git a/app/views/order.py b/app/views/order.py index e3f094c..a12ba3d 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -1,5 +1,5 @@ __author__ = 'feliciaan' -from flask import url_for, render_template, abort, redirect +from flask import url_for, render_template, abort, redirect, Blueprint from flask.ext.login import current_user, login_required from datetime import datetime @@ -7,17 +7,45 @@ from app import app, db from models import Order, OrderItem from forms import OrderItemForm, OrderForm +order_bp = Blueprint('order_bp', 'order') -@app.route('/order/') +@order_bp.route('/') +@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) + + +@order_bp.route('/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")) + + +@order_bp.route('/') @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) + form = OrderItemForm() + form.populate(order.location) + total_price = sum([o.food.price for o in order.orders]) + return render_template('order.html', order=order, form=form, total_price=total_price) return abort(404) -@app.route('/order//create', methods=['GET', 'POST']) +@order_bp.route('//create', methods=['GET', 'POST']) @login_required def order_item_create(id): order = Order.query.filter(Order.id == id).first() @@ -31,31 +59,18 @@ def order_item_create(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 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']) +@order_bp.route('///delete') @login_required -def order_create(): - orderForm = OrderForm() - orderForm.populate() - if orderForm.validate_on_submit(): - order = Order() - orderForm.populate_obj(order) - db.session.add(order) +def delete_item(order_id, item_id): + item = OrderItem.query.filter(OrderItem.id == item_id).first() + if item.can_delete(order_id, current_user.id): + db.session.delete(item) 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) + return redirect(url_for('.order', id=order_id)) + abort(404) +app.register_blueprint(order_bp, url_prefix='/order') \ No newline at end of file From 7514396669051197adeaa1b3261f21478d29e26b Mon Sep 17 00:00:00 2001 From: Feliciaan De Palmenaer Date: Sat, 28 Mar 2015 01:09:59 +0100 Subject: [PATCH 2/5] "I volunteer as Tribute" --- app/forms.py | 8 ++++++-- app/templates/layout.html | 2 ++ app/templates/order.html | 8 ++++++-- app/views/order.py | 18 +++++++++++++++++- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/forms.py b/app/forms.py index 8aa1a20..873c9cb 100644 --- a/app/forms.py +++ b/app/forms.py @@ -1,4 +1,5 @@ from datetime import datetime, timedelta +from flask.ext.login import current_user from flask_wtf import Form from wtforms import SelectField, DateTimeField, validators, SubmitField, HiddenField from models import User, Location @@ -15,8 +16,11 @@ class OrderForm(Form): submit_button = SubmitField('Submit') def populate(self): - self.courrier_id.choices = [(0, None)] + \ - [(u.id, u.username) for u in User.query.order_by('username')] + if current_user.is_admin(): + self.courrier_id.choices = [(0, None)] + \ + [(u.id, u.username) for u in User.query.order_by('username')] + else: + self.courrier_id.choices = [(0, None), (current_user.id, current_user.username)] self.location_id.choices = [(l.id, l.name) for l in Location.query.order_by('name')] if self.stoptime.data is None: diff --git a/app/templates/layout.html b/app/templates/layout.html index 8f4c712..0971737 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -1,4 +1,5 @@ {% extends "bootstrap/base.html" %} +{% import "bootstrap/utils.html" as utils %} {% set navbar = [ ('home', 'Home'), @@ -52,6 +53,7 @@ {% endblock %} {% block content -%} + {{ utils.flashed_messages() }}
{% block container -%} {%- endblock %} diff --git a/app/templates/order.html b/app/templates/order.html index 39e13b0..ab64a8b 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -6,7 +6,11 @@

Order {{ order.id }}

- Courrier: {{ order.courrier.username }}
+ Courrier: {{ order.courrier.username }} + {% if order.courrier == None %} + Volunteer + {% endif %} +
Location: {{ order.location.name }}
Starttime: {{ order.starttime }}
Stoptime: {{ order.stoptime }}
@@ -19,7 +23,7 @@

Order:

- {{ wtf.quick_form(form, action=url_for('.order_create'), button_map={'submit_button': 'primary'}, form_type='horizontal') }} + {{ wtf.quick_form(form, action=url_for('.order_item_create', id=order.id), button_map={'submit_button': 'primary'}, form_type='horizontal') }}
{% endblock %} \ No newline at end of file diff --git a/app/views/order.py b/app/views/order.py index a12ba3d..92b59e9 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -1,5 +1,5 @@ __author__ = 'feliciaan' -from flask import url_for, render_template, abort, redirect, Blueprint +from flask import url_for, render_template, abort, redirect, Blueprint, flash from flask.ext.login import current_user, login_required from datetime import datetime @@ -73,4 +73,20 @@ def delete_item(order_id, item_id): return redirect(url_for('.order', id=order_id)) abort(404) + +@order_bp.route('//volunteer') +@login_required +def volunteer(id): + order = Order.query.filter(Order.id == id).first() + if order is not None: + print(order.courrier_id) + if order.courrier_id == 0: + order.courrier_id = current_user.id + db.session.commit() + flash("Thank you for volunteering!") + else: + flash("Volunteering not possible!") + return redirect(url_for('.order', id=id)) + abort(404) + app.register_blueprint(order_bp, url_prefix='/order') \ No newline at end of file From a0b79b68aca51054f1d7b036d5fa3e556976f608 Mon Sep 17 00:00:00 2001 From: Feliciaan De Palmenaer Date: Sat, 28 Mar 2015 01:31:04 +0100 Subject: [PATCH 3/5] Added totals --- app/models.py | 13 +++++++++++++ app/templates/order.html | 4 ++++ app/views/order.py | 3 ++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/models.py b/app/models.py index 418d4d2..f338157 100644 --- a/app/models.py +++ b/app/models.py @@ -1,4 +1,6 @@ from datetime import datetime +from collections import defaultdict + from app import db @@ -90,6 +92,17 @@ class Order(db.Model): def __repr__(self): return 'Order %s' % (self.location.name) + def group_by_user(self): + group = defaultdict(list) + for item in self.orders: + group[item.user_id] += [item.food] + return group + + def group_by_user_pay(self): + group = defaultdict(int) + for item in self.orders: + group[item.user] += item.food.price + return group class OrderItem(db.Model): id = db.Column(db.Integer, primary_key=True) diff --git a/app/templates/order.html b/app/templates/order.html index ab64a8b..3346ab4 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -20,6 +20,10 @@ {{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }} {% if item.can_delete(order.id, current_user.id) -%}{%- endif %}
{% endfor %} +

Debts

+ {% for key, value in total_payments.items() %} + {{ key.username }} - {{ value|euro }}
+ {% endfor %}

Order:

diff --git a/app/views/order.py b/app/views/order.py index 92b59e9..f912bda 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -41,7 +41,8 @@ def order(id): form = OrderItemForm() form.populate(order.location) total_price = sum([o.food.price for o in order.orders]) - return render_template('order.html', order=order, form=form, total_price=total_price) + total_payments = order.group_by_user_pay() + return render_template('order.html', order=order, form=form, total_price=total_price, total_payments=total_payments) return abort(404) From 09e3aece7322635513dc773d3bac7c2530cc67ad Mon Sep 17 00:00:00 2001 From: Feliciaan De Palmenaer Date: Sat, 28 Mar 2015 02:01:30 +0100 Subject: [PATCH 4/5] Randomly select someone --- app/templates/order.html | 3 +++ app/views/order.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/templates/order.html b/app/templates/order.html index 3346ab4..8844a23 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -6,6 +6,9 @@

Order {{ order.id }}

+ {% if (current_user.id == order.courrier_id or current_user.is_admin()) and not order.stoptime -%} + Close
+ {%- endif %} Courrier: {{ order.courrier.username }} {% if order.courrier == None %} Volunteer diff --git a/app/views/order.py b/app/views/order.py index f912bda..15bdd51 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -1,6 +1,7 @@ __author__ = 'feliciaan' from flask import url_for, render_template, abort, redirect, Blueprint, flash from flask.ext.login import current_user, login_required +import random from datetime import datetime from app import app, db @@ -90,4 +91,36 @@ def volunteer(id): return redirect(url_for('.order', id=id)) abort(404) -app.register_blueprint(order_bp, url_prefix='/order') \ No newline at end of file + +@order_bp.route('//close') +@login_required +def close_order(id): + order = Order.query.filter(Order.id == id).first() + if order is not None: + if (current_user.id == order.courrier_id or current_user.is_admin()) \ + and order.stoptime is None: + order.stoptime = datetime.now() + print(order.courrier_id) + if order.courrier_id == 0 or order.courrier_id is None: + order.courrier_id = select_user(order.orders).id + print(order.courrier_id) + db.session.commit() + return redirect(url_for('.order', id=id)) + abort(401) + +app.register_blueprint(order_bp, url_prefix='/order') + + +def select_user(items): + user = None + items = list(items) + if len(items) <= 0: + return None + + while user is None: + item = random.choice(items) + user = item.user + if random.randint(user.bias, 100) < 80: + user = None + + return user From 8b99a763d82fb76e7a02f897b48123c261188253 Mon Sep 17 00:00:00 2001 From: Feliciaan De Palmenaer Date: Sat, 28 Mar 2015 10:49:28 +0100 Subject: [PATCH 5/5] Better close button --- app/templates/order.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/templates/order.html b/app/templates/order.html index 8844a23..8c8b3fa 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -5,10 +5,10 @@ {% block container %}
-

Order {{ order.id }}

- {% if (current_user.id == order.courrier_id or current_user.is_admin()) and not order.stoptime -%} +

Order {{ order.id }} + {% if (current_user.id == order.courrier_id and (not order.stoptime)) or current_user.is_admin() -%} Close
- {%- endif %} + {%- endif %}

Courrier: {{ order.courrier.username }} {% if order.courrier == None %} Volunteer