cleaned up code as well as comments

This commit is contained in:
David Kennedy 2024-05-15 17:10:08 -04:00
parent aeef0b76e1
commit fef606c1dc
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
6 changed files with 22 additions and 22 deletions

View file

@ -27,6 +27,8 @@ class UserProfileForm(forms.ModelForm):
required = ["first_name", "last_name", "title", "email", "phone"]
def __init__(self, *args, **kwargs):
"""Override the inerited __init__ method to update the fields."""
super().__init__(*args, **kwargs)
# take off maxlength attribute for the phone number field
# which interferes with out input_with_errors template tag
@ -51,8 +53,8 @@ class UserProfileForm(forms.ModelForm):
self.fields["email"].error_messages = {
"required": "Enter your email address in the required format, like name@example.com."
}
# self.fields["email"].widget.attrs["readonly"] = "readonly"
self.fields["phone"].error_messages["required"] = "Enter your phone number."
self.domainInfo = None
DomainHelper.disable_field(self.fields["email"], disable_required=True)

View file

@ -74,6 +74,7 @@
</li>
{% if not has_profile_feature_flag %}
{% comment %}Conditionally display profile link in main nav{% endcomment %}
<li class="usa-sidenav__item">
{% url 'domain-your-contact-information' pk=domain.id as url %}
<a href="{{ url }}"

View file

@ -103,7 +103,7 @@ class DomainBaseView(DomainPermissionView):
self.session[domain_pk] = self.object
def get_context_data(self, **kwargs):
"""Adjust context from FormMixin for formsets."""
"""Extend get_context_data to add has_profile_feature_flag to context"""
context = super().get_context_data(**kwargs)
# This is a django waffle flag which toggles features based off of the "flag" table
context["has_profile_feature_flag"] = flag_is_active(self.request, "profile_feature")
@ -597,10 +597,10 @@ class DomainYourContactInformationView(DomainFormBaseView):
return super().form_valid(form)
def has_permission(self):
"""Check if this user has access to this domain.
"""Check if this user has permission to see this view.
The user is in self.request.user and the domain needs to be looked
up from the domain's primary key in self.kwargs["pk"]
In addition to permissions for the user, check that the profile_feature waffle flag
is not turned on.
"""
if flag_is_active(self.request, "profile_feature"):
return False

View file

@ -699,7 +699,7 @@ class DomainRequestStatus(DomainRequestPermissionView):
template_name = "domain_request_status.html"
def get_context_data(self, **kwargs):
"""Adjust context from FormMixin for formsets."""
"""Extend get_context_data to add has_profile_feature_flag to context"""
context = super().get_context_data(**kwargs)
# This is a django waffle flag which toggles features based off of the "flag" table
context["has_profile_feature_flag"] = flag_is_active(self.request, "profile_feature")
@ -716,7 +716,7 @@ class DomainRequestWithdrawConfirmation(DomainRequestPermissionWithdrawView):
template_name = "domain_request_withdraw_confirmation.html"
def get_context_data(self, **kwargs):
"""Adjust context from FormMixin for formsets."""
"""Extend get_context_data to add has_profile_feature_flag to context"""
context = super().get_context_data(**kwargs)
# This is a django waffle flag which toggles features based off of the "flag" table
context["has_profile_feature_flag"] = flag_is_active(self.request, "profile_feature")

View file

@ -20,9 +20,7 @@ logger = logging.getLogger(__name__)
class UserProfileView(UserProfilePermissionView, FormMixin):
"""
Base View for the Domain. Handles getting and setting the domain
in session cache on GETs. Also provides methods for getting
and setting the domain in cache
Base View for the User Profile. Handles getting and setting the User Profile
"""
model = Contact
@ -30,25 +28,27 @@ class UserProfileView(UserProfilePermissionView, FormMixin):
form_class = UserProfileForm
def get(self, request, *args, **kwargs):
logger.info("in get()")
"""Handle get requests by getting user's contact object and setting object
and form to context before rendering."""
self.object = self.get_object()
form = self.form_class(instance=self.object)
context = self.get_context_data(object=self.object, form=form)
logger.info(context)
return self.render_to_response(context)
def get_context_data(self, **kwargs):
"""Adjust context from FormMixin for formsets."""
"""Extend get_context_data to include has_profile_feature_flag"""
self.get()
context = super().get_context_data(**kwargs)
# This is a django waffle flag which toggles features based off of the "flag" table
context["has_profile_feature_flag"] = flag_is_active(self.request, "profile_feature")
return context
def get_success_url(self):
"""Redirect to the overview page for the domain."""
"""Redirect to the user's profile page."""
return reverse("user-profile")
def post(self, request, *args, **kwargs):
"""Handle post requests (form submissions)"""
self.object = self.get_object()
form = self.form_class(request.POST, instance=self.object)
@ -58,6 +58,7 @@ class UserProfileView(UserProfilePermissionView, FormMixin):
return self.form_invalid(form)
def form_valid(self, form):
"""Handle successful and valid form submissions."""
form.save()
messages.success(self.request, "Your profile has been updated.")
@ -65,11 +66,9 @@ class UserProfileView(UserProfilePermissionView, FormMixin):
# superclass has the redirect
return super().form_valid(form)
# Override get_object to return the logged-in user
def get_object(self, queryset=None):
logger.info("in get_object")
"""Override get_object to return the logged-in user's contact"""
user = self.request.user # get the logged in user
if hasattr(user, 'contact'): # Check if the user has a contact instance
logger.info(user.contact)
return user.contact
return None

View file

@ -154,11 +154,9 @@ class UserProfilePermissionView(UserProfilePermission, DetailView, abc.ABC):
`template_name`.
"""
# # DetailView property for what model this is viewing
# model = get_user_model()
# # variable name in template context for the model object
# context_object_name = "user"
# DetailView property for what model this is viewing
model = Contact
# variable name in template context for the model object
context_object_name = "contact"
# Abstract property enforces NotImplementedError on an attribute.