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
|
|
|
|
|
|
|
|
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):
|
2019-09-10 01:06:11 +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))
|
|
|
|
dish_id = db.Column(db.String(120), nullable=True)
|
|
|
|
dish_name = db.Column(db.String(120), nullable=True)
|
|
|
|
price = db.Column(db.Integer, nullable=False)
|
2019-09-05 03:33:29 +02:00
|
|
|
paid = db.Column(
|
2020-01-26 02:39:58 +01:00
|
|
|
db.Boolean, default=False, nullable=False
|
|
|
|
)
|
|
|
|
comment = db.Column(db.String(254), nullable=True)
|
|
|
|
hlds_data_version = db.Column(db.String(40), nullable=True)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2020-01-26 02:52:45 +01:00
|
|
|
def configure(self, user: User, order: Order) -> None:
|
2019-09-10 01:06:11 +02:00
|
|
|
"Configure the OrderItem"
|
|
|
|
# pylint: disable=W0201
|
2019-08-28 03:46:04 +02:00
|
|
|
self.user = user
|
|
|
|
self.order = order
|
|
|
|
|
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"
|
2019-08-28 03:46:04 +02:00
|
|
|
if self.user_id is not None and self.user_id > 0:
|
|
|
|
return self.user.username
|
|
|
|
return self.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(),
|
2020-01-26 02:52:45 +01:00
|
|
|
self.dish_name or "None",
|
2019-09-05 03:33:29 +02:00
|
|
|
)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
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"
|
2019-08-28 03:46:04 +02:00
|
|
|
if int(self.order_id) != int(order_id):
|
|
|
|
return False
|
|
|
|
if self.order.stoptime and self.order.stoptime < datetime.now():
|
|
|
|
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
|