From 4308628938e2fb6cacf605e482e207efd0938163 Mon Sep 17 00:00:00 2001 From: chrisjsimpson Date: Sun, 23 Jun 2024 18:22:15 +0100 Subject: [PATCH] Fix #1364 load plan share urls by plan name when uuid fallsback --- subscribie/views.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/subscribie/views.py b/subscribie/views.py index 1652543e..3f262289 100644 --- a/subscribie/views.py +++ b/subscribie/views.py @@ -44,6 +44,10 @@ import pycountry from types import SimpleNamespace from flask_babel import Domain +from urllib.parse import urlparse +from pathlib import PurePosixPath +from urllib.parse import unquote +from sqlalchemy import func log = logging.getLogger(__name__) @@ -390,13 +394,23 @@ def get_translations(): @bp.route("/plan//") def view_plan(uuid, plan_title=None): """ - Note: "plan_name" is not used, and is also - optional. It's just there to make - urls look 'pretty' - when humans share them. + Match on plan uuid, or fallback to plan name. + + Note: "uuid" may refer to an archived plan for backward + compatibility with published links. + See https://github.com/Subscribie/subscribie/issues/1364 """ # fetch plan from db plan = Plan.query.filter_by(uuid=uuid).first() + if plan is None: + # Try to locate plan by title only + url = urlparse(request.url) + request_path = PurePosixPath(url.path).parts + requested_plan_name_slug = unquote(request_path[3]) + plan = Plan.query.filter( + func.lower(Plan.title) == requested_plan_name_slug.lower() + ).first() + if plan is None: return "Plan not found. Visit home" elif plan.archived: