diff --git a/app/models/order.py b/app/models/order.py index 255704a..6e5391c 100644 --- a/app/models/order.py +++ b/app/models/order.py @@ -1,7 +1,9 @@ -"Script for everything Order related in the database" +"""Script for everything Order related in the database""" import typing from collections import defaultdict from datetime import datetime +import secrets +import string from hlds.definitions import location_definitions from utils import first @@ -10,8 +12,13 @@ from .database import db from .user import User +def generate_slug(): + alphabet = string.ascii_letters + string.digits + return ''.join(secrets.choice(alphabet) for i in range(7)) + + class Order(db.Model): - "Class used for configuring the Order model in the database" + """Class used for configuring the Order model in the database""" id = db.Column(db.Integer, primary_key=True) courier_id = db.Column(db.Integer, nullable=True) location_id = db.Column(db.String(64)) @@ -19,6 +26,7 @@ class Order(db.Model): starttime = db.Column(db.DateTime) stoptime = db.Column(db.DateTime) public = db.Column(db.Boolean, default=True) + slug = db.Column(db.String(7), default=generate_slug, unique=True) items = db.relationship("OrderItem", backref="order", lazy="dynamic") @@ -47,7 +55,7 @@ class Order(db.Model): self.location_name = self.location.name def for_user(self, anon=None, user=None) -> typing.List: - "Get the items for a certain user" + """Get the items for a certain user""" return list( filter( (lambda i: i.user == user) @@ -58,7 +66,7 @@ class Order(db.Model): ) def group_by_user(self) -> typing.List[typing.Tuple[str, typing.List]]: - "Group items of an Order by user" + """Group items of an Order by user""" group: typing.Dict[str, typing.List] = {} # pylint: disable=E1133 @@ -78,7 +86,7 @@ class Order(db.Model): ) -> typing.List[ typing.Tuple[str, int, typing.List[typing.Tuple[str, typing.List]]] ]: - "Group items of an Order by dish" + """Group items of an Order by dish""" group: typing.Dict[str, typing.Dict[str, typing.List]] = defaultdict( lambda: defaultdict(list) ) @@ -101,11 +109,11 @@ class Order(db.Model): ) def is_closed(self) -> bool: - "Return whether or not the order is closed" + """Return whether the order is closed""" return self.stoptime and datetime.now() > self.stoptime def can_close(self, user_id: int) -> bool: - "Check if a user can close the Order" + """Check if a user can close the Order""" if self.stoptime and self.stoptime < datetime.now(): return False user = None diff --git a/app/models/orderitem.py b/app/models/orderitem.py index 623d751..fa74bba 100644 --- a/app/models/orderitem.py +++ b/app/models/orderitem.py @@ -10,7 +10,7 @@ from .user import User class OrderItem(db.Model): - "Class used for configuring the OrderItem model in the database" + """Class used for configuring the OrderItem model in the database""" id = db.Column(db.Integer, primary_key=True) order_id = db.Column(db.Integer, db.ForeignKey("order.id"), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey("user.id")) @@ -61,7 +61,7 @@ class OrderItem(db.Model): # pylint: disable=W0613 def can_delete(self, order_id: int, user_id: int, name: str) -> bool: - "Check if a user can delete an item" + """Check if a user can delete an item""" if int(self.order_id) != int(order_id): return False if self.order.is_closed(): diff --git a/app/notification.py b/app/notification.py index fa97045..14ee065 100644 --- a/app/notification.py +++ b/app/notification.py @@ -11,14 +11,14 @@ from models.order import Order def webhook_text(order: Order) -> typing.Optional[str]: - "Function that makes the text for the notification" + """Function that makes the text for the notification""" if order.location_id == "test": return None if order.courier is not None: # pylint: disable=C0301, C0209 return " {3} is going to {1}, order <{0}|here>! Deadline in {2} minutes!".format( - url_for("order_bp.order_from_id", order_id=order.id, _external=True), + url_for("order_bp.order_from_slug", order_slug=order.slug, _external=True), order.location_name, remaining_minutes(order.stoptime), order.courier.username.title(), @@ -28,12 +28,12 @@ def webhook_text(order: Order) -> typing.Optional[str]: return " New order for {}. Deadline in {} minutes. <{}|Open here.>".format( order.location_name, remaining_minutes(order.stoptime), - url_for("order_bp.order_from_id", order_id=order.id, _external=True), + url_for("order_bp.order_from_slug", order_slug=order.slug, _external=True), ) def post_order_to_webhook(order: Order) -> None: - "Function that sends the notification for the order" + """Function that sends the notification for the order""" message = webhook_text(order) if message: webhookthread = WebhookSenderThread(message, app.config["SLACK_WEBHOOK"]) @@ -41,7 +41,7 @@ def post_order_to_webhook(order: Order) -> None: class WebhookSenderThread(Thread): - "Extension of the Thread class, which sends a webhook for the notification" + """Extension of the Thread class, which sends a webhook for the notification""" def __init__(self, message: str, url: str) -> None: super().__init__() @@ -52,7 +52,7 @@ class WebhookSenderThread(Thread): self.slack_webhook() def slack_webhook(self) -> None: - "The webhook for the specified chat platform" + """The webhook for the specified chat platform""" if self.url: requests.post(self.url, json={"text": self.message}) else: @@ -60,7 +60,7 @@ class WebhookSenderThread(Thread): def remaining_minutes(value) -> str: - "Return the remaining minutes until the deadline of and order" + """Return the remaining minutes until the deadline of and order""" delta = value - datetime.now() if delta.total_seconds() < 0: return "0" diff --git a/app/templates/order.html b/app/templates/order.html index ffb267b..cd218ff 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -36,7 +36,7 @@ {% for item in my_items %}