mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-25 10:33:50 +02:00
Add link
This commit is contained in:
parent
bf1d5fad47
commit
a5a0c136df
4 changed files with 68 additions and 11 deletions
|
@ -3,6 +3,7 @@
|
|||
"""
|
||||
|
||||
import logging
|
||||
from urllib.parse import parse_qs, unquote, urlencode
|
||||
|
||||
from django.contrib import messages
|
||||
from django.views.generic.edit import FormMixin
|
||||
|
@ -11,6 +12,7 @@ from django.urls import reverse
|
|||
from registrar.models import (
|
||||
Contact,
|
||||
)
|
||||
from registrar.models.utility.generic_helper import replace_url_queryparams
|
||||
from registrar.views.utility.permission_views import UserProfilePermissionView
|
||||
from waffle.decorators import flag_is_active, waffle_flag
|
||||
|
||||
|
@ -30,13 +32,26 @@ class UserProfileView(UserProfilePermissionView, FormMixin):
|
|||
def get(self, request, *args, **kwargs):
|
||||
"""Handle get requests by getting user's contact object and setting object
|
||||
and form to context before rendering."""
|
||||
self.object = self.get_object()
|
||||
self._refresh_session_and_object(request)
|
||||
form = self.form_class(instance=self.object)
|
||||
context = self.get_context_data(object=self.object, form=form)
|
||||
|
||||
return_to_request = request.GET.get("return_to_request")
|
||||
if return_to_request:
|
||||
context["return_to_request"] = True
|
||||
|
||||
return self.render_to_response(context)
|
||||
|
||||
def _refresh_session_and_object(self, request):
|
||||
"""Sets the current session to self.session and the current object to self.object"""
|
||||
self.session = request.session
|
||||
self.object = self.get_object()
|
||||
|
||||
@waffle_flag("profile_feature") # type: ignore
|
||||
def dispatch(self, request, *args, **kwargs): # type: ignore
|
||||
# Store the original queryparams to persist them
|
||||
query_params = request.META["QUERY_STRING"]
|
||||
request.session["query_params"] = query_params
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
|
@ -48,11 +63,20 @@ class UserProfileView(UserProfilePermissionView, FormMixin):
|
|||
|
||||
def get_success_url(self):
|
||||
"""Redirect to the user's profile page."""
|
||||
return reverse("user-profile")
|
||||
|
||||
query_params = {}
|
||||
if "query_params" in self.session:
|
||||
params = unquote(self.session["query_params"])
|
||||
query_params = parse_qs(params)
|
||||
|
||||
# Preserve queryparams and add them back to the url
|
||||
base_url = reverse("user-profile")
|
||||
new_redirect = replace_url_queryparams(base_url, query_params) if query_params else base_url
|
||||
return new_redirect
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
"""Handle post requests (form submissions)"""
|
||||
self.object = self.get_object()
|
||||
self._refresh_session_and_object(request)
|
||||
form = self.form_class(request.POST, instance=self.object)
|
||||
|
||||
if form.is_valid():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue