2022-04-20 02:05:10 +02:00
|
|
|
"""Script to generate the order related views of Haldis"""
|
2015-03-31 20:15:22 +02:00
|
|
|
import random
|
2022-04-22 01:15:54 +02:00
|
|
|
import re
|
2019-09-08 01:34:16 +02:00
|
|
|
import typing
|
2019-09-08 02:02:16 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
# from flask import current_app as app
|
2022-04-19 22:03:00 +02:00
|
|
|
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
|
2022-04-19 22:03:00 +02:00
|
|
|
from hlds.definitions import location_definition_version, location_definitions
|
2019-08-28 03:46:04 +02:00
|
|
|
from models import Order, OrderItem, User, db
|
|
|
|
from notification import post_order_to_webhook
|
2022-04-22 01:15:54 +02:00
|
|
|
from utils import ignore_none, parse_euro_string
|
2022-04-19 22:03:00 +02:00
|
|
|
from werkzeug.wrappers import Response
|
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:
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Generate general order view"""
|
2022-05-20 22:46:56 +02:00
|
|
|
if form is None and current_user.association_list():
|
2015-03-31 20:15:22 +02:00
|
|
|
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()
|
2022-05-30 20:23:14 +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]:
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Generate order create view"""
|
2022-05-20 22:46:56 +02:00
|
|
|
if not current_user.association_list():
|
|
|
|
flash("Not allowed to create an order.", "info")
|
|
|
|
abort(401)
|
2015-03-31 20:15:22 +02:00
|
|
|
orderForm = OrderForm()
|
|
|
|
orderForm.populate()
|
|
|
|
if orderForm.validate_on_submit():
|
|
|
|
order = Order()
|
|
|
|
orderForm.populate_obj(order)
|
2020-01-27 02:31:02 +01:00
|
|
|
order.update_from_hlds()
|
2015-03-31 20:15:22 +02:00
|
|
|
db.session.add(order)
|
|
|
|
db.session.commit()
|
2015-06-04 18:33:17 +02:00
|
|
|
post_order_to_webhook(order)
|
2022-04-20 02:05:10 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_slug", order_slug=order.slug))
|
2015-03-31 20:15:22 +02:00
|
|
|
return orders(form=orderForm)
|
|
|
|
|
|
|
|
|
2022-04-20 02:05:10 +02:00
|
|
|
@order_bp.route("/<order_slug>")
|
|
|
|
def order_from_slug(order_slug: str, form: OrderForm = None, dish_id=None) -> str:
|
|
|
|
"""Generate order view from id"""
|
|
|
|
order = Order.query.filter(Order.slug == order_slug).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:
|
2020-07-17 11:40:15 +02:00
|
|
|
form = AnonOrderItemForm() if current_user.is_anonymous() else OrderItemForm()
|
2020-01-27 22:59:07 +01:00
|
|
|
if order.location:
|
2020-02-26 18:05:56 +01:00
|
|
|
form.populate(order.location)
|
2020-01-27 03:52:29 +01:00
|
|
|
if order.is_closed():
|
2015-03-31 20:15:22 +02:00
|
|
|
form = None
|
2022-05-11 02:35:38 +02:00
|
|
|
total_price = sum(o.price or 0 for o in order.items)
|
|
|
|
debts = sum(o.price or 0 for o in order.items if not o.paid)
|
2020-02-23 23:29:39 +01:00
|
|
|
|
|
|
|
dish = order.location.dish_by_id(dish_id) if order.location else None
|
|
|
|
|
2020-07-17 11:40:15 +02:00
|
|
|
return render_template(
|
|
|
|
"order.html",
|
|
|
|
order=order,
|
|
|
|
form=form,
|
|
|
|
total_price=total_price,
|
|
|
|
debts=debts,
|
2020-08-21 15:17:10 +02:00
|
|
|
selected_dish=dish,
|
2020-07-17 11:40:15 +02:00
|
|
|
)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2022-04-20 02:05:10 +02:00
|
|
|
@order_bp.route("/<order_slug>/items")
|
|
|
|
def items_shop_view(order_slug: int) -> str:
|
|
|
|
"""Generate order items view from id"""
|
|
|
|
order = Order.query.filter(Order.slug == order_slug).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)
|
2022-05-11 02:35:38 +02:00
|
|
|
total_price = sum(o.price or 0 for o in order.items)
|
2020-01-27 03:41:38 +01:00
|
|
|
return render_template("order_items.html", order=order, total_price=total_price)
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2022-04-20 02:05:10 +02:00
|
|
|
@order_bp.route("/<order_slug>/edit", methods=["GET", "POST"])
|
2015-06-04 19:11:08 +02:00
|
|
|
@login_required
|
2022-04-20 02:05:10 +02:00
|
|
|
def order_edit(order_slug: str) -> typing.Union[str, Response]:
|
|
|
|
"""Generate order edit view from id"""
|
|
|
|
order = Order.query.filter(Order.slug == order_slug).first()
|
2020-07-17 11:40:15 +02:00
|
|
|
if current_user.id is not order.courier_id and 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)
|
2022-04-20 02:05:10 +02:00
|
|
|
order_form = OrderForm(obj=order)
|
|
|
|
order_form.populate()
|
|
|
|
if order_form.validate_on_submit():
|
|
|
|
order_form.populate_obj(order)
|
2020-01-27 03:41:38 +01:00
|
|
|
order.update_from_hlds()
|
2015-06-04 19:11:08 +02:00
|
|
|
db.session.commit()
|
2022-04-20 02:05:10 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_slug", order_slug=order.slug))
|
|
|
|
return render_template("order_edit.html", form=order_form, order_slug=order.slug)
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
2022-04-20 02:05:10 +02:00
|
|
|
@order_bp.route("/<order_slug>/create", methods=["GET", "POST"])
|
|
|
|
def order_item_create(order_slug: str) -> 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
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Add item to order from slug"""
|
|
|
|
current_order = Order.query.filter(Order.slug == order_slug).first()
|
2015-03-31 20:15:22 +02:00
|
|
|
if current_order is None:
|
|
|
|
abort(404)
|
2020-01-27 03:52:29 +01:00
|
|
|
if current_order.is_closed():
|
2015-03-31 20:15:22 +02:00
|
|
|
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)
|
2020-02-21 18:38:30 +01:00
|
|
|
location = current_order.location
|
2022-04-20 02:05:10 +02:00
|
|
|
# If location doesn't exist anymore, adding items is nonsensical
|
2020-02-21 18:38:30 +01:00
|
|
|
if not location:
|
|
|
|
abort(404)
|
2020-07-17 11:40:15 +02:00
|
|
|
form = AnonOrderItemForm() if current_user.is_anonymous() else OrderItemForm()
|
2020-02-21 18:38:30 +01:00
|
|
|
|
2022-04-19 22:03:00 +02:00
|
|
|
dish_id = (
|
|
|
|
request.form["dish_id"] if form.is_submitted() else request.args.get("dish")
|
|
|
|
)
|
2020-02-21 18:38:30 +01:00
|
|
|
if dish_id and not location.dish_by_id(dish_id):
|
|
|
|
abort(404)
|
2020-02-26 21:25:51 +01:00
|
|
|
if not form.is_submitted():
|
|
|
|
form.dish_id.data = dish_id
|
2020-02-26 18:05:56 +01:00
|
|
|
form.populate(current_order.location)
|
2020-02-21 18:38:30 +01:00
|
|
|
|
2020-02-26 21:25:51 +01:00
|
|
|
if form.is_submitted():
|
2020-08-21 15:17:10 +02:00
|
|
|
form_for_dish = request.form["dish_id"]
|
2020-02-29 15:40:14 +01:00
|
|
|
dish_was_changed = form_for_dish != "" and form_for_dish != dish_id
|
2020-02-26 21:25:51 +01:00
|
|
|
|
|
|
|
# The form's validation tests that dish_id is valid and gives a friendly error if it's not
|
|
|
|
choices = location.dish_by_id(form.dish_id.data).choices
|
|
|
|
chosen = [
|
|
|
|
(
|
|
|
|
choice.option_by_id(request.form.get("choice_" + choice.id))
|
2020-07-17 11:40:15 +02:00
|
|
|
if choice_type == "single_choice"
|
|
|
|
else list(
|
|
|
|
ignore_none(
|
|
|
|
request.form.getlist(
|
|
|
|
"choice_" + choice.id, type=choice.option_by_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2020-02-26 21:25:51 +01:00
|
|
|
)
|
|
|
|
for (choice_type, choice) in choices
|
|
|
|
]
|
|
|
|
all_choices_present = all(x is not None for x in chosen)
|
|
|
|
|
|
|
|
if dish_was_changed or not all_choices_present:
|
2020-02-27 11:21:16 +01:00
|
|
|
try:
|
2020-07-17 11:40:15 +02:00
|
|
|
user_name = (
|
|
|
|
form.user_name.data if form.user_name.validate(form) else None
|
|
|
|
)
|
2020-02-27 11:21:16 +01:00
|
|
|
except AttributeError:
|
|
|
|
user_name = None
|
2020-02-26 21:25:51 +01:00
|
|
|
comment = form.comment.data if form.comment.validate(form) else None
|
|
|
|
|
2020-07-17 11:40:15 +02:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"order_bp.order_item_create",
|
2022-04-20 02:05:10 +02:00
|
|
|
order_slug=current_order.slug,
|
2020-07-17 11:40:15 +02:00
|
|
|
dish=form.dish_id.data,
|
|
|
|
user_name=user_name,
|
|
|
|
comment=comment,
|
|
|
|
)
|
|
|
|
)
|
2020-02-26 21:25:51 +01:00
|
|
|
|
|
|
|
# If the form was not submitted (GET request) or the form had errors: show form again
|
|
|
|
if not form.validate_on_submit():
|
2022-04-20 02:05:10 +02:00
|
|
|
return order_from_slug(current_order.slug, form=form, dish_id=dish_id)
|
2020-02-21 18:38:30 +01:00
|
|
|
|
|
|
|
# Form was submitted and is valid
|
|
|
|
item = OrderItem()
|
|
|
|
form.populate_obj(item)
|
2020-02-25 17:51:53 +01:00
|
|
|
item.hlds_data_version = location_definition_version
|
2022-04-20 02:05:10 +02:00
|
|
|
item.order_id = current_order.id
|
2020-02-21 18:38:30 +01:00
|
|
|
if not current_user.is_anonymous():
|
|
|
|
item.user_id = current_user.id
|
|
|
|
else:
|
2020-02-26 21:25:51 +01:00
|
|
|
session["anon_name"] = item.user_name
|
2020-02-23 23:29:39 +01:00
|
|
|
|
2020-02-24 00:42:24 +01:00
|
|
|
# XXX Temporary until OrderItemChoice is used
|
|
|
|
def _name(option):
|
2020-02-26 21:25:51 +01:00
|
|
|
no_text_tag = "no_text"
|
2020-02-24 00:42:24 +01:00
|
|
|
try:
|
2020-02-26 21:25:51 +01:00
|
|
|
if not option or no_text_tag in option.tags:
|
|
|
|
return None
|
2020-02-24 00:42:24 +01:00
|
|
|
return option.name
|
|
|
|
except AttributeError:
|
2020-02-26 21:25:51 +01:00
|
|
|
return ", ".join(o.name for o in option if no_text_tag not in o.tags)
|
2020-07-17 11:40:15 +02:00
|
|
|
|
2020-02-26 21:25:51 +01:00
|
|
|
comments = list(ignore_none(_name(option) for option in chosen))
|
2020-02-24 00:31:14 +01:00
|
|
|
if item.comment:
|
|
|
|
comments.append("Comment: " + item.comment)
|
|
|
|
item.comment = "; ".join(comments)
|
2020-02-23 23:29:39 +01:00
|
|
|
|
2020-02-21 18:38:30 +01:00
|
|
|
item.update_from_hlds()
|
2020-02-24 00:42:24 +01:00
|
|
|
|
|
|
|
# XXX Temporary until OrderItemChoice is used. Move this price calculation to update_from_hlds
|
|
|
|
# when in OrderItemChoice is in place.
|
|
|
|
def _price(option):
|
|
|
|
try:
|
|
|
|
return option.price or 0
|
|
|
|
except AttributeError:
|
|
|
|
return sum(o.price or 0 for o in option)
|
2020-07-17 11:40:15 +02:00
|
|
|
|
2020-02-24 00:42:24 +01:00
|
|
|
item.price += sum(_price(option) for option in chosen)
|
|
|
|
|
2020-02-21 18:38:30 +01:00
|
|
|
db.session.add(item)
|
|
|
|
db.session.commit()
|
2022-04-20 02:05:10 +02:00
|
|
|
flash("Ordered %s" % item.dish_name, "success")
|
|
|
|
return redirect(url_for("order_bp.order_from_slug", order_slug=order_slug))
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2015-06-04 21:20:38 +02:00
|
|
|
|
2022-04-20 02:05:10 +02:00
|
|
|
@order_bp.route("/<order_slug>/modify_items", methods=["POST"])
|
2015-06-04 21:20:38 +02:00
|
|
|
@login_required
|
2019-09-10 02:50:22 +02:00
|
|
|
# pylint: disable=R1710
|
2022-04-20 02:05:10 +02:00
|
|
|
def modify_items(order_slug: str) -> typing.Optional[Response]:
|
2022-05-02 22:25:06 +02:00
|
|
|
if "delete_item" in request.form:
|
2022-04-20 02:05:10 +02:00
|
|
|
return delete_item(order_slug, int(request.form["delete_item"]))
|
2022-03-27 19:33:14 +02:00
|
|
|
user_names = request.form.getlist("user_names")
|
|
|
|
if request.form.get("action") == "mark_paid":
|
2022-05-20 19:41:42 +02:00
|
|
|
return set_items_paid(order_slug, user_names, True)
|
2022-03-27 19:33:14 +02:00
|
|
|
elif request.form.get("action") == "mark_unpaid":
|
2022-05-20 19:41:42 +02:00
|
|
|
return set_items_paid(order_slug, user_names, False)
|
2015-06-04 21:20:38 +02:00
|
|
|
else:
|
2022-03-27 19:33:14 +02:00
|
|
|
abort(404)
|
2022-05-02 22:25:06 +02:00
|
|
|
return None
|
2022-03-27 19:33:14 +02:00
|
|
|
|
2022-04-20 02:05:10 +02:00
|
|
|
def set_items_paid(order_slug: str, user_names: typing.Iterable[str], paid: bool):
|
|
|
|
order = Order.query.filter(Order.slug == order_slug).first()
|
2022-03-27 19:33:14 +02:00
|
|
|
total_paid_items = 0
|
|
|
|
total_failed_items = 0
|
|
|
|
for user_name in user_names:
|
|
|
|
user = User.query.filter(User.username == user_name).first()
|
|
|
|
items: typing.List[OrderItem] = []
|
|
|
|
if user:
|
|
|
|
items = OrderItem.query.filter(
|
2022-04-20 02:05:10 +02:00
|
|
|
(OrderItem.user_id == user.id) & (OrderItem.order_id == order.id)
|
2022-03-27 19:33:14 +02:00
|
|
|
).all()
|
|
|
|
else:
|
|
|
|
items = OrderItem.query.filter(
|
2022-04-20 02:05:10 +02:00
|
|
|
(OrderItem.user_name == user_name) & (OrderItem.order_id == order.id)
|
2022-03-27 19:33:14 +02:00
|
|
|
).all()
|
|
|
|
|
2015-06-04 21:20:38 +02:00
|
|
|
for item in items:
|
2022-04-20 02:05:10 +02:00
|
|
|
if item.can_modify_payment(order.id, current_user.id):
|
2022-03-27 19:33:14 +02:00
|
|
|
if item.paid != paid:
|
|
|
|
item.paid = paid
|
|
|
|
total_paid_items += 1
|
|
|
|
else:
|
|
|
|
total_failed_items += 1
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
if total_failed_items == 0:
|
|
|
|
flash("Marked %d items as paid" % (total_paid_items,), "success")
|
|
|
|
else:
|
|
|
|
flash("Failed to mark %d items as paid (succeeded in marking %d items as paid)" % (total_failed_items, total_paid_items), "error")
|
2022-04-20 02:05:10 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_slug", order_slug=order_slug))
|
2015-06-04 21:20:38 +02:00
|
|
|
|
|
|
|
|
2022-04-20 02:05:10 +02:00
|
|
|
@order_bp.route("/<order_slug>/<item_id>/delete", methods=["POST"])
|
2019-09-10 02:50:22 +02:00
|
|
|
# pylint: disable=R1710
|
2022-04-20 02:05:10 +02:00
|
|
|
def delete_item(order_slug: str, item_id: int) -> typing.Any:
|
2019-09-08 01:34:16 +02:00
|
|
|
# type is 'typing.Optional[Response]', but this errors due to
|
|
|
|
# https://github.com/python/mypy/issues/7187
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Delete an item from an order"""
|
|
|
|
item: OrderItem = OrderItem.query.filter(OrderItem.id == item_id).first()
|
|
|
|
order: Order = Order.query.filter(Order.slug == order_slug).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():
|
2019-09-10 02:50:22 +02:00
|
|
|
user_id = current_user.id
|
2022-04-20 02:05:10 +02:00
|
|
|
if item.can_delete(order.id, user_id, session.get("anon_name", "")):
|
2020-01-27 02:31:02 +01:00
|
|
|
dish_name = item.dish_name
|
2015-03-31 20:15:22 +02:00
|
|
|
db.session.delete(item)
|
|
|
|
db.session.commit()
|
2022-04-20 02:05:10 +02:00
|
|
|
flash("Deleted %s" % dish_name, "success")
|
|
|
|
return redirect(url_for("order_bp.order_from_slug", order_slug=order_slug))
|
2015-03-31 20:15:22 +02:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
2022-04-20 02:05:10 +02:00
|
|
|
@order_bp.route("/<order_slug>/volunteer", methods=["POST"])
|
2015-03-31 20:15:22 +02:00
|
|
|
@login_required
|
2022-04-20 02:05:10 +02:00
|
|
|
def volunteer(order_slug: str) -> Response:
|
|
|
|
"""Add a volunteer to an order"""
|
|
|
|
order = Order.query.filter(Order.slug == order_slug).first()
|
2015-03-31 20:15:22 +02:00
|
|
|
if order is None:
|
|
|
|
abort(404)
|
2020-01-27 03:41:38 +01:00
|
|
|
if order.courier_id is None or order.courier_id == 0:
|
2020-01-26 02:28:20 +01:00
|
|
|
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!")
|
2022-04-20 02:05:10 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_slug", order_slug=order.slug))
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
2022-04-20 02:05:10 +02:00
|
|
|
@order_bp.route("/<order_slug>/close", methods=["POST"])
|
2015-03-31 20:15:22 +02:00
|
|
|
@login_required
|
2022-04-20 02:05:10 +02:00
|
|
|
def close_order(order_slug: str) -> typing.Optional[Response]:
|
|
|
|
"""Close an order"""
|
|
|
|
order = Order.query.filter(Order.slug == order_slug).first()
|
2015-03-31 20:15:22 +02:00
|
|
|
if order is None:
|
|
|
|
abort(404)
|
2020-07-17 11:40:15 +02:00
|
|
|
if (
|
|
|
|
current_user.id == order.courier_id or current_user.is_admin()
|
|
|
|
) and not order.is_closed():
|
2015-03-31 20:15:22 +02:00
|
|
|
order.stoptime = datetime.now()
|
2020-01-27 03:41:38 +01:00
|
|
|
if order.courier_id == 0 or order.courier_id is None:
|
2020-01-26 02:28:20 +01:00
|
|
|
courier = select_user(order.items)
|
|
|
|
if courier is not None:
|
2020-01-27 03:41:38 +01:00
|
|
|
order.courier_id = courier.id
|
2015-03-31 20:15:22 +02:00
|
|
|
db.session.commit()
|
2022-04-20 02:05:10 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_slug", order_slug=order_slug))
|
2019-09-08 01:34:16 +02:00
|
|
|
return None
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
2022-05-20 19:41:42 +02:00
|
|
|
@order_bp.route("/<order_slug>/prices", methods=["GET", "POST"])
|
2022-04-22 01:15:54 +02:00
|
|
|
@login_required
|
2022-05-20 19:41:42 +02:00
|
|
|
def prices(order_slug: str) -> typing.Optional[Response]:
|
|
|
|
order = Order.query.filter(Order.slug == order_slug).first()
|
2022-04-22 01:15:54 +02:00
|
|
|
if order is None:
|
|
|
|
abort(404)
|
2022-05-02 21:56:38 +02:00
|
|
|
if not order.can_modify_prices(current_user.id):
|
|
|
|
flash("You cannot modify the prices at this time.", "error")
|
2022-05-20 19:41:42 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_slug", order_slug=order.slug))
|
2022-04-22 01:15:54 +02:00
|
|
|
|
|
|
|
if request.method == "GET":
|
|
|
|
return render_template(
|
|
|
|
"order_prices.html",
|
|
|
|
order=order,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
new_prices = {}
|
|
|
|
|
|
|
|
for key, value in request.form.items():
|
|
|
|
m = re.fullmatch("item_([0-9]+)", key)
|
|
|
|
if not m:
|
|
|
|
continue
|
|
|
|
item_id = int(m.group(1))
|
|
|
|
|
|
|
|
price = parse_euro_string(value)
|
|
|
|
if not price:
|
|
|
|
flash(f"Could not recognize '{value}' as a price")
|
|
|
|
continue
|
|
|
|
|
|
|
|
new_prices[item_id] = price
|
|
|
|
|
|
|
|
for item in order.items:
|
|
|
|
new_price = new_prices.get(item.id)
|
|
|
|
if new_price is not None and new_price != item.price:
|
|
|
|
item.price = new_price
|
|
|
|
item.price_modified = datetime.now()
|
|
|
|
db.session.commit()
|
|
|
|
|
2022-05-20 19:41:42 +02:00
|
|
|
return redirect(url_for("order_bp.order_from_slug", order_slug=order.slug))
|
2022-04-22 01:15:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-08 01:34:16 +02:00
|
|
|
def select_user(items) -> typing.Optional[User]:
|
2022-04-20 02:05:10 +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]:
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Give the list of all currently open and public Orders"""
|
2019-09-10 02:50:22 +02:00
|
|
|
order_list: typing.List[OrderForm] = []
|
2015-03-31 20:15:22 +02:00
|
|
|
if expression is None:
|
2022-05-30 20:23:14 +02:00
|
|
|
expression = ((datetime.now() > Order.starttime) & (
|
|
|
|
Order.stoptime
|
|
|
|
> datetime.now()
|
|
|
|
# pylint: disable=C0121
|
|
|
|
) | (Order.stoptime == None)
|
|
|
|
) & (Order.association.in_(current_user.association_list()))
|
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
|
2022-05-30 20:23:14 +02:00
|
|
|
expression & (Order.public == True) & (Order.association.in_(current_user.association_list()))
|
2020-07-17 11:40:15 +02:00
|
|
|
).all()
|
2019-09-10 02:50:22 +02:00
|
|
|
return order_list
|