haldis/app/forms.py

90 lines
2.9 KiB
Python
Raw Normal View History

2019-09-10 13:17:35 +00:00
"Script for everything form related in Haldis"
2015-03-31 18:15:22 +00:00
from datetime import datetime, timedelta
2015-03-31 18:15:22 +00:00
from flask import session
2017-01-06 11:05:31 +00:00
from flask_login import current_user
from flask_wtf import FlaskForm as Form
2019-09-07 23:58:21 +00:00
from wtforms import (DateTimeField, SelectField, StringField, SubmitField,
validators)
2017-01-06 11:05:31 +00:00
from utils import euro_string
from hlds.definitions import location_definitions
from hlds.models import Location
from models import User
2015-03-31 18:15:22 +00:00
class OrderForm(Form):
2019-09-10 13:17:35 +00:00
"Class which defines the form for a new Order"
# pylint: disable=R0903
courier_id = SelectField("Courier", coerce=int)
2019-09-05 01:33:29 +00:00
location_id = SelectField(
"Location", coerce=str, validators=[validators.required()]
2019-09-05 01:33:29 +00:00
)
starttime = DateTimeField(
"Starttime", default=datetime.now, format="%d-%m-%Y %H:%M"
)
stoptime = DateTimeField("Stoptime", format="%d-%m-%Y %H:%M")
submit_button = SubmitField("Submit")
2015-03-31 18:15:22 +00:00
2019-09-07 23:58:21 +00:00
def populate(self) -> None:
"Fill in the options for courier for an Order"
2015-03-31 18:15:22 +00:00
if current_user.is_admin():
self.courier_id.choices = [(0, None)] + [
2019-09-05 01:33:29 +00:00
(u.id, u.username) for u in User.query.order_by("username")
]
2015-03-31 18:15:22 +00:00
else:
self.courier_id.choices = [
2019-09-05 01:33:29 +00:00
(0, None),
(current_user.id, current_user.username),
]
self.location_id.choices = [
(l.id, l.name) for l in location_definitions
2019-09-05 01:33:29 +00:00
]
2015-03-31 18:15:22 +00:00
if self.stoptime.data is None:
self.stoptime.data = datetime.now() + timedelta(hours=1)
class OrderItemForm(Form):
2019-09-10 13:17:35 +00:00
"Class which defines the form for a new Item in an Order"
# pylint: disable=R0903
dish_id = SelectField("Dish")
comment = StringField("Comment")
2019-09-05 01:33:29 +00:00
submit_button = SubmitField("Submit")
2015-03-31 18:15:22 +00:00
2019-09-07 23:58:21 +00:00
def populate(self, location: Location) -> None:
"Fill in all the dish options from the location"
self.dish_id.choices = [
2019-09-10 13:17:35 +00:00
(i.id, (i.name + ": " + euro_string(i.price)))
for i in location.dishes
2019-09-05 01:33:29 +00:00
]
2015-03-31 18:15:22 +00:00
class AnonOrderItemForm(OrderItemForm):
2019-09-10 13:17:35 +00:00
"""
Class which defines the form for a new Item in an Order
For Users who aren't logged in
"""
2019-09-05 01:33:29 +00:00
name = StringField("Name", validators=[validators.required()])
2015-03-31 18:15:22 +00:00
2019-09-07 23:58:21 +00:00
def populate(self, location: Location) -> None:
2019-09-10 13:17:35 +00:00
"""
Fill in all the dish options from the location and
2019-09-10 13:17:35 +00:00
the name of the anon user
"""
2015-03-31 18:15:22 +00:00
OrderItemForm.populate(self, location)
if self.name.data is None:
2019-09-05 01:33:29 +00:00
self.name.data = session.get("anon_name", None)
2015-03-31 18:15:22 +00:00
2019-09-07 23:58:21 +00:00
def validate(self) -> bool:
2019-09-10 13:17:35 +00:00
"Check if the provided anon_name is not already taken"
2015-03-31 18:15:22 +00:00
rv = OrderForm.validate(self)
if not rv:
return False
# check if we have a user with this name
user = User.query.filter_by(username=self.name.data).first()
if user is not None:
2019-09-05 01:33:29 +00:00
self.name.errors.append("Name already in use")
2015-03-31 18:15:22 +00:00
return False
return True