From ff0ea068def1abd2e25cb39db9720c2d8359a2a2 Mon Sep 17 00:00:00 2001 From: Midgard Date: Wed, 25 May 2022 10:51:06 +0200 Subject: [PATCH] Don't crash on orders that don't have a slug Orders created before we introduced slugs don't have a slug. This commit introduces code to work with them. Without these changes, the legacy orders are not reachable any more, and trying to create a link for them, crashes the page. I wrote this commit because in my test environment I had a long-lived order for testing purposes, and the home page crashed because the order would show up in the list of Open Orders. --- app/models/order.py | 16 ++++++++++++++++ app/notification.py | 4 ++-- app/templates/order.html | 22 ++++++++++----------- app/templates/order_prices.html | 6 +++--- app/templates/utils.html | 2 +- app/views/order.py | 34 +++++++++++++++++---------------- 6 files changed, 51 insertions(+), 33 deletions(-) diff --git a/app/models/order.py b/app/models/order.py index 6e5391c..b7fe624 100644 --- a/app/models/order.py +++ b/app/models/order.py @@ -132,3 +132,19 @@ class Order(db.Model): def can_modify_payment(self, user_id: int) -> bool: user = User.query.filter_by(id=user_id).first() return user and (user.is_admin() or user == self.courier) + + @staticmethod + def get_by_slug(slug: str) -> "typing.Optional[Order]": + """ + Find an order by slug. Also matches orders by ID if they don't have a slug + """ + order_id = None + try: + order_id = int(slug) + except: + pass + + return Order.query.filter( + (Order.slug == slug) | + ((Order.slug == None) & (Order.id == order_id)) + ).first() diff --git a/app/notification.py b/app/notification.py index 5569f2f..d6cf1cb 100644 --- a/app/notification.py +++ b/app/notification.py @@ -18,7 +18,7 @@ def webhook_text(order: Order) -> typing.Optional[str]: 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_slug", order_slug=order.slug, _external=True), + url_for("order_bp.order_from_slug", order_slug=order.slug or order.id, _external=True), order.location_name, remaining_minutes(order.stoptime), order.courier.username.title(), @@ -28,7 +28,7 @@ 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_slug", order_slug=order.slug, _external=True), + url_for("order_bp.order_from_slug", order_slug=order.slug or order.id, _external=True), ) diff --git a/app/templates/order.html b/app/templates/order.html index 0ad5611..5d9965a 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -23,7 +23,7 @@