Skip to content

Commit

Permalink
Merge pull request #2800 from fedspendingtransparency/fix/dev-6179-ha…
Browse files Browse the repository at this point in the history
…ndle-searching-all-agencies

[DEV-6179] Fix 'all' agency case breaking downloads
  • Loading branch information
tony-sappe authored Sep 24, 2020
2 parents 7a97fe9 + d760a9c commit fc49e97
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ This route sends a request to the backend to begin generating a zipfile of award
"file_name": "534_PrimeTransactionsAndSubawards_2020-01-13_H21M04S54995657.zip",
"file_url": "/csv_downloads/534_PrimeTransactionsAndSubawards_2020-01-13_H21M04S54995657.zip",
"download_request": {
"agency": 50,
"columns": [],
"download_types": [
"prime_awards",
Expand Down Expand Up @@ -163,4 +162,6 @@ Agency internal database id. If you wish to include all agencies, use 'all' inst
+ `type` (required, enum[string])
+ Members
+ `funding`
+ `awarding`
+ `awarding`
+ `toptier_name` (optional, string)
Provided when the `name` belongs to a subtier agency
2 changes: 2 additions & 0 deletions usaspending_api/api_contracts/contracts/v2/download/awards.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ This route sends a request to the backend to begin generating a zipfile of award
+ Members
+ `funding`
+ `awarding`
+ `toptier_name` (optional, string)
Provided when the `name` belongs to a subtier agency

### TimePeriod (object)
+ `start_date` (required, string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ This route sends a request to the backend to begin generating a zipfile of trans
+ Members
+ `funding`
+ `awarding`
+ `toptier_name` (optional, string)
Provided when the `name` belongs to a subtier agency

### TimePeriod (object)
+ `start_date` (required, string)
Expand Down
1 change: 0 additions & 1 deletion usaspending_api/download/v2/request_validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def validate_award_request(request_data: dict):
json_request = {"download_types": award_levels, "filters": {}}

# Set defaults of non-required parameters
json_request["agency"] = request_data["filters"]["agency"] if request_data["filters"].get("agency") else "all"
json_request["columns"] = request_data.get("columns", [])
json_request["file_format"] = str(request_data.get("file_format", "csv")).lower()

Expand Down
52 changes: 29 additions & 23 deletions usaspending_api/download/v2/year_limited_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,35 @@ def process_filters(self, request_data):
if required_param not in filters:
raise InvalidParameterException(f"Missing one or more required body parameters: {required_param}")

if "agency" not in filters and "agencies" not in filters:
raise InvalidParameterException(f"Request must include either 'agency' or 'agencies'")
# Replacing agency with agencies
if "agency" in filters:
if "agencies" not in filters:
filters["agencies"] = []
if str(filters["agency"]).lower() == "all":
toptier_name = "all"
else:
toptier_name = ToptierAgency.objects.filter(toptier_agency_id=filters["agency"]).values("name").first()
if toptier_name is None:
raise InvalidParameterException(f"Toptier ID not found: {filters['agency']}")
toptier_name = toptier_name["name"]
if "sub_agency" in filters:
filters["agencies"].append(
{
"type": "awarding",
"tier": "subtier",
"name": filters["sub_agency"],
"toptier_name": toptier_name,
}
)
del filters["sub_agency"]
else:
filters["agencies"].append({"type": "awarding", "tier": "toptier", "name": toptier_name})
del filters["agency"]

if "agencies" in filters:
filters["agencies"] = [val for val in filters["agencies"] if val.get("name", "").lower() != "all"]
else:
raise InvalidParameterException("Request must include either 'agency' or 'agencies'")

# Creating new filter for custom award download to keep Prime and Sub Awards separate;
# Also adding award levels based on filters passed
Expand Down Expand Up @@ -79,25 +106,4 @@ def process_filters(self, request_data):
del filters["date_range"]
del filters["date_type"]

# Replacing agency with agencies
if filters.get("agency"):
if filters["agency"] != "all":
toptier_name = ToptierAgency.objects.filter(toptier_agency_id=filters["agency"]).values("name")
if not toptier_name:
raise InvalidParameterException("Toptier ID not found: {}".format(filters["agency"]))
toptier_name = toptier_name[0]["name"]
if "sub_agency" in filters:
if filters["sub_agency"]:
filters["agencies"] = [
{
"type": "awarding",
"tier": "subtier",
"name": filters["sub_agency"],
"toptier_name": toptier_name,
}
]
del filters["sub_agency"]
else:
filters["agencies"] = [{"type": "awarding", "tier": "toptier", "name": toptier_name}]

request_data["filters"] = filters

0 comments on commit fc49e97

Please sign in to comment.