2019-09-10 15:17:35 +02:00
|
|
|
"Script for everything form related in Haldis"
|
2015-03-31 20:15:22 +02:00
|
|
|
from datetime import datetime, timedelta
|
2020-02-21 18:38:30 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
2022-04-19 22:03:00 +02:00
|
|
|
from flask import request, session
|
2017-01-06 12:05:31 +01:00
|
|
|
from flask_login import current_user
|
|
|
|
from flask_wtf import FlaskForm as Form
|
2020-01-26 16:02:03 +01:00
|
|
|
from hlds.definitions import location_definitions
|
2022-04-19 22:03:00 +02:00
|
|
|
from hlds.models import Choice, Dish, Location
|
2020-01-26 16:02:03 +01:00
|
|
|
from models import User
|
2022-04-19 22:03:00 +02:00
|
|
|
from utils import euro_string, price_range_string
|
|
|
|
from wtforms import (DateTimeField, FieldList, SelectField,
|
|
|
|
SelectMultipleField, StringField, SubmitField, validators)
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class OrderForm(Form):
|
2019-09-10 15:17:35 +02:00
|
|
|
"Class which defines the form for a new Order"
|
|
|
|
# pylint: disable=R0903
|
2020-01-26 02:28:20 +01:00
|
|
|
courier_id = SelectField("Courier", coerce=int)
|
2019-09-05 03:33:29 +02:00
|
|
|
location_id = SelectField(
|
2020-01-27 02:31:02 +01:00
|
|
|
"Location", coerce=str, validators=[validators.required()]
|
2019-09-05 03:33:29 +02:00
|
|
|
)
|
|
|
|
starttime = DateTimeField(
|
|
|
|
"Starttime", default=datetime.now, format="%d-%m-%Y %H:%M"
|
|
|
|
)
|
|
|
|
stoptime = DateTimeField("Stoptime", format="%d-%m-%Y %H:%M")
|
2022-05-20 22:46:56 +02:00
|
|
|
association = SelectField("Association", coerce=str, validators=[validators.required()])
|
2019-09-05 03:33:29 +02:00
|
|
|
submit_button = SubmitField("Submit")
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-09-08 01:58:21 +02:00
|
|
|
def populate(self) -> None:
|
2020-01-26 02:28:20 +01:00
|
|
|
"Fill in the options for courier for an Order"
|
2015-03-31 20:15:22 +02:00
|
|
|
if current_user.is_admin():
|
2023-01-24 19:06:10 +01:00
|
|
|
self.courier_id.choices = [
|
|
|
|
(0, None),
|
|
|
|
(current_user.id, current_user.username),
|
|
|
|
] + [
|
|
|
|
(u.id, u.username) for u in User.query.order_by("username") if u.id != current_user.id
|
2019-09-05 03:33:29 +02:00
|
|
|
]
|
2015-03-31 20:15:22 +02:00
|
|
|
else:
|
2020-01-26 02:28:20 +01:00
|
|
|
self.courier_id.choices = [
|
2019-09-05 03:33:29 +02:00
|
|
|
(0, None),
|
|
|
|
(current_user.id, current_user.username),
|
|
|
|
]
|
2020-07-17 11:40:15 +02:00
|
|
|
self.location_id.choices = [(l.id, l.name) for l in location_definitions]
|
2022-05-20 22:46:56 +02:00
|
|
|
self.association.choices = current_user.association_list()
|
2015-03-31 20:15:22 +02:00
|
|
|
if self.stoptime.data is None:
|
|
|
|
self.stoptime.data = datetime.now() + timedelta(hours=1)
|
|
|
|
|
|
|
|
|
|
|
|
class OrderItemForm(Form):
|
2020-02-21 18:38:30 +01:00
|
|
|
"New Item in an Order"
|
2019-09-10 15:17:35 +02:00
|
|
|
# pylint: disable=R0903
|
2020-01-27 02:31:02 +01:00
|
|
|
dish_id = SelectField("Dish")
|
|
|
|
comment = StringField("Comment")
|
2019-09-05 03:33:29 +02:00
|
|
|
submit_button = SubmitField("Submit")
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2020-02-26 18:05:56 +01:00
|
|
|
def populate(self, location: Location) -> None:
|
2022-04-19 23:20:03 +02:00
|
|
|
"Populate the order item form"
|
2020-09-26 00:30:40 +02:00
|
|
|
self.dish_id.choices = [(dish.id, dish.name) for dish in location.dishes]
|
2020-02-26 21:25:51 +01:00
|
|
|
if not self.is_submitted() and self.comment.data is None:
|
|
|
|
self.comment.data = request.args.get("comment")
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AnonOrderItemForm(OrderItemForm):
|
2019-09-10 15:17:35 +02:00
|
|
|
"""
|
|
|
|
Class which defines the form for a new Item in an Order
|
|
|
|
For Users who aren't logged in
|
|
|
|
"""
|
2020-07-17 11:40:15 +02:00
|
|
|
|
2020-02-26 21:25:51 +01:00
|
|
|
user_name = StringField("Name", validators=[validators.required()])
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-09-08 01:58:21 +02:00
|
|
|
def populate(self, location: Location) -> None:
|
2019-09-10 15:17:35 +02:00
|
|
|
"""
|
2020-01-27 02:31:02 +01:00
|
|
|
Fill in all the dish options from the location and
|
2019-09-10 15:17:35 +02:00
|
|
|
the name of the anon user
|
|
|
|
"""
|
2015-03-31 20:15:22 +02:00
|
|
|
OrderItemForm.populate(self, location)
|
2020-02-26 21:25:51 +01:00
|
|
|
if not self.is_submitted():
|
|
|
|
if self.user_name.data is None:
|
|
|
|
self.user_name.data = request.args.get("user_name")
|
|
|
|
if self.user_name.data is None:
|
|
|
|
self.user_name.data = session.get("anon_name", None)
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-09-08 01:58:21 +02:00
|
|
|
def validate(self) -> bool:
|
2022-04-19 23:59:23 +02:00
|
|
|
"""Check if the provided anon_name is not already taken"""
|
2015-03-31 20:15:22 +02:00
|
|
|
rv = OrderForm.validate(self)
|
|
|
|
if not rv:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# check if we have a user with this name
|
2020-02-26 21:25:51 +01:00
|
|
|
user = User.query.filter_by(username=self.user_name.data).first()
|
2015-03-31 20:15:22 +02:00
|
|
|
if user is not None:
|
2020-02-26 21:25:51 +01:00
|
|
|
self.user_name.errors.append("Name already in use")
|
2015-03-31 20:15:22 +02:00
|
|
|
return False
|
|
|
|
return True
|