From 517b0337f3c917e82edc933ac9e98f07d98fb935 Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Mon, 29 Apr 2024 10:18:07 +0200 Subject: [PATCH] [#2404] Update middleware for optional CMS app URLs --- src/open_inwoner/utils/middleware.py | 31 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/open_inwoner/utils/middleware.py b/src/open_inwoner/utils/middleware.py index 05ca1ba41a..3e4d42ccad 100644 --- a/src/open_inwoner/utils/middleware.py +++ b/src/open_inwoner/utils/middleware.py @@ -31,6 +31,27 @@ def get_always_pass_prefixes() -> tuple[str, ...]: ) +def get_app_pass_prefixes() -> tuple[str, ...]: + """ + Urls for apps that may not be active + """ + return ( + "profile:registration_necessary", + "profile:email_verification_user", + ) + + +def resolve_urls(urls) -> tuple[str, ...]: + ret = list() + for url in urls: + try: + ret.append(resolve_url(url)) + except NoReverseMatch: + # support testing without cms app mounted + pass + return tuple(ret) + + class BaseConditionalUserRedirectMiddleware(abc.ABC): """ Base class for middleware that redirects users to another view based on conditions @@ -64,6 +85,7 @@ def get_pass_prefixes(self, request) -> tuple[str, ...]: URL prefixes we're skipping without checking requires_redirect() """ prefixes = get_always_pass_prefixes() + prefixes += resolve_urls(get_app_pass_prefixes()) prefixes += (resolve_url(self.get_redirect_url(request)),) prefixes += self.get_extra_pass_prefixes() return prefixes @@ -71,14 +93,7 @@ def get_pass_prefixes(self, request) -> tuple[str, ...]: def get_extra_pass_prefixes(self) -> tuple[str, ...]: prefixes = [] if self.extra_pass_prefixes: - for e in self.extra_pass_prefixes: - try: - prefixes.append(resolve_url(e)) - prefixes.append(reverse("profile:registration_necessary")) - prefixes.append(reverse("profile:email_verification_user")) - except NoReverseMatch: - # support testing without cms app mounted - pass + return resolve_urls(self.extra_pass_prefixes) return tuple(prefixes) def __call__(self, request):