mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-05 10:43:23 +02:00
Code cleanup
This commit is contained in:
parent
c5f27769be
commit
2d1982c252
3 changed files with 57 additions and 14 deletions
|
@ -1,5 +1,5 @@
|
||||||
import logging
|
import logging
|
||||||
import time
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.db.models.functions import Concat, Coalesce
|
from django.db.models.functions import Concat, Coalesce
|
||||||
from django.db.models import Value, CharField
|
from django.db.models import Value, CharField
|
||||||
|
@ -120,7 +120,11 @@ class CustomLogEntryAdmin(LogEntryAdmin):
|
||||||
|
|
||||||
class AdminSortFields:
|
class AdminSortFields:
|
||||||
_name_sort = ["first_name", "last_name", "email"]
|
_name_sort = ["first_name", "last_name", "email"]
|
||||||
# Define a mapping of field names to model querysets and sort expressions
|
|
||||||
|
# Define a mapping of field names to model querysets and sort expressions.
|
||||||
|
# A dictionary is used for specificity, but the downside is some degree of repetition.
|
||||||
|
# To eliminate this, this list can be generated dynamically but the readability of that
|
||||||
|
# is impacted.
|
||||||
sort_mapping = {
|
sort_mapping = {
|
||||||
# == Contact == #
|
# == Contact == #
|
||||||
"other_contacts": (Contact, _name_sort),
|
"other_contacts": (Contact, _name_sort),
|
||||||
|
@ -149,10 +153,12 @@ class AdminSortFields:
|
||||||
if queryset_info is None:
|
if queryset_info is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
# Grab the model we want to order, and grab how we want to order it
|
||||||
model, order_by = queryset_info
|
model, order_by = queryset_info
|
||||||
match db_field.name:
|
match db_field.name:
|
||||||
case "investigator":
|
case "investigator":
|
||||||
# We should only return users who are staff
|
# We should only return users who are staff.
|
||||||
|
# Currently a fallback. Consider removing this if it is not needed.
|
||||||
return model.objects.filter(is_staff=True).order_by(*order_by)
|
return model.objects.filter(is_staff=True).order_by(*order_by)
|
||||||
case _:
|
case _:
|
||||||
# If no case is defined, return the default
|
# If no case is defined, return the default
|
||||||
|
@ -178,9 +184,14 @@ class AuditedAdmin(admin.ModelAdmin):
|
||||||
def formfield_for_manytomany(self, db_field, request, **kwargs):
|
def formfield_for_manytomany(self, db_field, request, **kwargs):
|
||||||
"""customize the behavior of formfields with manytomany relationships. the customized
|
"""customize the behavior of formfields with manytomany relationships. the customized
|
||||||
behavior includes sorting of objects in lists as well as customizing helper text"""
|
behavior includes sorting of objects in lists as well as customizing helper text"""
|
||||||
|
|
||||||
|
# Define a queryset. Note that in the super of this,
|
||||||
|
# a new queryset will only be generated if one does not exist.
|
||||||
|
# Thus, the order in which we define queryset matters.
|
||||||
queryset = AdminSortFields.get_queryset(db_field)
|
queryset = AdminSortFields.get_queryset(db_field)
|
||||||
if queryset:
|
if queryset:
|
||||||
kwargs["queryset"] = queryset
|
kwargs["queryset"] = queryset
|
||||||
|
|
||||||
formfield = super().formfield_for_manytomany(db_field, request, **kwargs)
|
formfield = super().formfield_for_manytomany(db_field, request, **kwargs)
|
||||||
# customize the help text for all formfields for manytomany
|
# customize the help text for all formfields for manytomany
|
||||||
formfield.help_text = (
|
formfield.help_text = (
|
||||||
|
@ -192,9 +203,14 @@ class AuditedAdmin(admin.ModelAdmin):
|
||||||
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
||||||
"""customize the behavior of formfields with foreign key relationships. this will customize
|
"""customize the behavior of formfields with foreign key relationships. this will customize
|
||||||
the behavior of selects. customized behavior includes sorting of objects in list"""
|
the behavior of selects. customized behavior includes sorting of objects in list"""
|
||||||
|
|
||||||
|
# Define a queryset. Note that in the super of this,
|
||||||
|
# a new queryset will only be generated if one does not exist.
|
||||||
|
# Thus, the order in which we define queryset matters.
|
||||||
queryset = AdminSortFields.get_queryset(db_field)
|
queryset = AdminSortFields.get_queryset(db_field)
|
||||||
if queryset:
|
if queryset:
|
||||||
kwargs["queryset"] = queryset
|
kwargs["queryset"] = queryset
|
||||||
|
|
||||||
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
@ -761,17 +777,6 @@ class DomainInformationAdmin(ListHeaderAdmin):
|
||||||
return readonly_fields # Read-only fields for analysts
|
return readonly_fields # Read-only fields for analysts
|
||||||
|
|
||||||
|
|
||||||
class Timer:
|
|
||||||
def __enter__(self):
|
|
||||||
self.start = time.time()
|
|
||||||
return self # This allows usage of the instance within the with block
|
|
||||||
|
|
||||||
def __exit__(self, *args):
|
|
||||||
self.end = time.time()
|
|
||||||
self.duration = self.end - self.start
|
|
||||||
logger.info(f"Execution time: {self.duration} seconds")
|
|
||||||
|
|
||||||
|
|
||||||
class DomainApplicationAdminForm(forms.ModelForm):
|
class DomainApplicationAdminForm(forms.ModelForm):
|
||||||
"""Custom form to limit transitions to available transitions"""
|
"""Custom form to limit transitions to available transitions"""
|
||||||
|
|
||||||
|
|
|
@ -271,10 +271,12 @@ h1, h2, h3,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hides the "clear" button on autocomplete, as we already have one to use
|
||||||
.select2-selection__clear {
|
.select2-selection__clear {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fixes a display issue where the list was entirely white, or had too much whitespace
|
||||||
.select2-dropdown {
|
.select2-dropdown {
|
||||||
display: inline-grid !important;
|
display: inline-grid !important;
|
||||||
}
|
}
|
36
src/registrar/models/utility/generic_helper.py
Normal file
36
src/registrar/models/utility/generic_helper.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
"""This file contains general purpose helpers that don't belong in any specific location"""
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Timer:
|
||||||
|
"""
|
||||||
|
This class is used to measure execution time for performance profiling.
|
||||||
|
__enter__ and __exit__ is used such that you can wrap any code you want
|
||||||
|
around a with statement. After this exits, logger.info will print
|
||||||
|
the execution time in seconds.
|
||||||
|
|
||||||
|
Note that this class does not account for general randomness as more
|
||||||
|
robust libraries do, so there is some tiny amount of latency involved
|
||||||
|
in using this, but it is minimal enough that for most applications it is not
|
||||||
|
noticable.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
with Timer():
|
||||||
|
...some code
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
"""Starts the timer"""
|
||||||
|
self.start = time.time()
|
||||||
|
# This allows usage of the instance within the with block
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
"""Ends the timer and logs what happened"""
|
||||||
|
self.end = time.time()
|
||||||
|
self.duration = self.end - self.start
|
||||||
|
logger.info(f"Execution time: {self.duration} seconds")
|
Loading…
Add table
Add a link
Reference in a new issue