Merge remote-tracking branch 'origin/master'

This commit is contained in:
Wout Schellaert 2015-03-28 11:32:50 +01:00
commit 4522adb3cc
7 changed files with 146 additions and 34 deletions

View file

@ -1,4 +1,5 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from flask.ext.login import current_user
from flask_wtf import Form from flask_wtf import Form
from wtforms import SelectField, DateTimeField, validators, SubmitField, HiddenField from wtforms import SelectField, DateTimeField, validators, SubmitField, HiddenField
from models import User, Location from models import User, Location
@ -15,8 +16,11 @@ class OrderForm(Form):
submit_button = SubmitField('Submit') submit_button = SubmitField('Submit')
def populate(self): def populate(self):
self.courrier_id.choices = [(0, None)] + \ if current_user.is_admin():
[(u.id, u.username) for u in User.query.order_by('username')] 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) self.location_id.choices = [(l.id, l.name)
for l in Location.query.order_by('name')] for l in Location.query.order_by('name')]
if self.stoptime.data is None: if self.stoptime.data is None:

View file

@ -1,3 +1,6 @@
from datetime import datetime
from collections import defaultdict
from app import db from app import db
@ -89,6 +92,17 @@ class Order(db.Model):
def __repr__(self): def __repr__(self):
return 'Order %s' % (self.location.name) 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): class OrderItem(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
@ -103,3 +117,12 @@ class OrderItem(db.Model):
def __repr__(self): def __repr__(self):
return 'OrderItem' 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

View file

@ -6,7 +6,7 @@
<h3>Open orders:</h3> <h3>Open orders:</h3>
<ul> <ul>
{% for order in orders %} {% for order in orders %}
<li><a href="{{ url_for('order', id=order.id) }}">{{ order.location.name }}-{{ order.stoptime }}</a></li> <li><a href="{{ url_for('order_bp.order', id=order.id) }}">{{ order.location.name }}-{{ order.stoptime }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

View file

@ -1,8 +1,9 @@
{% extends "bootstrap/base.html" %} {% extends "bootstrap/base.html" %}
{% import "bootstrap/utils.html" as utils %}
{% set navbar = [ {% set navbar = [
('home', 'Home'), ('home', 'Home'),
('orders', 'Orders'), ('order_bp.orders', 'Orders'),
('about', 'About'), ('about', 'About'),
('stats', 'Stats'), ('stats', 'Stats'),
] -%} ] -%}
@ -52,6 +53,7 @@
{% endblock %} {% endblock %}
{% block content -%} {% block content -%}
{{ utils.flashed_messages() }}
<div class="container"> <div class="container">
{% block container -%} {% block container -%}
{%- endblock %} {%- endblock %}

View file

@ -1,18 +1,36 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% set active_page = "orders" -%} {% set active_page = "orders" -%}
{% import "bootstrap/wtf.html" as wtf %}
{% block container %} {% block container %}
<div class="row"> <div class="row">
<div class="col-md-12"><!-- Shitty html--> <div class="col-md-7"><!-- Shitty html-->
<h3>Order {{ order.id }}</h3> <h3>Order {{ order.id }}
Courrier: {{ order.courrier.username }}<br/> {% if (current_user.id == order.courrier_id and (not order.stoptime)) or current_user.is_admin() -%}
<a class="btn btn-danger" href="{{ url_for('.close_order', id=order.id) }}">Close</a><br/>
{%- endif %}</h3>
Courrier: {{ order.courrier.username }}
{% if order.courrier == None %}
<a href="{{ url_for('.volunteer', id=order.id) }}" class="btn btn-primary">Volunteer</a>
{% endif %}
<br/>
Location: <a href="{{ order.location.website }}">{{ order.location.name }}</a><br/> Location: <a href="{{ order.location.website }}">{{ order.location.name }}</a><br/>
Starttime: {{ order.starttime }}<br/> Starttime: {{ order.starttime }}<br/>
Stoptime: {{ order.stoptime }}<br/> Stoptime: {{ order.stoptime }}<br/>
Total price: {{ total_price|euro }}
<h3>Orders</h3> <h3>Orders</h3>
{% for item in order.orders %} {% for item in order.orders %}
{{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }}<br/> {{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }}
{% if item.can_delete(order.id, current_user.id) -%}<a href="{{ url_for('.delete_item', order_id=order.id, item_id=item.id) }}"><span class="glyphicon glyphicon-remove"></span></a>{%- endif %}<br/>
{% endfor %} {% endfor %}
<h3>Debts</h3>
{% for key, value in total_payments.items() %}
{{ key.username }} - {{ value|euro }}<br/>
{% endfor %}
</div>
<div class="col-md-push-1 col-md-4">
<h4>Order:</h4>
{{ wtf.quick_form(form, action=url_for('.order_item_create', id=order.id), button_map={'submit_button': 'primary'}, form_type='horizontal') }}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View file

@ -8,12 +8,12 @@
<h3>Open orders:</h3> <h3>Open orders:</h3>
<ul> <ul>
{% for order in orders %} {% for order in orders %}
<li><a href="{{ url_for('order', id=order.id) }}">{{ order.location.name }}-{{ order.stoptime }}</a></li> <li><a href="{{ url_for('.order', id=order.id) }}">{{ order.location.name }}-{{ order.stoptime }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>
<div class="col-md-push-1 col-md-6"> <div class="col-md-push-1 col-md-6">
{{ 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') }}
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,23 +1,53 @@
__author__ = 'feliciaan' __author__ = 'feliciaan'
from flask import url_for, render_template, abort, redirect from flask import url_for, render_template, abort, redirect, Blueprint, flash
from flask.ext.login import current_user, login_required from flask.ext.login import current_user, login_required
import random
from datetime import datetime from datetime import datetime
from app import app, db from app import app, db
from models import Order, OrderItem from models import Order, OrderItem
from forms import OrderItemForm, OrderForm from forms import OrderItemForm, OrderForm
order_bp = Blueprint('order_bp', 'order')
@app.route('/order/<id>') @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('/<id>')
@login_required @login_required
def order(id): def order(id):
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
if order is not None: 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])
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) return abort(404)
@app.route('/order/<id>/create', methods=['GET', 'POST']) @order_bp.route('/<id>/create', methods=['GET', 'POST'])
@login_required @login_required
def order_item_create(id): def order_item_create(id):
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
@ -31,31 +61,66 @@ def order_item_create(id):
item.user_id = current_user.id item.user_id = current_user.id
db.session.add(item) db.session.add(item)
db.session.commit() db.session.commit()
return redirect(url_for('order', 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 render_template('order_form.html', form=form, url=url_for(".order_item_create", id=id))
return abort(404) return abort(404)
@order_bp.route('/<order_id>/<item_id>/delete')
@app.route('/order/create', methods=['GET', 'POST'])
@login_required @login_required
def order_create(): def delete_item(order_id, item_id):
orderForm = OrderForm() item = OrderItem.query.filter(OrderItem.id == item_id).first()
orderForm.populate() if item.can_delete(order_id, current_user.id):
if orderForm.validate_on_submit(): db.session.delete(item)
order = Order()
orderForm.populate_obj(order)
db.session.add(order)
db.session.commit() db.session.commit()
return redirect(url_for('order', id=order.id)) return redirect(url_for('.order', id=order_id))
abort(404)
return render_template('order_form.html', form=orderForm, url=url_for("order_create"))
@app.route('/order') @order_bp.route('/<id>/volunteer')
@login_required @login_required
def orders(): def volunteer(id):
orders = Order.query.filter((Order.stoptime > datetime.now()) | (Order.stoptime == None)).all() order = Order.query.filter(Order.id == id).first()
orderForm = OrderForm() if order is not None:
orderForm.populate() print(order.courrier_id)
return render_template('orders.html', orders=orders, form=orderForm) 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)
@order_bp.route('/<id>/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