mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-25 20:18:38 +02:00
Improve performance
This commit is contained in:
parent
7ec4b32f88
commit
a51949da0b
1 changed files with 141 additions and 90 deletions
|
@ -1,6 +1,8 @@
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.db.models.functions import Concat
|
from django.db.models.functions import Concat, Coalesce
|
||||||
|
from django.db.models import Value, CharField
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django_fsm import get_available_FIELD_transitions
|
from django_fsm import get_available_FIELD_transitions
|
||||||
|
@ -12,6 +14,7 @@ from django.http.response import HttpResponseRedirect
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from epplibwrapper.errors import ErrorCode, RegistryError
|
from epplibwrapper.errors import ErrorCode, RegistryError
|
||||||
from registrar.models.domain import Domain
|
from registrar.models.domain import Domain
|
||||||
|
from registrar.models.domain_application import DomainApplication
|
||||||
from registrar.models.user import User
|
from registrar.models.user import User
|
||||||
from registrar.utility import csv_export
|
from registrar.utility import csv_export
|
||||||
from registrar.views.utility.mixins import OrderableFieldsMixin
|
from registrar.views.utility.mixins import OrderableFieldsMixin
|
||||||
|
@ -707,6 +710,17 @@ 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"""
|
||||||
|
|
||||||
|
@ -753,11 +767,37 @@ class DomainApplicationAdmin(ListHeaderAdmin):
|
||||||
"""Lookup reimplementation, gets users of is_staff.
|
"""Lookup reimplementation, gets users of is_staff.
|
||||||
Returns a list of tuples consisting of (user.id, user)
|
Returns a list of tuples consisting of (user.id, user)
|
||||||
"""
|
"""
|
||||||
privileged_users = User.objects.filter(is_staff=True).order_by("first_name", "last_name", "email")
|
logger.info("timing lookups")
|
||||||
return [(user.id, user) for user in privileged_users]
|
with Timer() as t:
|
||||||
|
|
||||||
|
# Select all investigators that are staff, then order by name and email
|
||||||
|
privileged_users = (
|
||||||
|
DomainApplication.objects.select_related("investigator")
|
||||||
|
.filter(investigator__is_staff=True)
|
||||||
|
.order_by(
|
||||||
|
"investigator__first_name",
|
||||||
|
"investigator__last_name",
|
||||||
|
"investigator__email"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Annotate the full name and return a values list that lookups can use
|
||||||
|
privileged_users_annotated = privileged_users.annotate(
|
||||||
|
full_name=Coalesce(
|
||||||
|
Concat(
|
||||||
|
"investigator__first_name", Value(" "), "investigator__last_name", output_field=CharField()
|
||||||
|
),
|
||||||
|
"investigator__email",
|
||||||
|
output_field=CharField()
|
||||||
|
)
|
||||||
|
).values_list("investigator__id", "full_name")
|
||||||
|
|
||||||
|
return privileged_users_annotated
|
||||||
|
|
||||||
def queryset(self, request, queryset):
|
def queryset(self, request, queryset):
|
||||||
"""Custom queryset implementation, filters by investigator"""
|
"""Custom queryset implementation, filters by investigator"""
|
||||||
|
logger.info("timing queryset")
|
||||||
|
with Timer() as t:
|
||||||
if self.value() is None:
|
if self.value() is None:
|
||||||
return queryset
|
return queryset
|
||||||
else:
|
else:
|
||||||
|
@ -863,11 +903,15 @@ class DomainApplicationAdmin(ListHeaderAdmin):
|
||||||
# lists in filter_horizontal are not sorted properly, sort them
|
# lists in filter_horizontal are not sorted properly, sort them
|
||||||
# by website
|
# by website
|
||||||
def formfield_for_manytomany(self, db_field, request, **kwargs):
|
def formfield_for_manytomany(self, db_field, request, **kwargs):
|
||||||
|
logger.info("timing formfield_for_manytomany")
|
||||||
|
with Timer() as t:
|
||||||
if db_field.name in ("current_websites", "alternative_domains"):
|
if db_field.name in ("current_websites", "alternative_domains"):
|
||||||
kwargs["queryset"] = models.Website.objects.all().order_by("website") # Sort websites
|
kwargs["queryset"] = models.Website.objects.all().order_by("website") # Sort websites
|
||||||
return super().formfield_for_manytomany(db_field, request, **kwargs)
|
return super().formfield_for_manytomany(db_field, request, **kwargs)
|
||||||
|
|
||||||
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
||||||
|
logger.info("timing formfield_for_foreignkey")
|
||||||
|
with Timer() as t:
|
||||||
# Removes invalid investigator options from the investigator dropdown
|
# Removes invalid investigator options from the investigator dropdown
|
||||||
if db_field.name == "investigator":
|
if db_field.name == "investigator":
|
||||||
kwargs["queryset"] = User.objects.filter(is_staff=True)
|
kwargs["queryset"] = User.objects.filter(is_staff=True)
|
||||||
|
@ -876,6 +920,8 @@ class DomainApplicationAdmin(ListHeaderAdmin):
|
||||||
|
|
||||||
# Trigger action when a fieldset is changed
|
# Trigger action when a fieldset is changed
|
||||||
def save_model(self, request, obj, form, change):
|
def save_model(self, request, obj, form, change):
|
||||||
|
logger.info("timing save_model")
|
||||||
|
with Timer() as t:
|
||||||
if obj and obj.creator.status != models.User.RESTRICTED:
|
if obj and obj.creator.status != models.User.RESTRICTED:
|
||||||
if change: # Check if the application is being edited
|
if change: # Check if the application is being edited
|
||||||
# Get the original application from the database
|
# Get the original application from the database
|
||||||
|
@ -941,7 +987,8 @@ class DomainApplicationAdmin(ListHeaderAdmin):
|
||||||
admin user permissions and the application creator's status, so
|
admin user permissions and the application creator's status, so
|
||||||
we'll use the baseline readonly_fields and extend it as needed.
|
we'll use the baseline readonly_fields and extend it as needed.
|
||||||
"""
|
"""
|
||||||
|
logger.info("timing get_readonly_fields")
|
||||||
|
with Timer() as t:
|
||||||
readonly_fields = list(self.readonly_fields)
|
readonly_fields = list(self.readonly_fields)
|
||||||
|
|
||||||
# Check if the creator is restricted
|
# Check if the creator is restricted
|
||||||
|
@ -961,6 +1008,8 @@ class DomainApplicationAdmin(ListHeaderAdmin):
|
||||||
return readonly_fields
|
return readonly_fields
|
||||||
|
|
||||||
def display_restricted_warning(self, request, obj):
|
def display_restricted_warning(self, request, obj):
|
||||||
|
logger.info("timing display_restricted_warning")
|
||||||
|
with Timer() as t:
|
||||||
if obj and obj.creator.status == models.User.RESTRICTED:
|
if obj and obj.creator.status == models.User.RESTRICTED:
|
||||||
messages.warning(
|
messages.warning(
|
||||||
request,
|
request,
|
||||||
|
@ -968,6 +1017,8 @@ class DomainApplicationAdmin(ListHeaderAdmin):
|
||||||
)
|
)
|
||||||
|
|
||||||
def change_view(self, request, object_id, form_url="", extra_context=None):
|
def change_view(self, request, object_id, form_url="", extra_context=None):
|
||||||
|
logger.info("timing change_view")
|
||||||
|
with Timer() as t:
|
||||||
obj = self.get_object(request, object_id)
|
obj = self.get_object(request, object_id)
|
||||||
self.display_restricted_warning(request, obj)
|
self.display_restricted_warning(request, obj)
|
||||||
return super().change_view(request, object_id, form_url, extra_context)
|
return super().change_view(request, object_id, form_url, extra_context)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue