From 65384710910664433fb4942ffd121ee1ba876951 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Tue, 22 Aug 2023 12:58:37 -0600 Subject: [PATCH] Black and lint changes --- src/registrar/admin.py | 13 +++---- src/registrar/views/domain.py | 29 +++++++++++---- src/registrar/views/utility/mixins.py | 36 ++++++++++++++----- .../views/utility/permission_views.py | 31 +++++++++------- 4 files changed, 75 insertions(+), 34 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 98d1cc553..4ea37e6e8 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -161,20 +161,21 @@ class DomainAdmin(ListHeaderAdmin): return HttpResponseRedirect(".") elif EDIT_DOMAIN in request.POST: # We want to know, globally, when an edit action occurs - request.session['analyst_action'] = 'edit' + request.session["analyst_action"] = "edit" # Restricts this action to this domain only - request.session['analyst_action_location'] = obj.id + request.session["analyst_action_location"] = obj.id - return HttpResponseRedirect(reverse('domain', args=(obj.id,))) + return HttpResponseRedirect(reverse("domain", args=(obj.id,))) return super().response_change(request, obj) + # Sets domain_id as a context var def change_view(self, request, object_id, form_url="", extra_context=None): - if 'analyst_action' in request.session: + if "analyst_action" in request.session: # If an analyst performed an edit action, # delete the session variable - del request.session['analyst_action'] + del request.session["analyst_action"] # delete the associated location - del request.session['analyst_action_location'] + del request.session["analyst_action_location"] extra_context = extra_context or {} extra_context["domain_id"] = object_id diff --git a/src/registrar/views/domain.py b/src/registrar/views/domain.py index 5eee3e41b..6f649661b 100644 --- a/src/registrar/views/domain.py +++ b/src/registrar/views/domain.py @@ -38,6 +38,7 @@ logger = logging.getLogger(__name__) class DomainView(DomainPermissionView): """Domain detail overview page.""" + template_name = "domain_detail.html" @@ -81,7 +82,9 @@ class DomainOrgNameAddressView(DomainPermissionView, FormMixin): if self.request.user.is_staff or self.request.user.is_superuser: # if they are editing from an '/admin' redirect, log their actions - self.log_analyst_form_actions(self.form_class.__name__, self.get_object().domain_info) + self.log_analyst_form_actions( + self.form_class.__name__, self.get_object().domain_info + ) # superclass has the redirect return super().form_valid(form) @@ -128,7 +131,9 @@ class DomainAuthorizingOfficialView(DomainPermissionView, FormMixin): if self.request.user.is_staff or self.request.user.is_superuser: # if they are editing from an '/admin' redirect, log their actions - self.log_analyst_form_actions(self.form_class.__name__, self.get_object().domain_info) + self.log_analyst_form_actions( + self.form_class.__name__, self.get_object().domain_info + ) # superclass has the redirect return super().form_valid(form) @@ -199,7 +204,9 @@ class DomainNameserversView(DomainPermissionView, FormMixin): if self.request.user.is_staff or self.request.user.is_superuser: # if they are editing from an '/admin' redirect, log their actions - self.log_analyst_form_actions(self.form_class.__name__, self.get_object().domain_info) + self.log_analyst_form_actions( + self.form_class.__name__, self.get_object().domain_info + ) # superclass has the redirect return super().form_valid(formset) @@ -244,7 +251,9 @@ class DomainYourContactInformationView(DomainPermissionView, FormMixin): if self.request.user.is_staff or self.request.user.is_superuser: # if they are editing from an '/admin' redirect, log their actions - self.log_analyst_form_actions(self.form_class.__name__, self.get_object().domain_info) + self.log_analyst_form_actions( + self.form_class.__name__, self.get_object().domain_info + ) # superclass has the redirect return super().form_valid(form) @@ -294,7 +303,9 @@ class DomainSecurityEmailView(DomainPermissionView, FormMixin): if self.request.user.is_staff or self.request.user.is_superuser: # if they are editing from an '/admin' redirect, log their actions - self.log_analyst_form_actions(self.form_class.__name__, self.get_object().domain_info) + self.log_analyst_form_actions( + self.form_class.__name__, self.get_object().domain_info + ) # superclass has the redirect return redirect(self.get_success_url()) @@ -373,7 +384,9 @@ class DomainAddUserView(DomainPermissionView, FormMixin): ) if self.request.user.is_staff or self.request.user.is_superuser: # if they are editing from an '/admin' redirect, log their actions - self.log_analyst_form_actions(self.form_class.__name__, self.get_object().domain_info) + self.log_analyst_form_actions( + self.form_class.__name__, self.get_object().domain_info + ) return redirect(self.get_success_url()) def form_valid(self, form): @@ -398,7 +411,9 @@ class DomainAddUserView(DomainPermissionView, FormMixin): if self.request.user.is_staff or self.request.user.is_superuser: # if they are editing from an '/admin' redirect, log their actions - self.log_analyst_form_actions(self.form_class.__name__, self.get_object().domain_info) + self.log_analyst_form_actions( + self.form_class.__name__, self.get_object().domain_info + ) return redirect(self.get_success_url()) diff --git a/src/registrar/views/utility/mixins.py b/src/registrar/views/utility/mixins.py index 33d70ae32..7644ef7cf 100644 --- a/src/registrar/views/utility/mixins.py +++ b/src/registrar/views/utility/mixins.py @@ -7,8 +7,10 @@ from registrar.models import DomainApplication, DomainInvitation import logging from registrar.models.domain_information import DomainInformation + logger = logging.getLogger(__name__) + class PermissionsLoginMixin(PermissionRequiredMixin): """Mixin that redirects to login page if not logged in, otherwise 403.""" @@ -39,9 +41,7 @@ class DomainPermission(PermissionsLoginMixin): requested_domain = None try: - requested_domain = DomainInformation.objects.get( - id=pk - ) + requested_domain = DomainInformation.objects.get(id=pk) # This should never happen in normal flow. # That said, it does need to be raised here. @@ -49,7 +49,9 @@ class DomainPermission(PermissionsLoginMixin): raise Http404() # Checks if the creator is the user requesting this item - user_is_creator: bool = requested_domain.creator.username == self.request.user.username + user_is_creator: bool = ( + requested_domain.creator.username == self.request.user.username + ) # user needs to have a role on the domain if user_is_creator: @@ -57,19 +59,35 @@ class DomainPermission(PermissionsLoginMixin): # ticket 806 # Analysts may manage domains, when they are in these statuses: - valid_domain_statuses = [DomainApplication.APPROVED, DomainApplication.IN_REVIEW, DomainApplication.REJECTED, DomainApplication.ACTION_NEEDED] + valid_domain_statuses = [ + DomainApplication.APPROVED, + DomainApplication.IN_REVIEW, + DomainApplication.REJECTED, + DomainApplication.ACTION_NEEDED, + ] # Check if the user is permissioned... - user_is_analyst_or_superuser = self.request.user.is_staff or self.request.user.is_superuser + user_is_analyst_or_superuser = ( + self.request.user.is_staff or self.request.user.is_superuser + ) session = self.request.session # Check if the user is attempting a valid edit action. # If analyst_action is present, analyst_action_location will be present. - # if it isn't, then it either suggests tampering or a larger omnipresent issue with sessions. - can_do_action = 'analyst_action' in session and session['analyst_action_location'] == pk + # if it isn't, then it either suggests tampering + # or a larger omnipresent issue with sessions. + can_do_action = ( + "analyst_action" in session and session["analyst_action_location"] == pk + ) - if can_do_action and user_is_analyst_or_superuser and requested_domain.domain_application.status in valid_domain_statuses: + # If the valid session keys exist, if the user is permissioned, + # and if its in a valid status + if ( + can_do_action + and user_is_analyst_or_superuser + and requested_domain.domain_application.status in valid_domain_statuses + ): return True # ticket 796 diff --git a/src/registrar/views/utility/permission_views.py b/src/registrar/views/utility/permission_views.py index faabc171d..6a991d016 100644 --- a/src/registrar/views/utility/permission_views.py +++ b/src/registrar/views/utility/permission_views.py @@ -13,7 +13,10 @@ from .mixins import ( DomainInvitationPermission, ) import logging + logger = logging.getLogger(__name__) + + class DomainPermissionView(DomainPermission, DetailView, abc.ABC): """Abstract base view for domains that enforces permissions. @@ -37,22 +40,22 @@ class DomainPermissionView(DomainPermission, DetailView, abc.ABC): is_original_creator = DomainInformation.objects.filter( creator_id=self.request.user.id, id=self.kwargs["pk"] ).exists() - context['is_original_creator'] = is_original_creator - context['is_analyst_or_superuser'] = user.is_superuser or user.is_staff + context["is_original_creator"] = is_original_creator + context["is_analyst_or_superuser"] = user.is_superuser or user.is_staff # Flag to see if an analyst is attempting to make edits - if 'analyst_action' in self.request.session: - context['analyst_action'] = self.request.session['analyst_action'] + if "analyst_action" in self.request.session: + context["analyst_action"] = self.request.session["analyst_action"] return context def log_analyst_form_actions(self, form_class_name, printable_object_info): - """ Generates a log for when 'analyst_action' exists on the session """ - if 'analyst_action' in self.request.session: - action = self.request.session['analyst_action'] + """Generates a log for when 'analyst_action' exists on the session""" + if "analyst_action" in self.request.session: + action = self.request.session["analyst_action"] user_type = "Analyst" - if(self.request.user.is_superuser): + if self.request.user.is_superuser: user_type = "Superuser" # Template for potential future expansion, @@ -60,11 +63,15 @@ class DomainPermissionView(DomainPermission, DetailView, abc.ABC): # Could include things such as 'view' # or 'copy', for instance. match action: - case 'edit': + case "edit": # Q: do we want to be logging on every changed field? - # I could see that becoming spammy log-wise, but it may also be important. - # To do so, I'd likely have to override some of the save() functionality of ModelForm. - logger.info(f"{user_type} {self.request.user} edited {form_class_name} in {printable_object_info}") + # I could see that becoming spammy log-wise, + # but it may also be important. + + # noqa here as breaking this up further leaves it hard to read + logger.info( + f"{user_type} {self.request.user} edited {form_class_name} in {printable_object_info}" # noqa + ) else: logger.debug("'analyst_action' does not exist on the session")