2019-09-10 02:50:22 +02:00
|
|
|
"Script to generate the order related views of Haldis"
|
2015-03-31 20:15:22 +02:00
|
|
|
import random
|
2019-09-08 01:34:16 +02:00
|
|
|
import typing
|
2019-09-08 02:02:16 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
from werkzeug.wrappers import Response
|
2019-09-08 02:02:16 +02:00
|
|
|
# from flask import current_app as app
|
|
|
|
from flask import (Blueprint, abort, flash, redirect, render_template, request,
|
|
|
|
session, url_for, wrappers)
|
2019-08-28 03:46:04 +02:00
|
|
|
from flask_login import current_user, login_required
|
|
|
|
|
|
|
|
from forms import AnonOrderItemForm, OrderForm, OrderItemForm
|
|
|
|
from models import Order, OrderItem, User, db
|
|
|
|
from notification import post_order_to_webhook
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-09-05 03:33:29 +02:00
|
|
|
order_bp = Blueprint("order_bp", "order")
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-05 03:33:29 +02:00
|
|
|
@order_bp.route("/")
|
2019-09-08 01:34:16 +02:00
|
|
|
def orders(form: OrderForm = None) -> str:
|
2019-09-10 02:50:22 +02:00
|
|
|
"Generate general order view"
|
2015-03-31 20:15:22 +02:00
|
|
|
if form is None and not current_user.is_anonymous():
|
|
|
|
form = OrderForm()
|
2019-09-05 03:33:29 +02:00
|
|
|
location_id = request.args.get("location_id")
|
2019-05-30 04:10:47 +02:00
|
|
|
form.location_id.default = location_id
|
|
|
|
form.process()
|
2015-03-31 20:15:22 +02:00
|
|
|
form.populate()
|
2019-09-05 03:33:29 +02:00
|
|
|
return render_template("orders.html", orders=get_orders(), form=form)
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
2019-09-05 03:33:29 +02:00
|
|
|
@order_bp.route("/create", methods=["POST"])
|
2015-03-31 20:15:22 +02:00
|
|
|
@login_required
|
2019-09-08 01:34:16 +02:00
|
|
|
def order_create() -> typing.Union[str, Response]:
|
2019-09-10 02:50:22 +02:00
|
|
|
"Generate order create view"
|
2015-03-31 20:15:22 +02:00
|
|
|
orderForm = OrderForm()
|
|
|
|
orderForm.populate()
|
|
|
|
if orderForm.validate_on_submit():
|
|
|
|
order = Order()
|
|
|
|
orderForm.populate_obj(order)
|
|
|
|
db.session.add(order)
|
|
|
|
db.session.commit()
|
2015-06-04 18:33:17 +02:00
|
|
|
post_order_to_webhook(order)
|
2019-09-10 02:50:22 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_id", order_id=order.id))
|
2015-03-31 20:15:22 +02:00
|
|
|
return orders(form=orderForm)
|
|
|
|
|
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
@order_bp.route("/<order_id>")
|
|
|
|
def order_from_id(order_id: int, form: OrderForm = None) -> str:
|
|
|
|
"Generate order view from id"
|
|
|
|
order = Order.query.filter(Order.id == order_id).first()
|
2015-03-31 20:15:22 +02:00
|
|
|
if order is None:
|
|
|
|
abort(404)
|
|
|
|
if current_user.is_anonymous() and not order.public:
|
2019-09-05 03:33:29 +02:00
|
|
|
flash("Please login to see this order.", "info")
|
2015-03-31 20:15:22 +02:00
|
|
|
abort(401)
|
|
|
|
if form is None:
|
2019-09-10 02:50:22 +02:00
|
|
|
form = AnonOrderItemForm() if current_user.is_anonymous() \
|
|
|
|
else OrderItemForm()
|
2015-03-31 20:15:22 +02:00
|
|
|
form.populate(order.location)
|
|
|
|
if order.stoptime and order.stoptime < datetime.now():
|
|
|
|
form = None
|
|
|
|
total_price = sum([o.product.price for o in order.items])
|
2015-06-04 21:20:38 +02:00
|
|
|
debts = sum([o.product.price for o in order.items if not o.paid])
|
2019-09-10 02:50:22 +02:00
|
|
|
return render_template("order.html", order=order, form=form,
|
|
|
|
total_price=total_price, debts=debts)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
@order_bp.route("/<order_id>/items")
|
|
|
|
def items_showcase(order_id: int) -> str:
|
|
|
|
"Generate order items view from id"
|
|
|
|
order = Order.query.filter(Order.id == order_id).first()
|
2019-05-29 20:12:19 +02:00
|
|
|
if order is None:
|
|
|
|
abort(404)
|
|
|
|
if current_user.is_anonymous() and not order.public:
|
2019-09-05 03:33:29 +02:00
|
|
|
flash("Please login to see this order.", "info")
|
2019-05-29 20:12:19 +02:00
|
|
|
abort(401)
|
2019-09-05 03:33:29 +02:00
|
|
|
return render_template("order_items.html", order=order)
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
@order_bp.route("/<order_id>/edit", methods=["GET", "POST"])
|
2015-06-04 19:11:08 +02:00
|
|
|
@login_required
|
2019-09-10 02:50:22 +02:00
|
|
|
def order_edit(order_id: int) -> typing.Union[str, Response]:
|
|
|
|
"Generate order edit view from id"
|
|
|
|
order = Order.query.filter(Order.id == order_id).first()
|
2020-01-26 02:28:20 +01:00
|
|
|
if current_user.id is not order.courier_id and \
|
2019-09-10 02:50:22 +02:00
|
|
|
not current_user.is_admin():
|
2015-06-04 21:36:57 +02:00
|
|
|
abort(401)
|
2015-06-04 19:11:08 +02:00
|
|
|
if order is None:
|
|
|
|
abort(404)
|
|
|
|
orderForm = OrderForm(obj=order)
|
|
|
|
orderForm.populate()
|
|
|
|
if orderForm.validate_on_submit():
|
|
|
|
orderForm.populate_obj(order)
|
|
|
|
db.session.commit()
|
2019-09-10 02:50:22 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_id", order_id=order.id))
|
|
|
|
return render_template("order_edit.html", form=orderForm,
|
|
|
|
order_id=order_id)
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
@order_bp.route("/<order_id>/create", methods=["POST"])
|
|
|
|
def order_item_create(order_id: int) -> typing.Any:
|
2019-09-08 01:34:16 +02:00
|
|
|
# type is 'typing.Union[str, Response]', but this errors due to
|
|
|
|
# https://github.com/python/mypy/issues/7187
|
2019-09-10 02:50:22 +02:00
|
|
|
"Add item to order from id"
|
|
|
|
current_order = Order.query.filter(Order.id == order_id).first()
|
2015-03-31 20:15:22 +02:00
|
|
|
if current_order is None:
|
|
|
|
abort(404)
|
|
|
|
if current_order.stoptime and current_order.stoptime < datetime.now():
|
|
|
|
abort(404)
|
|
|
|
if current_user.is_anonymous() and not current_order.public:
|
2019-09-05 03:33:29 +02:00
|
|
|
flash("Please login to see this order.", "info")
|
2015-03-31 20:15:22 +02:00
|
|
|
abort(401)
|
2019-09-10 02:50:22 +02:00
|
|
|
form = AnonOrderItemForm() if current_user.is_anonymous() \
|
|
|
|
else OrderItemForm()
|
2015-03-31 20:15:22 +02:00
|
|
|
form.populate(current_order.location)
|
|
|
|
if form.validate_on_submit():
|
|
|
|
item = OrderItem()
|
|
|
|
form.populate_obj(item)
|
2019-09-10 02:50:22 +02:00
|
|
|
item.order_id = order_id
|
2015-03-31 20:15:22 +02:00
|
|
|
if not current_user.is_anonymous():
|
|
|
|
item.user_id = current_user.id
|
|
|
|
else:
|
2019-09-05 03:33:29 +02:00
|
|
|
session["anon_name"] = item.name
|
2015-03-31 20:15:22 +02:00
|
|
|
db.session.add(item)
|
|
|
|
db.session.commit()
|
2019-09-05 03:33:29 +02:00
|
|
|
flash("Ordered %s" % (item.product.name), "success")
|
2019-09-10 02:50:22 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_id", order_id=order_id))
|
|
|
|
return order_from_id(order_id, form=form)
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2015-06-04 21:20:38 +02:00
|
|
|
|
2019-09-11 16:44:36 +02:00
|
|
|
@order_bp.route("/<order_id>/<item_id>/paid", methods=["POST"])
|
2015-06-04 21:20:38 +02:00
|
|
|
@login_required
|
2019-09-10 02:50:22 +02:00
|
|
|
# pylint: disable=R1710
|
2019-09-08 01:34:16 +02:00
|
|
|
def item_paid(order_id: int, item_id: int) -> typing.Optional[Response]:
|
2019-09-10 02:50:22 +02:00
|
|
|
"Indicate payment status for an item in an order"
|
2015-06-04 21:20:38 +02:00
|
|
|
item = OrderItem.query.filter(OrderItem.id == item_id).first()
|
2019-09-10 02:50:22 +02:00
|
|
|
user_id = current_user.id
|
2020-01-26 02:28:20 +01:00
|
|
|
if item.order.courier_id == user_id or current_user.admin:
|
2015-06-04 21:20:38 +02:00
|
|
|
item.paid = True
|
|
|
|
db.session.commit()
|
2019-09-10 02:50:22 +02:00
|
|
|
flash("Paid %s by %s" % (item.product.name, item.get_name()),
|
|
|
|
"success")
|
|
|
|
return redirect(url_for("order_bp.order_from_id", order_id=order_id))
|
2015-06-04 21:20:38 +02:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
2019-09-11 16:44:36 +02:00
|
|
|
@order_bp.route("/<order_id>/<user_name>/user_paid", methods=["POST"])
|
2015-06-04 21:20:38 +02:00
|
|
|
@login_required
|
2019-09-10 02:50:22 +02:00
|
|
|
# pylint: disable=R1710
|
2019-09-11 16:01:49 +02:00
|
|
|
def items_user_paid(order_id: int, user_name: str) -> typing.Optional[Response]:
|
2019-09-10 02:50:22 +02:00
|
|
|
"Indicate payment status for a user in an order"
|
2015-06-04 21:20:38 +02:00
|
|
|
user = User.query.filter(User.username == user_name).first()
|
2019-09-08 01:34:16 +02:00
|
|
|
items: typing.List[OrderItem] = []
|
2015-06-04 21:20:38 +02:00
|
|
|
if user:
|
2019-09-05 03:33:29 +02:00
|
|
|
items = OrderItem.query.filter(
|
|
|
|
(OrderItem.user_id == user.id) & (OrderItem.order_id == order_id)
|
2019-09-11 16:44:36 +02:00
|
|
|
).all()
|
2015-06-04 21:20:38 +02:00
|
|
|
else:
|
2019-09-05 03:33:29 +02:00
|
|
|
items = OrderItem.query.filter(
|
|
|
|
(OrderItem.name == user_name) & (OrderItem.order_id == order_id)
|
2019-09-11 16:44:36 +02:00
|
|
|
).all()
|
2015-06-04 21:20:38 +02:00
|
|
|
current_order = Order.query.filter(Order.id == order_id).first()
|
|
|
|
for item in items:
|
|
|
|
print(item)
|
2020-01-26 02:28:20 +01:00
|
|
|
if current_order.courier_id == current_user.id or current_user.admin:
|
2015-06-04 21:20:38 +02:00
|
|
|
for item in items:
|
|
|
|
item.paid = True
|
|
|
|
db.session.commit()
|
2019-09-10 02:50:22 +02:00
|
|
|
flash("Paid %d items for %s" %
|
|
|
|
(len(items), item.get_name()), "success")
|
|
|
|
return redirect(url_for("order_bp.order_from_id", order_id=order_id))
|
2015-06-04 21:20:38 +02:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
2019-09-11 16:44:36 +02:00
|
|
|
@order_bp.route("/<order_id>/<item_id>/delete", methods=["POST"])
|
2019-09-10 02:50:22 +02:00
|
|
|
# pylint: disable=R1710
|
2019-09-08 01:34:16 +02:00
|
|
|
def delete_item(order_id: int, item_id: int) -> typing.Any:
|
|
|
|
# type is 'typing.Optional[Response]', but this errors due to
|
|
|
|
# https://github.com/python/mypy/issues/7187
|
2019-09-10 02:50:22 +02:00
|
|
|
"Delete an item from an order"
|
2015-03-31 20:15:22 +02:00
|
|
|
item = OrderItem.query.filter(OrderItem.id == item_id).first()
|
2019-09-10 02:50:22 +02:00
|
|
|
user_id = None
|
2015-03-31 20:15:22 +02:00
|
|
|
if not current_user.is_anonymous():
|
2015-10-30 20:30:18 +01:00
|
|
|
print("%s tries to delete orders" % (current_user.username))
|
2019-09-10 02:50:22 +02:00
|
|
|
user_id = current_user.id
|
|
|
|
if item.can_delete(order_id, user_id, session.get("anon_name", "")):
|
2015-03-31 20:15:22 +02:00
|
|
|
product_name = item.product.name
|
|
|
|
db.session.delete(item)
|
|
|
|
db.session.commit()
|
2019-09-05 03:33:29 +02:00
|
|
|
flash("Deleted %s" % (product_name), "success")
|
2019-09-10 02:50:22 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_id", order_id=order_id))
|
2015-03-31 20:15:22 +02:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
@order_bp.route("/<order_id>/volunteer", methods=["POST"])
|
2015-03-31 20:15:22 +02:00
|
|
|
@login_required
|
2019-09-10 02:50:22 +02:00
|
|
|
def volunteer(order_id: int) -> Response:
|
|
|
|
"Add a volunteer to an order"
|
|
|
|
order = Order.query.filter(Order.id == order_id).first()
|
2015-03-31 20:15:22 +02:00
|
|
|
if order is None:
|
|
|
|
abort(404)
|
2020-01-26 02:28:20 +01:00
|
|
|
if order.courier_id is None or order.courrier_id == 0:
|
|
|
|
order.courier_id = current_user.id
|
2015-03-31 20:15:22 +02:00
|
|
|
db.session.commit()
|
|
|
|
flash("Thank you for volunteering!")
|
|
|
|
else:
|
|
|
|
flash("Volunteering not possible!")
|
2019-09-10 02:50:22 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_id", order_id=order_id))
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
@order_bp.route("/<order_id>/close", methods=["POST"])
|
2015-03-31 20:15:22 +02:00
|
|
|
@login_required
|
2019-09-10 02:50:22 +02:00
|
|
|
def close_order(order_id: int) -> typing.Optional[Response]:
|
|
|
|
"Close an order"
|
|
|
|
order = Order.query.filter(Order.id == order_id).first()
|
2015-03-31 20:15:22 +02:00
|
|
|
if order is None:
|
|
|
|
abort(404)
|
2020-01-26 02:28:20 +01:00
|
|
|
if (current_user.id == order.courier_id or current_user.is_admin()) and (
|
2019-09-10 02:50:22 +02:00
|
|
|
order.stoptime is None or (order.stoptime > datetime.now())):
|
2015-03-31 20:15:22 +02:00
|
|
|
order.stoptime = datetime.now()
|
2020-01-26 02:28:20 +01:00
|
|
|
if order.courier_id == 0 or order.courrier_id is None:
|
|
|
|
courier = select_user(order.items)
|
|
|
|
print(courier)
|
|
|
|
if courier is not None:
|
|
|
|
order.courier_id = courrier.id
|
2015-03-31 20:15:22 +02:00
|
|
|
db.session.commit()
|
2019-09-10 02:50:22 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_id", order_id=order_id))
|
2019-09-08 01:34:16 +02:00
|
|
|
# The line below is to make sure mypy doesn't say
|
|
|
|
# "Missing return statement"
|
|
|
|
# https://github.com/python/mypy/issues/4223
|
|
|
|
return None
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
2019-09-08 01:34:16 +02:00
|
|
|
def select_user(items) -> typing.Optional[User]:
|
2019-09-10 02:50:22 +02:00
|
|
|
"Select a random user from those who are signed up for the order"
|
2015-03-31 20:15:22 +02:00
|
|
|
user = None
|
|
|
|
# remove non users
|
|
|
|
items = [i for i in items if i.user_id]
|
|
|
|
|
2019-09-10 02:50:22 +02:00
|
|
|
if not items:
|
2015-03-31 20:15:22 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
while user is None:
|
|
|
|
item = random.choice(items)
|
|
|
|
user = item.user
|
|
|
|
if user:
|
|
|
|
if random.randint(user.bias, 100) < 80:
|
|
|
|
user = None
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
2019-09-08 01:34:16 +02:00
|
|
|
def get_orders(expression=None) -> typing.List[Order]:
|
2019-09-10 02:50:22 +02:00
|
|
|
"Give the list of all currently open and public Orders"
|
|
|
|
order_list: typing.List[OrderForm] = []
|
2015-03-31 20:15:22 +02:00
|
|
|
if expression is None:
|
2019-09-05 03:33:29 +02:00
|
|
|
expression = (datetime.now() > Order.starttime) & (
|
|
|
|
Order.stoptime > datetime.now()
|
2019-09-10 02:50:22 +02:00
|
|
|
# pylint: disable=C0121
|
2019-09-05 03:33:29 +02:00
|
|
|
) | (Order.stoptime == None)
|
2015-03-31 20:15:22 +02:00
|
|
|
if not current_user.is_anonymous():
|
2019-09-10 02:50:22 +02:00
|
|
|
order_list = Order.query.filter(expression).all()
|
2015-03-31 20:15:22 +02:00
|
|
|
else:
|
2019-09-10 02:50:22 +02:00
|
|
|
order_list = Order.query.filter(
|
|
|
|
# pylint: disable=C0121
|
|
|
|
(expression & (Order.public == True))).all()
|
|
|
|
return order_list
|