[DEV-14527] - Fix Account Download Agency Filter Validation#4643
Open
DavidMikolaKC wants to merge 9 commits intoqatfrom
Open
[DEV-14527] - Fix Account Download Agency Filter Validation#4643DavidMikolaKC wants to merge 9 commits intoqatfrom
DavidMikolaKC wants to merge 9 commits intoqatfrom
Conversation
zachflanders-frb
approved these changes
Apr 30, 2026
Contributor
zachflanders-frb
left a comment
There was a problem hiding this comment.
Generally, we try to avoid branching if/else statements because they can be a little hard to read. I included a few ideas for patterns that avoid the branching if/else pattern. Feel free to include these, but I am not blocking over these since they are more style/readability comments and this passes the style checks.
Comment on lines
+151
to
+163
| if filters.get("agency") and filters["agency"] != "all": | ||
| if filters["agency"].isdigit(): | ||
| if not ToptierAgency.objects.filter(toptier_agency_id=filters["agency"]).exists(): | ||
| raise NotFound(f"No agency was found with id {filters['agency']}") | ||
| else: | ||
| query_filters[f"{tas_id}__funding_toptier_agency_id"] = filters["agency"] | ||
| else: | ||
| try: | ||
| ToptierAgency.objects.get(abbreviation=filters["agency"]) | ||
| query_filters[f"{tas_id}__funding_toptier_agency__abbreviation"] = filters["agency"] | ||
| except ToptierAgency.DoesNotExist as e: | ||
| raise NotFound(f"No agency was found with abbreviation {filters['agency']}") from e | ||
|
|
Contributor
There was a problem hiding this comment.
You could isolate the items that are changing to make this a little more DRY.
Suggested change
| if filters.get("agency") and filters["agency"] != "all": | |
| if filters["agency"].isdigit(): | |
| if not ToptierAgency.objects.filter(toptier_agency_id=filters["agency"]).exists(): | |
| raise NotFound(f"No agency was found with id {filters['agency']}") | |
| else: | |
| query_filters[f"{tas_id}__funding_toptier_agency_id"] = filters["agency"] | |
| else: | |
| try: | |
| ToptierAgency.objects.get(abbreviation=filters["agency"]) | |
| query_filters[f"{tas_id}__funding_toptier_agency__abbreviation"] = filters["agency"] | |
| except ToptierAgency.DoesNotExist as e: | |
| raise NotFound(f"No agency was found with abbreviation {filters['agency']}") from e | |
| filter_param, query_filter_suffix = ( | |
| ("top_tier_agency_id", "id") | |
| if filters["agency"].isdigit() | |
| else ("abbreviation", "abbreviation") | |
| ) | |
| if ToptierAgency.objects.filter(**{filter_param: filters["agency"]}).exists(): | |
| query_filters[f"{tas_id}__funding_toptier_agency_{query_filter_suffix}"] = filters["agency"] | |
| else: | |
| raise NotFound(f"No agency was found with {query_filter_suffix} {filters['agency']}") |
Comment on lines
+676
to
+684
| if agency_filter and agency_filter.lower() != "all": | ||
| if agency_filter.isdigit(): | ||
| if not ToptierAgency.objects.filter(toptier_agency_id=agency_filter).exists(): | ||
| raise NotFound(f"No agency was found with id {agency_filter}") | ||
| else: | ||
| try: | ||
| ToptierAgency.objects.get(abbreviation=agency_filter) | ||
| except ToptierAgency.DoesNotExist as e: | ||
| raise NotFound(f"No agency was found with abbreviation {agency_filter}") from e |
Contributor
There was a problem hiding this comment.
Here you could name the conditionals and use them together instead of the branching if/elses
Suggested change
| if agency_filter and agency_filter.lower() != "all": | |
| if agency_filter.isdigit(): | |
| if not ToptierAgency.objects.filter(toptier_agency_id=agency_filter).exists(): | |
| raise NotFound(f"No agency was found with id {agency_filter}") | |
| else: | |
| try: | |
| ToptierAgency.objects.get(abbreviation=agency_filter) | |
| except ToptierAgency.DoesNotExist as e: | |
| raise NotFound(f"No agency was found with abbreviation {agency_filter}") from e | |
| has_agency_filter = agency_filter and agency_filter.lower() != "all" | |
| is_valid_id = ( | |
| agency_filter.isdigit() | |
| and ToptierAgency.objects.filter(toptier_agency_id=agency_filter).exists() | |
| ) | |
| is_valid_abbr = ToptierAgency.objects.filter(abbreviation=agency_filter).exists() | |
| if has_agency_filter and not (is_valid_id or is_valid_abbr): | |
| raise NotFound( | |
| f"No agency was found with {'id' if agency_filter.isdigit() else 'abbreviation'} {agency_filter}" | |
| ) |
Comment on lines
+66
to
80
| field = "" | ||
| result = "All" | ||
| if request_agency and request_agency != "all": | ||
| toptier_agency_filter = ToptierAgency.objects.filter(toptier_agency_id=request_agency).first() | ||
| if type(request_agency) is int or request_agency.isdigit(): | ||
| toptier_agency_filter = ToptierAgency.objects.filter(toptier_agency_id=request_agency).first() | ||
| field = "toptier_code" | ||
| else: | ||
| try: | ||
| toptier_agency_filter = ToptierAgency.objects.get(abbreviation=request_agency) | ||
| field = "abbreviation" | ||
| except ToptierAgency.DoesNotExist: | ||
| toptier_agency_filter = None | ||
| if toptier_agency_filter: | ||
| result = toptier_agency_filter.toptier_code | ||
| result = getattr(toptier_agency_filter, field) | ||
| return result |
Contributor
There was a problem hiding this comment.
Here's another idea to avoid the branching if/else statements, you could try a match/case statement:
Suggested change
| field = "" | |
| result = "All" | |
| if request_agency and request_agency != "all": | |
| toptier_agency_filter = ToptierAgency.objects.filter(toptier_agency_id=request_agency).first() | |
| if type(request_agency) is int or request_agency.isdigit(): | |
| toptier_agency_filter = ToptierAgency.objects.filter(toptier_agency_id=request_agency).first() | |
| field = "toptier_code" | |
| else: | |
| try: | |
| toptier_agency_filter = ToptierAgency.objects.get(abbreviation=request_agency) | |
| field = "abbreviation" | |
| except ToptierAgency.DoesNotExist: | |
| toptier_agency_filter = None | |
| if toptier_agency_filter: | |
| result = toptier_agency_filter.toptier_code | |
| result = getattr(toptier_agency_filter, field) | |
| return result | |
| match request_agency: | |
| case None | "" | "all": | |
| result = "All" | |
| case int() | str() if isinstance(request_agency, int) or request_agency.isdigit(): | |
| toptier_agency = ToptierAgency.objects.filter(toptier_agency_id=request_agency).first() | |
| result = toptier_agency.toptier_code if top_tier_agency else "All" | |
| case str(): | |
| toptier_agency = ToptierAgency.objects.get(abbreviation=request_agency) | |
| result = toptier_agency.abbreviation if top_tier_agency else "All" | |
| case _: | |
| result = "All" | |
| return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
The validation for the /api/v2/download/accounts endpoint has been updated to prevent users from submitting toptier_agency_id's that don't exist. Additionally, users are now able to submit agency abbreviations within the agency field. Invalid agency abbreviations are similarly prevented from submitting abbreviations that do not exist.
Requirements for PR Merge:
Explain N/A in above checklist: