Skip to content

Commit

Permalink
linted
Browse files Browse the repository at this point in the history
  • Loading branch information
CocoByte committed Jan 15, 2025
1 parent e276f45 commit 604004d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 43 deletions.
123 changes: 84 additions & 39 deletions src/registrar/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand All @@ -2014,23 +2022,34 @@ 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])

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

Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions src/registrar/fixtures/fixtures_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 604004d

Please sign in to comment.