2019-09-10 01:06:11 +02:00
|
|
|
"Script for everything OrderItem related in the database"
|
2019-08-28 03:46:04 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
from hlds.definitions import location_definitions
|
2022-04-19 22:03:00 +02:00
|
|
|
from utils import first
|
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
from .database import db
|
2019-09-08 00:41:50 +02:00
|
|
|
from .order import Order
|
2019-08-28 03:46:04 +02:00
|
|
|
from .user import User
|
|
|
|
|
|
|
|
|
|
|
|
class OrderItem(db.Model):
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Class used for configuring the OrderItem model in the database"""
|
2019-08-28 03:46:04 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2019-09-05 03:33:29 +02:00
|
|
|
order_id = db.Column(db.Integer, db.ForeignKey("order.id"), nullable=False)
|
2020-01-26 02:39:58 +01:00
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
|
|
|
|
user_name = db.Column(db.String(120))
|
2020-01-26 18:53:14 +01:00
|
|
|
dish_id = db.Column(db.String(64), nullable=True)
|
2020-01-26 02:39:58 +01:00
|
|
|
dish_name = db.Column(db.String(120), nullable=True)
|
2020-01-26 21:01:04 +01:00
|
|
|
price = db.Column(db.Integer, nullable=True)
|
2020-07-17 11:40:15 +02:00
|
|
|
paid = db.Column(db.Boolean, default=False, nullable=True)
|
2020-01-26 16:25:59 +01:00
|
|
|
comment = db.Column(db.Text(), nullable=True)
|
2020-01-26 02:39:58 +01:00
|
|
|
hlds_data_version = db.Column(db.String(40), nullable=True)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2022-04-19 22:03:00 +02:00
|
|
|
choices = db.relationship("OrderItemChoice",
|
|
|
|
backref="order_item",
|
|
|
|
lazy="dynamic")
|
2020-01-26 16:25:59 +01:00
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
def __getattr__(self, name):
|
|
|
|
if name == "dish":
|
2022-04-19 22:03:00 +02:00
|
|
|
location_id = (Order.query.filter(
|
|
|
|
Order.id == self.order_id).first().location_id)
|
2020-07-17 11:40:15 +02:00
|
|
|
location = first(
|
2022-04-19 22:03:00 +02:00
|
|
|
filter(lambda l: l.id == location_id, location_definitions))
|
2020-01-27 02:31:02 +01:00
|
|
|
if location:
|
2022-04-19 22:03:00 +02:00
|
|
|
return first(
|
|
|
|
filter(lambda d: d.id == self.dish_id, location.dishes))
|
|
|
|
raise ValueError(f"No Location found with id: {location_id}")
|
2020-01-27 02:31:02 +01:00
|
|
|
raise AttributeError()
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2020-08-14 04:57:02 +02:00
|
|
|
@property
|
|
|
|
def for_name(self) -> str:
|
2019-09-10 01:06:11 +02:00
|
|
|
"Get the name of the user which 'owns' the item"
|
2019-08-28 03:46:04 +02:00
|
|
|
if self.user_id is not None and self.user_id > 0:
|
|
|
|
return self.user.username
|
2020-01-27 02:31:02 +01:00
|
|
|
return self.user_name
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def __repr__(self) -> str:
|
2022-04-19 22:03:00 +02:00
|
|
|
return "Order {self.order_id or 0}: {self.for_name} wants {self.dish_name or 'None'}"
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2020-01-27 02:31:02 +01:00
|
|
|
def update_from_hlds(self) -> None:
|
|
|
|
"""
|
|
|
|
Update the dish name and price from the HLDS definition.
|
|
|
|
User should commit after running this to make the change persistent.
|
|
|
|
"""
|
|
|
|
assert self.order_id, "order_id must be configured before updating from HLDS"
|
|
|
|
assert self.dish_id, "dish_id must be configured before updating from HLDS"
|
|
|
|
self.dish_name = self.dish.name
|
|
|
|
self.price = self.dish.price
|
|
|
|
|
2019-09-10 01:06:11 +02:00
|
|
|
# pylint: disable=W0613
|
2019-09-08 00:41:50 +02:00
|
|
|
def can_delete(self, order_id: int, user_id: int, name: str) -> bool:
|
2022-04-20 02:05:10 +02:00
|
|
|
"""Check if a user can delete an item"""
|
2019-08-28 03:46:04 +02:00
|
|
|
if int(self.order_id) != int(order_id):
|
|
|
|
return False
|
2020-01-27 03:52:29 +01:00
|
|
|
if self.order.is_closed():
|
2019-08-28 03:46:04 +02:00
|
|
|
return False
|
|
|
|
if self.user is not None and self.user_id == user_id:
|
|
|
|
return True
|
|
|
|
if user_id is None:
|
|
|
|
return False
|
|
|
|
user = User.query.filter(User.id == user_id).first()
|
2020-01-26 02:28:20 +01:00
|
|
|
if user and (user.is_admin() or user == self.order.courier):
|
2019-08-28 03:46:04 +02:00
|
|
|
return True
|
|
|
|
return False
|