This commit is contained in:
CocoByte 2025-01-14 17:19:45 -07:00
parent e276f45f1d
commit 604004d897
No known key found for this signature in database
GPG key ID: BBFAA2526384C97F
2 changed files with 89 additions and 43 deletions

View file

@ -1632,14 +1632,18 @@ class DomainInformationAdmin(ListHeaderAdmin, ImportExportModelAdmin):
def lookups(self, request, model_admin): def lookups(self, request, model_admin):
# Annotate the queryset to avoid Python-side iteration # Annotate the queryset to avoid Python-side iteration
queryset = DomainInformation.objects.annotate( queryset = (
converted_generic_org=Case( DomainInformation.objects.annotate(
When(portfolio__organization_type__isnull=False, then="portfolio__organization_type"), converted_generic_org=Case(
When(portfolio__isnull=True, generic_org_type__isnull=False, then="generic_org_type"), When(portfolio__organization_type__isnull=False, then="portfolio__organization_type"),
default=Value(''), When(portfolio__isnull=True, generic_org_type__isnull=False, then="generic_org_type"),
output_field=CharField() default=Value(""),
output_field=CharField(),
)
) )
).values_list('converted_generic_org', flat=True).distinct() .values_list("converted_generic_org", flat=True)
.distinct()
)
# Filter out empty results and return sorted list of unique values # Filter out empty results and return sorted list of unique values
return sorted([(org, org) for org in queryset if org]) return sorted([(org, org) for org in queryset if org])
@ -1984,14 +1988,18 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
def lookups(self, request, model_admin): def lookups(self, request, model_admin):
# Annotate the queryset to avoid Python-side iteration # Annotate the queryset to avoid Python-side iteration
queryset = DomainRequest.objects.annotate( queryset = (
converted_generic_org=Case( DomainRequest.objects.annotate(
When(portfolio__organization_type__isnull=False, then="portfolio__organization_type"), converted_generic_org=Case(
When(portfolio__isnull=True, generic_org_type__isnull=False, then="generic_org_type"), When(portfolio__organization_type__isnull=False, then="portfolio__organization_type"),
default=Value(''), When(portfolio__isnull=True, generic_org_type__isnull=False, then="generic_org_type"),
output_field=CharField() default=Value(""),
output_field=CharField(),
)
) )
).values_list('converted_generic_org', flat=True).distinct() .values_list("converted_generic_org", flat=True)
.distinct()
)
# Filter out empty results and return sorted list of unique values # Filter out empty results and return sorted list of unique values
return sorted([(org, org) for org in queryset if org]) return sorted([(org, org) for org in queryset if org])
@ -2014,14 +2022,26 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
def lookups(self, request, model_admin): def lookups(self, request, model_admin):
# Annotate the queryset for efficient filtering # Annotate the queryset for efficient filtering
queryset = DomainRequest.objects.annotate( queryset = (
converted_federal_type=Case( DomainRequest.objects.annotate(
When(portfolio__isnull=False, portfolio__federal_agency__federal_type__isnull=False, then="portfolio__federal_agency__federal_type"), converted_federal_type=Case(
When(portfolio__isnull=True, federal_agency__federal_type__isnull=False, then="federal_agency__federal_type"), When(
default=Value(''), portfolio__isnull=False,
output_field=CharField() portfolio__federal_agency__federal_type__isnull=False,
then="portfolio__federal_agency__federal_type",
),
When(
portfolio__isnull=True,
federal_agency__federal_type__isnull=False,
then="federal_agency__federal_type",
),
default=Value(""),
output_field=CharField(),
)
) )
).values_list('converted_federal_type', flat=True).distinct() .values_list("converted_federal_type", flat=True)
.distinct()
)
# Filter out empty values and return sorted unique entries # Filter out empty values and return sorted unique entries
return sorted([(federal_type, federal_type) for federal_type in queryset if federal_type]) return sorted([(federal_type, federal_type) for federal_type in queryset if federal_type])
@ -2029,8 +2049,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
def queryset(self, request, queryset): def queryset(self, request, queryset):
if self.value(): if self.value():
return queryset.filter( return queryset.filter(
Q(portfolio__federal_type=self.value()) Q(portfolio__federal_type=self.value()) | Q(portfolio__isnull=True, federal_type=self.value())
| Q(portfolio__isnull=True, federal_type=self.value())
) )
return queryset return queryset
@ -3164,7 +3183,7 @@ class DomainAdmin(ListHeaderAdmin, ImportExportModelAdmin):
return queryset.filter(domain_info__is_election_board=True) return queryset.filter(domain_info__is_election_board=True)
if self.value() == "0": if self.value() == "0":
return queryset.filter(Q(domain_info__is_election_board=False) | Q(domain_info__is_election_board=None)) return queryset.filter(Q(domain_info__is_election_board=False) | Q(domain_info__is_election_board=None))
class GenericOrgFilter(admin.SimpleListFilter): class GenericOrgFilter(admin.SimpleListFilter):
"""Custom Generic Organization filter that accomodates portfolio feature. """Custom Generic Organization filter that accomodates portfolio feature.
If we have a portfolio, use the portfolio's organization. If not, use the If we have a portfolio, use the portfolio's organization. If not, use the
@ -3175,14 +3194,27 @@ class DomainAdmin(ListHeaderAdmin, ImportExportModelAdmin):
def lookups(self, request, model_admin): def lookups(self, request, model_admin):
# Annotate the queryset to avoid Python-side iteration # Annotate the queryset to avoid Python-side iteration
queryset = Domain.objects.annotate( queryset = (
converted_generic_org=Case( Domain.objects.annotate(
When(domain_info__isnull=False, domain_info__portfolio__organization_type__isnull=False, then="domain_info__portfolio__organization_type"), converted_generic_org=Case(
When(domain_info__isnull=False, domain_info__portfolio__isnull=True, domain_info__generic_org_type__isnull=False, then="domain_info__generic_org_type"), When(
default=Value(''), domain_info__isnull=False,
output_field=CharField() domain_info__portfolio__organization_type__isnull=False,
then="domain_info__portfolio__organization_type",
),
When(
domain_info__isnull=False,
domain_info__portfolio__isnull=True,
domain_info__generic_org_type__isnull=False,
then="domain_info__generic_org_type",
),
default=Value(""),
output_field=CharField(),
)
) )
).values_list('converted_generic_org', flat=True).distinct() .values_list("converted_generic_org", flat=True)
.distinct()
)
# Filter out empty results and return sorted list of unique values # Filter out empty results and return sorted list of unique values
return sorted([(org, org) for org in queryset if org]) return sorted([(org, org) for org in queryset if org])
@ -3205,14 +3237,27 @@ class DomainAdmin(ListHeaderAdmin, ImportExportModelAdmin):
def lookups(self, request, model_admin): def lookups(self, request, model_admin):
# Annotate the queryset for efficient filtering # Annotate the queryset for efficient filtering
queryset = Domain.objects.annotate( queryset = (
converted_federal_type=Case( Domain.objects.annotate(
When(domain_info__isnull=False, domain_info__portfolio__isnull=False, then=F("domain_info__portfolio__organization_type")), converted_federal_type=Case(
When(domain_info__isnull=False, domain_info__portfolio__isnull=True, domain_info__federal_type__isnull=False, then="domain_info__federal_agency__federal_type"), When(
default=Value(''), domain_info__isnull=False,
output_field=CharField() domain_info__portfolio__isnull=False,
then=F("domain_info__portfolio__organization_type"),
),
When(
domain_info__isnull=False,
domain_info__portfolio__isnull=True,
domain_info__federal_type__isnull=False,
then="domain_info__federal_agency__federal_type",
),
default=Value(""),
output_field=CharField(),
)
) )
).values_list('converted_federal_type', flat=True).distinct() .values_list("converted_federal_type", flat=True)
.distinct()
)
# Filter out empty values and return sorted unique entries # Filter out empty values and return sorted unique entries
return sorted([(federal_type, federal_type) for federal_type in queryset if federal_type]) return sorted([(federal_type, federal_type) for federal_type in queryset if federal_type])
@ -3224,7 +3269,7 @@ class DomainAdmin(ListHeaderAdmin, ImportExportModelAdmin):
| Q(domain_info__portfolio__isnull=True, domain_info__federal_type=self.value()) | Q(domain_info__portfolio__isnull=True, domain_info__federal_type=self.value())
) )
return queryset return queryset
def get_annotated_queryset(self, queryset): def get_annotated_queryset(self, queryset):
return queryset.annotate( return queryset.annotate(
converted_generic_org_type=Case( converted_generic_org_type=Case(

View file

@ -323,10 +323,10 @@ class DomainRequestFixture:
cls._create_domain_requests(users) cls._create_domain_requests(users)
@classmethod @classmethod
def _create_domain_requests(cls, users): def _create_domain_requests(cls, users): # noqa: C901
"""Creates DomainRequests given a list of users.""" """Creates DomainRequests given a list of users."""
total_domain_requests_to_make = 1000 total_domain_requests_to_make = 1000
# Check if the database is already populated with the desired # Check if the database is already populated with the desired
# number of entries. # number of entries.
# (Prevents re-adding more entries to an already populated database, # (Prevents re-adding more entries to an already populated database,
@ -349,7 +349,9 @@ class DomainRequestFixture:
except Exception as e: except Exception as e:
logger.warning(e) logger.warning(e)
num_additional_requests_to_make = total_domain_requests_to_make-domain_requests_already_made-len(domain_requests_to_create) num_additional_requests_to_make = (
total_domain_requests_to_make - domain_requests_already_made - len(domain_requests_to_create)
)
if num_additional_requests_to_make > 0: if num_additional_requests_to_make > 0:
for _ in range(num_additional_requests_to_make): for _ in range(num_additional_requests_to_make):
random_user = random.choice(users) random_user = random.choice(users)
@ -366,7 +368,6 @@ class DomainRequestFixture:
except Exception as e: except Exception as e:
logger.warning(f"Error creating random domain request: {e}") logger.warning(f"Error creating random domain request: {e}")
# Bulk create domain requests # Bulk create domain requests
cls._bulk_create_requests(domain_requests_to_create) cls._bulk_create_requests(domain_requests_to_create)