Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions usaspending_api/download/download_utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from datetime import datetime, timezone
from typing import Any

from django.conf import settings
from rest_framework.request import Request

from usaspending_api.common.helpers.text_helpers import slugify_text_for_file_names
from usaspending_api.common.logging import get_remote_addr
from usaspending_api.download.helpers import write_to_download_log
from usaspending_api.download.lookups import VALUE_MAPPINGS
from usaspending_api.download.models import DownloadJob
from usaspending_api.references.models import ToptierAgency


def create_unique_filename(json_request, origination=None):
def create_unique_filename(json_request: dict[str, Any], origination: str | None = None) -> str:
timestamp = datetime.strftime(datetime.now(timezone.utc), "%Y-%m-%d_H%HM%MS%S%f")
request_agency = json_request.get("agency", "all")
request_agency = json_request.get("filters", {}).get("agency", "all")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just needed to update the access path to include "filters". The "agency" property is on "filters" instead of the parent level of the json_request.


if json_request.get("is_for_idv"):
download_name = f"IDV_{slugify_text_for_file_names(json_request.get('piid'), 'UNKNOWN', 50)}_{timestamp}.zip"
Expand Down Expand Up @@ -50,7 +53,7 @@ def create_unique_filename(json_request, origination=None):
return download_name


def obtain_zip_filename_format(download_types):
def obtain_zip_filename_format(download_types: list[str]) -> str:
if len(download_types) > 1:
return "{data_quarters}_{agency}_{level}_AccountData_{timestamp}.zip"
return f"{VALUE_MAPPINGS[download_types[0]]['zipfile_template']}.zip"
Expand All @@ -65,7 +68,7 @@ def obtain_filename_prefix_from_agency_id(request_agency: int | str) -> str:
return result


def create_award_level_string(download_types):
def create_award_level_string(download_types: list[str]) -> str:
type_list = []
for award_level in download_types:
if "type_name" in VALUE_MAPPINGS[award_level]:
Expand All @@ -75,7 +78,7 @@ def create_award_level_string(download_types):
return "And".join(type_list)


def log_new_download_job(request, download_job):
def log_new_download_job(request: Request, download_job: DownloadJob) -> None:
write_to_download_log(
message="Starting new download job [{}]".format(download_job.download_job_id),
download_job=download_job,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import json
import pytest
import random
import zipfile
import pandas as pd

from unittest.mock import Mock

import pandas as pd
import pytest
from django.conf import settings
from django.core.management import call_command
from model_bakery import baker
Expand All @@ -26,19 +25,19 @@ def create_download_delta_tables(spark, s3_unittest_data_bucket, hive_unittest_m
call_command(
"create_delta_table",
f"--spark-s3-bucket={s3_unittest_data_bucket}",
f"--destination-table=award_financial_download",
"--destination-table=award_financial_download",
)

call_command(
"create_delta_table",
f"--spark-s3-bucket={s3_unittest_data_bucket}",
f"--destination-table=object_class_program_activity_download",
"--destination-table=object_class_program_activity_download",
)

call_command(
"create_delta_table",
f"--spark-s3-bucket={s3_unittest_data_bucket}",
f"--destination-table=account_balances_download",
"--destination-table=account_balances_download",
)
yield

Expand Down Expand Up @@ -237,8 +236,8 @@ def test_agency_filter_success(client, download_test_data):
}
),
)

assert resp.status_code == status.HTTP_200_OK
assert "FY2017Q1-Q4_100_FA_AccountBalances" in resp.json()["file_name"]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to the test to ensure the name is correct and includes the agency code "100"



@pytest.mark.django_db(databases=[settings.DOWNLOAD_DB_ALIAS, settings.DEFAULT_DB_ALIAS])
Expand Down
Loading