Skip to content

Instantly share code, notes, and snippets.

@rgaudin
Last active December 12, 2015 03:29
Show Gist options
  • Save rgaudin/4707314 to your computer and use it in GitHub Desktop.
Save rgaudin/4707314 to your computer and use it in GitHub Desktop.
Example scripted submission to formhub
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu
from microsite.formhub import (submit_xml_forms_formhub_raw,
ErrorUploadingDataToFormhub,
ErrorMultipleUploadingDataToFormhub)
submission_target_user = 'reg'
submission_url = 'http://formhub.org/%s/submission' % submission_target_user
bulk_submission_url = 'http://formhub.org/%s/bulk-submission' % submission_target_user
xforms = []
submission_xml = u"""<?xml version='1.0' ?>
<atest id="atest">
<formhub>
</formhub>
<imei>no device properties in enketo</imei>
<start_time>2013-01-03T12:01:49.000-00:00</start_time>
<end_time>2013-01-03T12:01:59.000-00:00</end_time>
<survey_day>2013-01-03</survey_day>
<deviceid>no device properties in enketo</deviceid>
<sim_serial_number>no device properties in enketo</sim_serial_number>
<phone_number>no device properties in enketo</phone_number>
<name>ali</name>
<age>20</age>
<meta>
</meta>
</atest>"""
# submission is just an xml string.
# f = open('some_file.xml', 'r')
# xforms.append(f.read())
# f.close()
xforms.append(submission_xml)
# attachments is an array of tuples ; each containing (name, content)
# attachments = [('picture.jpg', open('xxx.jpg').read())]
attachments = []
try:
submit_xml_forms_formhub_raw(submission_url=submission_url,
xforms=xforms,
as_bulk=False,
attachments=attachments,
bulk_submission_url=bulk_submission_url)
except (ErrorUploadingDataToFormhub, ErrorMultipleUploadingDataToFormhub) as e:
print(e)
print(e.details())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment