Skip to content

Commit

Permalink
Check if session files empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Rees authored and Morgan Rees committed Jan 3, 2025
1 parent 4cbaef6 commit 37fc87d
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions django_app/utils/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ def generate_presigned_url(s3_storage: Any, s3_file_object: Any) -> str:
def get_all_session_files(s3_storage: S3Boto3Storage, session: SessionBase) -> dict[str, dict[str, str]]:
"""Gets all files that a user has uploaded in a session."""
s3_client = get_s3_client_from_storage(s3_storage=s3_storage)
response = s3_client.list_objects_v2(Bucket=s3_storage.bucket.name, Prefix=session.session_key)
session_files = {}

user_uploaded_files = get_user_uploaded_files(session)
for content in response.get("Contents", []):
key = content["Key"]
file_name = key.rpartition("/")[2]

# checking that a file with this name was uploaded in the session
if file_name in user_uploaded_files:
session_files[key] = {
"file_name": file_name,
"url": reverse("download_document", kwargs={"file_name": file_name}),
}
if not session.is_empty():
response = s3_client.list_objects_v2(Bucket=s3_storage.bucket.name, Prefix=session.session_key)

user_uploaded_files = get_user_uploaded_files(session)
for content in response.get("Contents", []):
key = content["Key"]
file_name = key.rpartition("/")[2]

# checking that a file with this name was uploaded in the session
if file_name in user_uploaded_files:
session_files[key] = {
"file_name": file_name,
"url": reverse("download_document", kwargs={"file_name": file_name}),
}

return session_files

Expand Down

0 comments on commit 37fc87d

Please sign in to comment.