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
|
|
|
|
|
|
|
|
from .database import db
|
2019-09-08 00:41:50 +02:00
|
|
|
from .location import Location
|
2019-08-28 03:46:04 +02:00
|
|
|
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)
|
|
|
|
courrier_id = db.Column(db.Integer, nullable=True)
|
2019-09-05 03:33:29 +02:00
|
|
|
location_id = db.Column(db.Integer, db.ForeignKey("location.id"))
|
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)
|
2019-09-05 03:33:29 +02:00
|
|
|
items = db.relationship("OrderItem", backref="order", lazy="dynamic")
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-10 01:06:11 +02:00
|
|
|
def configure(self, courrier: User, location: Location,
|
|
|
|
starttime: db.DateTime, stoptime: db.DateTime,) -> None:
|
|
|
|
"Configure the Order"
|
|
|
|
# pylint: disable=W0201
|
2019-08-28 03:46:04 +02:00
|
|
|
self.courrier = courrier
|
|
|
|
self.location = location
|
|
|
|
self.starttime = starttime
|
|
|
|
self.stoptime = stoptime
|
|
|
|
|
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
|
|
|
|
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())
|
|
|
|
user["total"] = user.get("total", 0) + item.product.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) +
|
|
|
|
item.product.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
|
|
|
|
user["products"] = user.get("products", []) + [item.product]
|
|
|
|
group[item.get_name()] = user
|
|
|
|
|
|
|
|
return group
|
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def group_by_product(self) -> typing.Dict[str, typing.Any]:
|
2019-09-10 01:06:11 +02:00
|
|
|
"Group items of an Order by product"
|
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:
|
|
|
|
product = group.get(item.product.name, dict())
|
2019-09-05 03:33:29 +02:00
|
|
|
product["count"] = product.get("count", 0) + 1
|
2019-08-28 03:46:04 +02:00
|
|
|
if item.extra:
|
|
|
|
product["extras"] = product.get("extras", []) + [item.extra]
|
|
|
|
group[item.product.name] = product
|
|
|
|
|
|
|
|
return group
|
|
|
|
|
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)
|
|
|
|
if self.courrier_id == user_id or (user and user.is_admin()):
|
|
|
|
return True
|
|
|
|
return False
|