This commit is contained in:
David Kennedy 2024-06-19 07:30:09 -04:00
parent 79d017127a
commit c020007d51
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B

View file

@ -52,6 +52,30 @@ class CheckUserProfileMiddleware:
"/admin", "/admin",
] ]
def __init__(self):
self.setup_page = reverse("finish-user-profile-setup")
self.profile_page = reverse("user-profile")
self.logout_page = reverse("logout")
self.regular_excluded_pages = [
self.setup_page,
self.logout_page,
"/admin",
]
self.other_excluded_pages = [
self.profile_page,
self.logout_page,
"/admin",
]
self.excluded_pages = {
self.setup_page: self.regular_excluded_pages,
self.profile_page: self.other_excluded_pages
}
def _get_excluded_pages(self, page):
return self.excluded_pages.get(page, [])
def __call__(self, request): def __call__(self, request):
response = self.get_response(request) response = self.get_response(request)
return response return response
@ -68,16 +92,16 @@ class CheckUserProfileMiddleware:
return None return None
if request.user.is_authenticated: if request.user.is_authenticated:
if hasattr(request.user, "finished_setup") and not request.user.finished_setup: profile_page = self.profile_page
if request.user.verification_type == User.VerificationTypeChoices.REGULAR: if request.user.verification_type == User.VerificationTypeChoices.REGULAR:
return self._handle_regular_user_setup_not_finished(request) profile_page = self.setup_page
else: if hasattr(request.user, "finished_setup") and not request.user.finished_setup:
return self._handle_other_user_setup_not_finished(request) return self._handle_user_setup_not_finished(request, profile_page)
# Continue processing the view # Continue processing the view
return None return None
def _handle_regular_user_setup_not_finished(self, request): def _handle_user_setup_not_finished(self, request, profile_page):
"""Redirects the given user to the finish setup page. """Redirects the given user to the finish setup page.
We set the "redirect" query param equal to where the user wants to go. We set the "redirect" query param equal to where the user wants to go.
@ -93,7 +117,8 @@ class CheckUserProfileMiddleware:
custom_redirect = "domain-request:" if request.path == "/request/" else None custom_redirect = "domain-request:" if request.path == "/request/" else None
# Don't redirect on excluded pages (such as the setup page itself) # Don't redirect on excluded pages (such as the setup page itself)
if not any(request.path.startswith(page) for page in self.regular_excluded_pages): #if not any(request.path.startswith(page) for page in excluded_pages):
if not any(request.path.startswith(page) for page in self._get_excluded_pages(profile_page)):
# Preserve the original query parameters, and coerce them into a dict # Preserve the original query parameters, and coerce them into a dict
query_params = parse_qs(request.META["QUERY_STRING"]) query_params = parse_qs(request.META["QUERY_STRING"])
@ -103,19 +128,9 @@ class CheckUserProfileMiddleware:
query_params["redirect"] = custom_redirect query_params["redirect"] = custom_redirect
# Add our new query param, while preserving old ones # Add our new query param, while preserving old ones
new_setup_page = replace_url_queryparams(self.setup_page, query_params) if query_params else self.setup_page new_setup_page = replace_url_queryparams(profile_page, query_params) if query_params else profile_page
return HttpResponseRedirect(new_setup_page) return HttpResponseRedirect(new_setup_page)
else: else:
# Process the view as normal # Process the view as normal
return None return None
def _handle_other_user_setup_not_finished(self, request):
"""Redirects the given user to the profile page to finish setup."""
# Don't redirect on excluded pages (such as the setup page itself)
if not any(request.path.startswith(page) for page in self.other_excluded_pages):
return HttpResponseRedirect(self.profile_page)
else:
# Process the view as normal
return None