From 604004d89711c67eb749b0eea937f2ac6073ec73 Mon Sep 17 00:00:00 2001 From: CocoByte Date: Tue, 14 Jan 2025 17:19:45 -0700 Subject: [PATCH] linted --- src/registrar/admin.py | 123 +++++++++++++------- src/registrar/fixtures/fixtures_requests.py | 9 +- 2 files changed, 89 insertions(+), 43 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 090ac56a8..1da5ee6ba 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1632,14 +1632,18 @@ class GenericOrgFilter(admin.SimpleListFilter): def lookups(self, request, model_admin): # Annotate the queryset to avoid Python-side iteration - queryset = DomainInformation.objects.annotate( - converted_generic_org=Case( - When(portfolio__organization_type__isnull=False, then="portfolio__organization_type"), - When(portfolio__isnull=True, generic_org_type__isnull=False, then="generic_org_type"), - default=Value(''), - output_field=CharField() + queryset = ( + DomainInformation.objects.annotate( + converted_generic_org=Case( + When(portfolio__organization_type__isnull=False, then="portfolio__organization_type"), + When(portfolio__isnull=True, generic_org_type__isnull=False, then="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 return sorted([(org, org) for org in queryset if org]) @@ -1984,14 +1988,18 @@ class GenericOrgFilter(admin.SimpleListFilter): def lookups(self, request, model_admin): # Annotate the queryset to avoid Python-side iteration - queryset = DomainRequest.objects.annotate( - converted_generic_org=Case( - When(portfolio__organization_type__isnull=False, then="portfolio__organization_type"), - When(portfolio__isnull=True, generic_org_type__isnull=False, then="generic_org_type"), - default=Value(''), - output_field=CharField() + queryset = ( + DomainRequest.objects.annotate( + converted_generic_org=Case( + When(portfolio__organization_type__isnull=False, then="portfolio__organization_type"), + When(portfolio__isnull=True, generic_org_type__isnull=False, then="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 return sorted([(org, org) for org in queryset if org]) @@ -2014,14 +2022,26 @@ class FederalTypeFilter(admin.SimpleListFilter): def lookups(self, request, model_admin): # Annotate the queryset for efficient filtering - queryset = DomainRequest.objects.annotate( - converted_federal_type=Case( - When(portfolio__isnull=False, 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() + queryset = ( + DomainRequest.objects.annotate( + converted_federal_type=Case( + When( + portfolio__isnull=False, + 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 return sorted([(federal_type, federal_type) for federal_type in queryset if federal_type]) @@ -2029,8 +2049,7 @@ def lookups(self, request, model_admin): def queryset(self, request, queryset): if self.value(): return queryset.filter( - Q(portfolio__federal_type=self.value()) - | Q(portfolio__isnull=True, federal_type=self.value()) + Q(portfolio__federal_type=self.value()) | Q(portfolio__isnull=True, federal_type=self.value()) ) return queryset @@ -3164,7 +3183,7 @@ def queryset(self, request, queryset): return queryset.filter(domain_info__is_election_board=True) if self.value() == "0": return queryset.filter(Q(domain_info__is_election_board=False) | Q(domain_info__is_election_board=None)) - + class GenericOrgFilter(admin.SimpleListFilter): """Custom Generic Organization filter that accomodates portfolio feature. If we have a portfolio, use the portfolio's organization. If not, use the @@ -3175,14 +3194,27 @@ class GenericOrgFilter(admin.SimpleListFilter): def lookups(self, request, model_admin): # Annotate the queryset to avoid Python-side iteration - queryset = Domain.objects.annotate( - converted_generic_org=Case( - When(domain_info__isnull=False, 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() + queryset = ( + Domain.objects.annotate( + converted_generic_org=Case( + When( + domain_info__isnull=False, + 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 return sorted([(org, org) for org in queryset if org]) @@ -3205,14 +3237,27 @@ class FederalTypeFilter(admin.SimpleListFilter): def lookups(self, request, model_admin): # Annotate the queryset for efficient filtering - queryset = Domain.objects.annotate( - converted_federal_type=Case( - When(domain_info__isnull=False, 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() + queryset = ( + Domain.objects.annotate( + converted_federal_type=Case( + When( + domain_info__isnull=False, + 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 return sorted([(federal_type, federal_type) for federal_type in queryset if federal_type]) @@ -3224,7 +3269,7 @@ def queryset(self, request, queryset): | Q(domain_info__portfolio__isnull=True, domain_info__federal_type=self.value()) ) return queryset - + def get_annotated_queryset(self, queryset): return queryset.annotate( converted_generic_org_type=Case( diff --git a/src/registrar/fixtures/fixtures_requests.py b/src/registrar/fixtures/fixtures_requests.py index 6fd03c410..56653b3b2 100644 --- a/src/registrar/fixtures/fixtures_requests.py +++ b/src/registrar/fixtures/fixtures_requests.py @@ -323,10 +323,10 @@ def load(cls): cls._create_domain_requests(users) @classmethod - def _create_domain_requests(cls, users): + def _create_domain_requests(cls, users): # noqa: C901 """Creates DomainRequests given a list of users.""" total_domain_requests_to_make = 1000 - + # Check if the database is already populated with the desired # number of entries. # (Prevents re-adding more entries to an already populated database, @@ -349,7 +349,9 @@ def _create_domain_requests(cls, users): except Exception as 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: for _ in range(num_additional_requests_to_make): random_user = random.choice(users) @@ -366,7 +368,6 @@ def _create_domain_requests(cls, users): except Exception as e: logger.warning(f"Error creating random domain request: {e}") - # Bulk create domain requests cls._bulk_create_requests(domain_requests_to_create)