mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-12 04:29:44 +02:00
Remove custom logging / cleanup
This commit is contained in:
parent
3cfa7f8b03
commit
11bed39dd8
4 changed files with 16 additions and 113 deletions
|
@ -27,11 +27,16 @@ function openInNewTab(el, removeAttribute = false){
|
||||||
* Currently only appends target="_blank" to the domain_form object,
|
* Currently only appends target="_blank" to the domain_form object,
|
||||||
* but this can be expanded.
|
* but this can be expanded.
|
||||||
*/
|
*/
|
||||||
(function prepareDjangoAdmin(){
|
(function (){
|
||||||
|
// Keep scope tight and allow for scalability
|
||||||
|
function prepareDjangoAdmin() {
|
||||||
let domainFormElement = document.getElementById("domain_form");
|
let domainFormElement = document.getElementById("domain_form");
|
||||||
let domainSubmitButton = document.getElementById("manageDomainSubmitButton");
|
let domainSubmitButton = document.getElementById("manageDomainSubmitButton");
|
||||||
if(domainSubmitButton && domainFormElement){
|
if(domainSubmitButton && domainFormElement){
|
||||||
domainSubmitButton.addEventListener("mouseover", () => openInNewTab(domainFormElement, true));
|
domainSubmitButton.addEventListener("mouseover", () => openInNewTab(domainFormElement, true));
|
||||||
domainSubmitButton.addEventListener("mouseout", () => openInNewTab(domainFormElement, false));
|
domainSubmitButton.addEventListener("mouseout", () => openInNewTab(domainFormElement, false));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
prepareDjangoAdmin();
|
||||||
})();
|
})();
|
|
@ -80,15 +80,6 @@ class DomainOrgNameAddressView(DomainPermissionView, FormMixin):
|
||||||
self.request, "The organization name and mailing address has been updated."
|
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
|
# superclass has the redirect
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
@ -132,12 +123,7 @@ class DomainAuthorizingOfficialView(DomainPermissionView, FormMixin):
|
||||||
self.request, "The authorizing official for this domain has been updated."
|
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
|
# superclass has the redirect
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
@ -206,13 +192,6 @@ class DomainNameserversView(DomainPermissionView, FormMixin):
|
||||||
self.request, "The name servers for this domain have been updated."
|
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
|
# superclass has the redirect
|
||||||
return super().form_valid(formset)
|
return super().form_valid(formset)
|
||||||
|
|
||||||
|
@ -254,13 +233,6 @@ class DomainYourContactInformationView(DomainPermissionView, FormMixin):
|
||||||
self.request, "Your contact information for this domain has been updated."
|
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
|
# superclass has the redirect
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
@ -307,13 +279,6 @@ class DomainSecurityEmailView(DomainPermissionView, FormMixin):
|
||||||
self.request, "The security email for this domain have been updated."
|
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
|
# superclass has the redirect
|
||||||
return redirect(self.get_success_url())
|
return redirect(self.get_success_url())
|
||||||
|
|
||||||
|
@ -389,11 +354,7 @@ class DomainAddUserView(DomainPermissionView, FormMixin):
|
||||||
messages.success(
|
messages.success(
|
||||||
self.request, f"Invited {email_address} to this domain."
|
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())
|
return redirect(self.get_success_url())
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
|
@ -416,12 +377,6 @@ class DomainAddUserView(DomainPermissionView, FormMixin):
|
||||||
|
|
||||||
messages.success(self.request, f"Added user {requested_email}.")
|
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())
|
return redirect(self.get_success_url())
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ from registrar.models import (
|
||||||
)
|
)
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,12 +45,10 @@ class DomainPermission(PermissionsLoginMixin):
|
||||||
if pk is None:
|
if pk is None:
|
||||||
raise ValueError("Primary key is None")
|
raise ValueError("Primary key is None")
|
||||||
|
|
||||||
# ticket 806
|
|
||||||
if self.can_access_other_user_domains(pk):
|
if self.can_access_other_user_domains(pk):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# user needs to have a role on the domain,
|
# user needs to have a role on the domain
|
||||||
# and user cannot be restricted
|
|
||||||
if not UserDomainRole.objects.filter(
|
if not UserDomainRole.objects.filter(
|
||||||
user=self.request.user, domain__id=pk
|
user=self.request.user, domain__id=pk
|
||||||
).exists():
|
).exists():
|
||||||
|
@ -67,7 +66,6 @@ class DomainPermission(PermissionsLoginMixin):
|
||||||
user_is_analyst_or_superuser = (
|
user_is_analyst_or_superuser = (
|
||||||
self.request.user.is_staff or self.request.user.is_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:
|
if not user_is_analyst_or_superuser:
|
||||||
return False
|
return False
|
||||||
|
@ -82,7 +80,7 @@ class DomainPermission(PermissionsLoginMixin):
|
||||||
and "analyst_action_location" in session
|
and "analyst_action_location" in session
|
||||||
and session["analyst_action_location"] == pk
|
and session["analyst_action_location"] == pk
|
||||||
)
|
)
|
||||||
logger.debug(f"can do {can_do_action}")
|
|
||||||
if not can_do_action:
|
if not can_do_action:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -47,61 +47,6 @@ class DomainPermissionView(DomainPermission, DetailView, abc.ABC):
|
||||||
|
|
||||||
return context
|
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.
|
# Abstract property enforces NotImplementedError on an attribute.
|
||||||
@property
|
@property
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue