Skip to content

Commit

Permalink
Update Spreadsheet Retrieval using Metadata (#849)
Browse files Browse the repository at this point in the history
* fix retrieve spreadsheet

* improved fallback mechanism

* get user friendly filename
  • Loading branch information
israr-ulhaq authored Nov 19, 2024
1 parent 38d2d41 commit dd9ee8e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions data_store/controllers/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
get_or_generate_submission_id,
)
from data_store.controllers.mappings import INGEST_MAPPINGS, DataMapping
from data_store.controllers.retrieve_submission_file import get_custom_file_name
from data_store.db import db
from data_store.db.entities import Fund, Programme, ProgrammeJunction, Submission
from data_store.db.queries import (
Expand Down Expand Up @@ -504,6 +505,7 @@ def save_submission_file_s3(excel_file: FileStorage, submission_id: str):
metadata={
"submission_id": submission_id,
"filename": f"{fund_type}-{str(uuid)}.xlsx",
"download_filename": f"{get_custom_file_name(uuid)}.xlsx",
"programme_name": programme_name,
},
)
Expand Down
23 changes: 18 additions & 5 deletions data_store/controllers/retrieve_submission_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from config import Config
from data_store.aws import create_presigned_url, get_file_header
from data_store.db.entities import Fund, Organisation, Programme, ProgrammeJunction, ReportingRound, Submission
Expand Down Expand Up @@ -52,7 +54,12 @@ def retrieve_submission_file(submission_id) -> str:
return presigned_url


def get_custom_file_name(submission_id: str) -> str:
def get_custom_file_name(
submission_id: str,
fallback_date: Optional[str] = None,
fallback_fund_code: Optional[str] = None,
fallback_org_name: Optional[str] = None,
) -> str:
submission_info = (
Programme.query.join(ProgrammeJunction)
.join(Submission)
Expand All @@ -71,10 +78,16 @@ def get_custom_file_name(submission_id: str) -> str:
.one_or_none()
)

date = submission_info.submission_date.strftime("%Y-%m-%d")
start_date = submission_info.observation_period_start.strftime("%b%Y")
end_date = submission_info.observation_period_end.strftime("%b%Y")
if submission_info:
date = submission_info.submission_date.strftime("%Y-%m-%d")
start_date = submission_info.observation_period_start.strftime("%b%Y")
end_date = submission_info.observation_period_end.strftime("%b%Y")

file_name = f"{date}-{submission_info.fund_code}-{submission_info.organisation_name}-{start_date}-{end_date}"
file_name = f"{date}-{submission_info.fund_code}-{submission_info.organisation_name}-{start_date}-{end_date}"
else:
date = fallback_date
fund_code = fallback_fund_code
org_name = fallback_org_name
file_name = f"{date}-{fund_code}-{org_name}-{submission_id}"

return file_name.replace(" ", "-").lower()
15 changes: 13 additions & 2 deletions find/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,23 @@ def retrieve_spreadsheet(fund_code: str, submission_id: str):
return abort(404)

form = RetrieveForm()

file_metadata = file_header["metadata"]
file_name = file_metadata.get("download_filename")
programme_name = file_metadata.get("programme_name")
submission_date = file_header["last_modified"].strftime("%d %B %Y")
if not file_name:
custom_file_name = get_custom_file_name(
submission_id=submission_id,
fallback_date=submission_date,
fallback_fund_code=fund_code,
fallback_org_name=programme_name,
)
file_name = f"{custom_file_name}.xlsx"
if form.validate_on_submit():
presigned_url = create_presigned_url(
bucket_name=Config.AWS_S3_BUCKET_SUCCESSFUL_FILES,
file_key=object_name,
filename=f"{get_custom_file_name(submission_id)}.xlsx",
filename=file_name,
)

return redirect(presigned_url)
Expand Down

0 comments on commit dd9ee8e

Please sign in to comment.