Remove custom logging / cleanup

This commit is contained in:
zandercymatics 2023-09-01 14:16:44 -06:00
parent 3cfa7f8b03
commit 11bed39dd8
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 16 additions and 113 deletions

View file

@ -27,11 +27,16 @@ function openInNewTab(el, removeAttribute = false){
* Currently only appends target="_blank" to the domain_form object,
* but this can be expanded.
*/
(function prepareDjangoAdmin(){
let domainFormElement = document.getElementById("domain_form");
let domainSubmitButton = document.getElementById("manageDomainSubmitButton");
if(domainSubmitButton && domainFormElement){
domainSubmitButton.addEventListener("mouseover", () => openInNewTab(domainFormElement, true));
domainSubmitButton.addEventListener("mouseout", () => openInNewTab(domainFormElement, false));
(function (){
// Keep scope tight and allow for scalability
function prepareDjangoAdmin() {
let domainFormElement = document.getElementById("domain_form");
let domainSubmitButton = document.getElementById("manageDomainSubmitButton");
if(domainSubmitButton && domainFormElement){
domainSubmitButton.addEventListener("mouseover", () => openInNewTab(domainFormElement, true));
domainSubmitButton.addEventListener("mouseout", () => openInNewTab(domainFormElement, false));
}
}
prepareDjangoAdmin();
})();

View file

@ -80,15 +80,6 @@ class DomainOrgNameAddressView(DomainPermissionView, FormMixin):
self.request, "The organization name and mailing address has been updated."
)
# Q: Is there a more efficent way to do this?
# It would be ideal if we didn't have to repeat this.
if self.request.user.is_staff or self.request.user.is_superuser:
# if they are editing from an '/admin' redirect, log their actions
changes = {field: form.cleaned_data[field] for field in form.changed_data}
self.log_analyst_form_actions(
self.form_class.__name__, self.get_object().domain_info, changes, obj=self.get_object()
)
# superclass has the redirect
return super().form_valid(form)
@ -132,12 +123,7 @@ class DomainAuthorizingOfficialView(DomainPermissionView, FormMixin):
self.request, "The authorizing official for this domain has been updated."
)
if self.request.user.is_staff or self.request.user.is_superuser:
# if they are editing from an '/admin' redirect, log their actions
changes = {field: form.cleaned_data[field] for field in form.changed_data}
self.log_analyst_form_actions(
self.form_class.__name__, self.get_object().domain_info, changes, obj=self.get_object()
)
# superclass has the redirect
return super().form_valid(form)
@ -206,13 +192,6 @@ class DomainNameserversView(DomainPermissionView, FormMixin):
self.request, "The name servers for this domain have been updated."
)
if self.request.user.is_staff or self.request.user.is_superuser:
# if they are editing from an '/admin' redirect, log their actions
changes = {field: formset.cleaned_data[field] for field in formset.changed_data}
self.log_analyst_form_actions(
self.form_class.__name__, self.get_object().domain_info, changes, obj=self.get_object()
)
# superclass has the redirect
return super().form_valid(formset)
@ -254,13 +233,6 @@ class DomainYourContactInformationView(DomainPermissionView, FormMixin):
self.request, "Your contact information for this domain has been updated."
)
if self.request.user.is_staff or self.request.user.is_superuser:
# if they are editing from an '/admin' redirect, log their actions
changes = {field: form.cleaned_data[field] for field in form.changed_data}
self.log_analyst_form_actions(
self.form_class.__name__, self.get_object().domain_info, changes, obj=self.get_object()
)
# superclass has the redirect
return super().form_valid(form)
@ -307,13 +279,6 @@ class DomainSecurityEmailView(DomainPermissionView, FormMixin):
self.request, "The security email for this domain have been updated."
)
if self.request.user.is_staff or self.request.user.is_superuser:
# if they are editing from an '/admin' redirect, log their actions
changes = {field: form.cleaned_data[field] for field in form.changed_data}
self.log_analyst_form_actions(
self.form_class.__name__, self.get_object().domain_info, changes, obj=self.get_object()
)
# superclass has the redirect
return redirect(self.get_success_url())
@ -389,11 +354,7 @@ class DomainAddUserView(DomainPermissionView, FormMixin):
messages.success(
self.request, f"Invited {email_address} to this domain."
)
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, obj=self.get_object()
)
return redirect(self.get_success_url())
def form_valid(self, form):
@ -416,12 +377,6 @@ class DomainAddUserView(DomainPermissionView, FormMixin):
messages.success(self.request, f"Added user {requested_email}.")
if self.request.user.is_staff or self.request.user.is_superuser:
# if they are editing from an '/admin' redirect, log their actions
changes = {field: form.cleaned_data[field] for field in form.changed_data}
self.log_analyst_form_actions(
self.form_class.__name__, self.get_object().domain_info, changes, obj=self.get_object()
)
return redirect(self.get_success_url())

View file

@ -10,6 +10,7 @@ from registrar.models import (
)
import logging
logger = logging.getLogger(__name__)
@ -44,12 +45,10 @@ class DomainPermission(PermissionsLoginMixin):
if pk is None:
raise ValueError("Primary key is None")
# ticket 806
if self.can_access_other_user_domains(pk):
return True
# user needs to have a role on the domain,
# and user cannot be restricted
# user needs to have a role on the domain
if not UserDomainRole.objects.filter(
user=self.request.user, domain__id=pk
).exists():
@ -67,7 +66,6 @@ class DomainPermission(PermissionsLoginMixin):
user_is_analyst_or_superuser = (
self.request.user.is_staff or self.request.user.is_superuser
)
logger.debug(f"is auth {user_is_analyst_or_superuser}")
if not user_is_analyst_or_superuser:
return False
@ -82,7 +80,7 @@ class DomainPermission(PermissionsLoginMixin):
and "analyst_action_location" in session
and session["analyst_action_location"] == pk
)
logger.debug(f"can do {can_do_action}")
if not can_do_action:
return False

View file

@ -47,61 +47,6 @@ class DomainPermissionView(DomainPermission, DetailView, abc.ABC):
return context
def log_analyst_form_actions(
self, form_class_name, printable_object_info, changes=None, obj=None
):
"""Generates a log for when key 'analyst_action' exists on the session.
Follows this format:
for field, new_value in changes.items():
"{user_type} '{self.request.user}'
set field '{field}': '{new_value}'
under class '{form_class_name}'
in domain '{printable_object_info}'"
"""
if "analyst_action" in self.request.session:
action = self.request.session["analyst_action"]
user_type = "Analyst"
if self.request.user.is_superuser:
user_type = "Superuser"
# Template for potential future expansion,
# in the event we want more logging granularity.
# Could include things such as 'view'
# or 'copy', for instance.
match action:
case "edit":
if obj is not None:
content_type = ContentType.objects.get_for_model(obj)
LogEntry.objects.log_action(
user_id=self.request.user.id,
content_type_id=content_type.pk,
object_id=obj.id,
object_repr=str(obj),
action_flag=CHANGE,
)
if changes is not None:
# Logs every change made to the domain field.
# noqa for readability/format.
# Used to manually capture changes, if need be.
for field, new_value in changes.items():
logger.info(
f"""
An analyst or superuser made alterations to a domain:
{user_type} '{self.request.user}'
set field '{field}': '{new_value}'
under class '{form_class_name}'
in domain '{printable_object_info}'
""" # noqa
)
else:
# 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
)
# Abstract property enforces NotImplementedError on an attribute.
@property
@abc.abstractmethod