2019-09-10 01:06:11 +02:00
|
|
|
"Script for everything Order related in the database"
|
2019-09-08 00:41:50 +02:00
|
|
|
import typing
|
2019-08-28 03:46:04 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
from utils import first
|
|
|
|
from hlds.definitions import location_definitions
|
2019-08-28 03:46:04 +02:00
|
|
|
from .database import db
|
|
|
|
from .user import User
|
|
|
|
|
|
|
|
|
|
|
|
class Order(db.Model):
|
2019-09-10 01:06:11 +02:00
|
|
|
"Class used for configuring the Order model in the database"
|
2019-08-28 03:46:04 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-01-26 02:28:20 +01:00
|
|
|
courier_id = db.Column(db.Integer, nullable=True)
|
2020-01-26 02:39:58 +01:00
|
|
|
location_id = db.Column(db.String(64))
|
|
|
|
location_name = db.Column(db.String(128))
|
2019-08-28 03:46:04 +02:00
|
|
|
starttime = db.Column(db.DateTime)
|
|
|
|
stoptime = db.Column(db.DateTime)
|
|
|
|
public = db.Column(db.Boolean, default=True)
|
2020-01-26 02:39:58 +01:00
|
|
|
|
2019-09-05 03:33:29 +02:00
|
|
|
items = db.relationship("OrderItem", backref="order", lazy="dynamic")
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
def __getattr__(self, name):
|
|
|
|
if name == "location":
|
|
|
|
location = first(filter(lambda l: l.id == self.location_id, location_definitions))
|
|
|
|
if location:
|
|
|
|
return location
|
|
|
|
raise AttributeError()
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def __repr__(self) -> str:
|
2019-09-10 01:06:11 +02:00
|
|
|
# pylint: disable=R1705
|
2019-08-28 03:46:04 +02:00
|
|
|
if self.location:
|
2019-09-05 03:33:29 +02:00
|
|
|
return "Order %d @ %s" % (self.id, self.location.name or "None")
|
2019-08-28 03:46:04 +02:00
|
|
|
else:
|
2019-09-05 03:33:29 +02:00
|
|
|
return "Order %d" % (self.id)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
def update_from_hlds(self) -> None:
|
|
|
|
"""
|
|
|
|
Update the location name from the HLDS definition.
|
|
|
|
User should commit after running this to make the change persistent.
|
|
|
|
"""
|
|
|
|
assert self.location_id, "location_id must be configured before updating from HLDS"
|
|
|
|
self.location_name = self.location.name
|
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def group_by_user(self) -> typing.Dict[str, typing.Any]:
|
2019-09-10 01:06:11 +02:00
|
|
|
"Group items of an Order by user"
|
2019-09-08 00:41:50 +02:00
|
|
|
group: typing.Dict[str, typing.Any] = dict()
|
2019-08-28 03:46:04 +02:00
|
|
|
for item in self.items:
|
|
|
|
user = group.get(item.get_name(), dict())
|
2020-01-26 02:52:45 +01:00
|
|
|
user["total"] = user.get("total", 0) + item.price
|
2019-09-08 02:02:16 +02:00
|
|
|
user["to_pay"] = (
|
2019-09-10 01:06:11 +02:00
|
|
|
user.get("to_pay", 0) +
|
2020-01-26 02:52:45 +01:00
|
|
|
item.price if not item.paid else 0
|
2019-09-08 02:02:16 +02:00
|
|
|
)
|
2019-08-28 03:46:04 +02:00
|
|
|
user["paid"] = user.get("paid", True) and item.paid
|
2020-01-27 02:31:02 +01:00
|
|
|
user["dishes"] = user.get("dishes", []) + [item.dish_name]
|
2019-08-28 03:46:04 +02:00
|
|
|
group[item.get_name()] = user
|
|
|
|
|
|
|
|
return group
|
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
def group_by_dish(self) -> typing.Dict[str, typing.Any]:
|
|
|
|
"Group items of an Order by dish"
|
2019-09-08 00:41:50 +02:00
|
|
|
group: typing.Dict[str, typing.Any] = dict()
|
2019-08-28 03:46:04 +02:00
|
|
|
for item in self.items:
|
2020-01-27 02:31:02 +01:00
|
|
|
dish = group.get(item.dish_name, dict())
|
|
|
|
dish["count"] = dish.get("count", 0) + 1
|
2020-01-27 03:01:49 +01:00
|
|
|
dish["comments"] = dish.get("comments", []) + [item.comment]
|
2020-01-27 02:31:02 +01:00
|
|
|
group[item.dish_name] = dish
|
2019-08-28 03:46:04 +02:00
|
|
|
|
|
|
|
return group
|
|
|
|
|
2020-01-27 03:41:38 +01:00
|
|
|
def is_closed(self) -> bool:
|
2020-01-27 03:52:29 +01:00
|
|
|
return self.stoptime and datetime.now() > self.stoptime
|
2020-01-27 03:41:38 +01:00
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def can_close(self, user_id: int) -> bool:
|
2019-09-10 01:06:11 +02:00
|
|
|
"Check if a user can close the Order"
|
2019-08-28 03:46:04 +02:00
|
|
|
if self.stoptime and self.stoptime < datetime.now():
|
|
|
|
return False
|
|
|
|
user = None
|
|
|
|
if user_id:
|
|
|
|
user = User.query.filter_by(id=user_id).first()
|
|
|
|
print(user)
|
2020-01-26 02:28:20 +01:00
|
|
|
if self.courier_id == user_id or (user and user.is_admin()):
|
2019-08-28 03:46:04 +02:00
|
|
|
return True
|
|
|
|
return False
|