haldis/app/views/order.py

148 lines
4.7 KiB
Python
Raw Normal View History

2015-03-28 00:02:12 +01:00
__author__ = 'feliciaan'
2015-03-28 20:38:15 +01:00
from flask import url_for, render_template, abort, redirect, Blueprint, flash, session
2015-03-28 00:02:12 +01:00
from flask.ext.login import current_user, login_required
2015-03-28 02:01:30 +01:00
import random
2015-03-28 00:02:12 +01:00
from datetime import datetime
from app import app, db
from models import Order, OrderItem
2015-03-28 20:38:15 +01:00
from forms import OrderItemForm, OrderForm, AnonOrderItemForm
2015-03-28 00:02:12 +01:00
2015-03-28 00:44:58 +01:00
order_bp = Blueprint('order_bp', 'order')
2015-03-28 00:02:12 +01:00
2015-03-28 00:44:58 +01:00
@order_bp.route('/')
def orders(form=None):
if form is None and not current_user.is_anonymous():
form = OrderForm()
form.populate()
return render_template('orders.html', orders=get_orders(), form=form)
2015-03-28 00:44:58 +01:00
@order_bp.route('/create', methods=['POST'])
2015-03-28 00:44:58 +01:00
@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 orders(form=orderForm)
2015-03-28 00:44:58 +01:00
@order_bp.route('/<id>')
def order(id, form=None):
2015-03-28 00:02:12 +01:00
order = Order.query.filter(Order.id == id).first()
2015-03-28 20:38:15 +01:00
if order is None:
abort(404)
if form is None:
form = AnonOrderItemForm() if current_user.is_anonymous() else OrderItemForm()
form.populate(order.location)
2015-03-28 21:12:02 +01:00
if order.stoptime and order.stoptime < datetime.now():
form = None
2015-03-28 20:38:15 +01:00
total_price = sum([o.product.price for o in order.items])
2015-03-28 23:06:36 +01:00
return render_template('order.html', order=order, form=form, total_price=total_price)
2015-03-28 00:02:12 +01:00
@order_bp.route('/<id>/create', methods=['POST'])
2015-03-28 00:02:12 +01:00
def order_item_create(id):
current_order = Order.query.filter(Order.id == id).first()
if current_order is None:
2015-03-28 20:38:15 +01:00
abort(404)
if current_order.stoptime and current_order.stoptime < datetime.now():
2015-03-28 21:12:02 +01:00
abort(404)
form = AnonOrderItemForm() if current_user.is_anonymous() else OrderItemForm()
form.populate(current_order.location)
2015-03-28 20:38:15 +01:00
if form.validate_on_submit():
item = OrderItem()
form.populate_obj(item)
item.order_id = id
if not current_user.is_anonymous():
2015-03-28 00:02:12 +01:00
item.user_id = current_user.id
2015-03-28 20:38:15 +01:00
else:
session['anon_name'] = item.name
db.session.add(item)
db.session.commit()
2015-03-29 13:01:42 +02:00
flash('Ordered %s' % (item.product.name), 'info')
2015-03-28 20:38:15 +01:00
return redirect(url_for('.order', id=id))
return order(id, form=form)
2015-03-28 00:02:12 +01:00
2015-03-28 00:44:58 +01:00
@order_bp.route('/<order_id>/<item_id>/delete')
def delete_item(order_id, item_id):
item = OrderItem.query.filter(OrderItem.id == item_id).first()
2015-03-28 20:38:15 +01:00
id = None
if not current_user.is_anonymous():
id = current_user.id
if item.can_delete(order_id, id, session.get('anon_name', '')):
2015-03-29 13:01:42 +02:00
product_name = item.product.name
2015-03-28 00:44:58 +01:00
db.session.delete(item)
2015-03-28 00:02:12 +01:00
db.session.commit()
2015-03-29 13:01:42 +02:00
flash('Deleted %s' % product_name, 'info')
2015-03-28 00:44:58 +01:00
return redirect(url_for('.order', id=order_id))
abort(404)
2015-03-28 00:02:12 +01:00
2015-03-28 01:09:59 +01:00
@order_bp.route('/<id>/volunteer')
@login_required
def volunteer(id):
order = Order.query.filter(Order.id == id).first()
2015-03-28 20:38:15 +01:00
if order is None:
abort(404)
if order.courrier_id is None or 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))
2015-03-28 01:09:59 +01:00
2015-03-28 02:01:30 +01:00
@order_bp.route('/<id>/close')
@login_required
def close_order(id):
order = Order.query.filter(Order.id == id).first()
2015-03-28 20:38:15 +01:00
if order is None:
2015-03-28 21:12:02 +01:00
abort(404)
2015-03-28 20:38:15 +01:00
if (current_user.id == order.courrier_id or current_user.is_admin()) \
and order.stoptime is None or (order.stoptime > datetime.now()):
order.stoptime = datetime.now()
if order.courrier_id == 0 or order.courrier_id is None:
courrier = select_user(order.items)
2015-03-28 21:12:02 +01:00
print(courrier)
2015-03-28 20:38:15 +01:00
if courrier is not None:
order.courrier_id = courrier.id
db.session.commit()
return redirect(url_for('.order', id=id))
2015-03-28 02:01:30 +01:00
app.register_blueprint(order_bp, url_prefix='/order')
def select_user(items):
user = None
2015-03-28 21:12:02 +01:00
# remove non users
items = [i for i in items if i.user_id]
2015-03-28 02:01:30 +01:00
if len(items) <= 0:
return None
while user is None:
item = random.choice(items)
user = item.user
2015-03-28 20:38:15 +01:00
if user:
if random.randint(user.bias, 100) < 80:
user = None
2015-03-28 02:01:30 +01:00
return user
2015-03-28 20:38:15 +01:00
2015-03-28 23:06:36 +01:00
def get_orders(expression=None):
2015-03-28 20:38:15 +01:00
orders = []
2015-03-28 23:06:36 +01:00
if expression is None:
expression = ((datetime.now() > Order.starttime) & (Order.stoptime > datetime.now()) | (Order.stoptime == None))
2015-03-28 20:38:15 +01:00
if not current_user.is_anonymous():
2015-03-28 21:25:55 +01:00
orders = Order.query.filter(expression).all()
2015-03-28 20:38:15 +01:00
else:
2015-03-28 21:25:55 +01:00
orders = Order.query.filter((expression & (Order.public == True))).all()
2015-03-28 20:38:15 +01:00
return orders