haldis/app/models/orderitem.py

76 lines
2.9 KiB
Python
Raw Normal View History

2019-09-10 01:06:11 +02:00
"Script for everything OrderItem related in the database"
from datetime import datetime
from utils import first
from hlds.definitions import location_definitions
from .database import db
2019-09-08 00:41:50 +02:00
from .order import Order
from .user import User
class OrderItem(db.Model):
2019-09-10 01:06:11 +02:00
"Class used for configuring the OrderItem model in the database"
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))
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)
price = db.Column(db.Integer, nullable=True)
2019-09-05 03:33:29 +02:00
paid = db.Column(
db.Boolean, default=False, nullable=True
2020-01-26 02:39:58 +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)
choices = db.relationship("OrderItemChoice", backref="order_item", lazy="dynamic")
def __getattr__(self, name):
if name == "dish":
location_id = Order.query.filter(Order.id == self.order_id).first().location_id
location = first(filter(lambda l: l.id == location_id, location_definitions))
if location:
return first(filter(lambda d: d.id == self.dish_id, location.dishes))
else:
raise ValueError("No Location found with id: " + location_id)
raise AttributeError()
2019-09-08 00:41:50 +02:00
def get_name(self) -> str:
2019-09-10 01:06:11 +02:00
"Get the name of the user which 'owns' the item"
if self.user_id is not None and self.user_id > 0:
return self.user.username
return self.user_name
2019-09-08 00:41:50 +02:00
def __repr__(self) -> str:
2019-09-05 03:33:29 +02:00
return "Order %d: %s wants %s" % (
self.order_id or 0,
self.get_name(),
self.dish_name or "None",
2019-09-05 03:33:29 +02: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:
2019-09-10 01:06:11 +02:00
"Check if a user can delete an item"
if int(self.order_id) != int(order_id):
return False
2020-01-27 03:52:29 +01:00
if self.order.is_closed():
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()
if user and (user.is_admin() or user == self.order.courier):
return True
return False