Skip to content

Commit

Permalink
Merge pull request #505 from ehallein/master
Browse files Browse the repository at this point in the history
Only send one email on ODK import errors
  • Loading branch information
ehallein authored Nov 1, 2023
2 parents da973d1 + 71bd1b6 commit 7540dc9
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions observations/odk.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,8 @@ def import_site_visit_end(form_id="site_visit_end", duration_hr=8, auth_headers=
project_id = settings.ODK_API_PROJECTID
LOGGER.info(f"Downloading {form_id} submission data")
submissions = get_form_submission_data(auth_headers, project_id, form_id)

#email content if any errors
emailText = None
for submission in submissions:
instance_id = submission['meta']['instanceID']
if Survey.objects.filter(source='odk', end_source_id=instance_id):
Expand All @@ -635,9 +636,10 @@ def import_site_visit_end(form_id="site_visit_end", duration_hr=8, auth_headers=
# Send a warning to the admins to investigate & address.
log = (f"Site Visit End form: unable to match a site for survey end at {location.wkt}")
LOGGER.warning(log)
content = json.dumps(submission, indent=2)
msg = EmailMultiAlternatives(log, content, settings.DEFAULT_FROM_EMAIL, settings.ADMIN_EMAILS)
msg.send(fail_silently=True)
if(emailText == None):
emailText = log
else:
emailText = emailText + "\n\n" + log
continue

# Try to match one (only) existing Survey object.
Expand All @@ -651,9 +653,10 @@ def import_site_visit_end(form_id="site_visit_end", duration_hr=8, auth_headers=
if surveys.count() != 1:
log = (f"Site Visit End form: unable to match a single Survey (matched {surveys.count()})")
LOGGER.warning(log)
content = json.dumps(submission, indent=2)
msg = EmailMultiAlternatives(log, content, settings.DEFAULT_FROM_EMAIL, settings.ADMIN_EMAILS)
msg.send(fail_silently=True)
if(emailText == None):
emailText = log
else:
emailText = emailText + "\n\n" + log
continue
else:
survey = surveys.first()
Expand All @@ -680,6 +683,11 @@ def import_site_visit_end(form_id="site_visit_end", duration_hr=8, auth_headers=
photo.save()
LOGGER.info(f'Created SurveyMediaAttachment {photo}')

#send an email with errors if needed
if emailText != None:
msg = EmailMultiAlternatives("Wastd import Errors", emailText, settings.DEFAULT_FROM_EMAIL, settings.ADMIN_EMAILS)
msg.send(fail_silently=True)


def import_marine_wildlife_incident(form_id="marine_wildlife_incident", auth_headers=None):
"""Import submissions to the Marine Wildlife Incident ODK form.
Expand Down

0 comments on commit 7540dc9

Please sign in to comment.