Skip to content

Commit

Permalink
check the last submission date from submission in s3 and compare with…
Browse files Browse the repository at this point in the history
… submission from odk
  • Loading branch information
nrjadkry committed Dec 13, 2023
1 parent a4c89b2 commit d54b08c
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/backend/app/submission/submission_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from app.central.central_crud import get_odk_form, get_odk_project, list_odk_xforms
from app.config import settings
from app.projects import project_crud, project_schemas
from app.s3 import add_obj_to_bucket
from app.s3 import add_obj_to_bucket, get_obj_from_bucket
from app.tasks import tasks_crud


Expand Down Expand Up @@ -471,13 +471,26 @@ def update_submission_in_s3(
last_submission = max(
valid_datetimes, key=lambda x: datetime.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ")
)
zip_file_last_submission = datetime()
# TODO query if a file exists already
# If it does, then query the metadata and get the zip_file_last_submission
if last_submission <= zip_file_last_submission:
# The zip file is up to date, redirect
pass
# return RedirectResponse(...)

# Check if the file already exists in s3
s3_path = f"/{project.organisation_id}/{project_id}/submission1.zip"
try:
file = get_obj_from_bucket(settings.S3_BUCKET_NAME,s3_path)

# Open the zip file
with zipfile.ZipFile(file, 'r') as zip_ref:
# Read the contents of the specific file from the zip archive
with zip_ref.open("metadata.json") as file_in_zip:
content = file_in_zip.read()

# Get the last submission date from the metadata
zip_file_last_submission = (json.loads(content))['last_submission']

if last_submission <= zip_file_last_submission:
return True

except Exception as e:
return True

# Zip file is outdated, regenerate
metadata = {
Expand Down Expand Up @@ -514,6 +527,7 @@ def update_submission_in_s3(
return True

except Exception as e:
print("Error = ", str(e))
# Update background task status to FAILED
update_bg_task_sync = async_to_sync(
project_crud.update_background_task_status_in_database
Expand Down

0 comments on commit d54b08c

Please sign in to comment.