def download_xform(request, survey_id): interviewer = request.user survey = get_object_or_404(Survey, pk=survey_id) allocation = get_survey_allocation(interviewer) if allocation: try: if survey.has_sampling and allocation.stage in [None, SurveyAllocation.LISTING]: if allocation.stage is None: allocation.stage = SurveyAllocation.LISTING allocation.save() survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(interviewer, survey) survey_xform = get_household_list_xform(interviewer, survey, survey_listing.listing) else: survey_xform = get_survey_xform(interviewer, survey) form_id = '%s'% survey_id audit = { "xform": form_id } audit_log( Actions.FORM_XML_DOWNLOADED, request.user, interviewer, _("'%(interviewer)s' Downloaded XML for form '%(id_string)s'.") % { "interviewer": interviewer.name, "id_string": form_id }, audit, request) response = response_with_mimetype_and_name('xml', 'survey-%s' %survey_id, show_date=False, full_mime='text/xml') response.content = survey_xform return response except: raise print 'an error occurred' pass return OpenRosaResponseNotFound()
def form_list(request): """ This is where ODK Collect gets its download list. """ interviewer = request.user #get_object_or_404(Interviewer, mobile_number=username, odk_token=token) #to do - Make fetching households more e allocation = get_survey_allocation(interviewer) if allocation and interviewer.ea.open_batches: audit_log(Actions.USER_FORMLIST_REQUESTED, request.user, interviewer, _("survey allocation %s" % allocation.survey), {}, request) survey = allocation.survey survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(interviewer, survey) audit = {} audit_log(Actions.USER_FORMLIST_REQUESTED, request.user, interviewer, _("Requested forms list. for %s" % interviewer.name), audit, request) content = render_to_string("odk/xformsList.xml", { 'allocation' : allocation, 'survey' : survey, 'interviewer' : interviewer, 'request' : request, 'survey_listing': survey_listing, 'Const': SurveyAllocation }) response = BaseOpenRosaResponse(content) response.status_code = 200 return response else: return OpenRosaResponseNotFound('No survey allocated presently')
def submission(request): interviewer = request.user #get_object_or_404(Interviewer, mobile_number=username, odk_token=token) submission_date = datetime.now().isoformat() xml_file_list = [] html_response = False # request.FILES is a django.utils.datastructures.MultiValueDict # for each key we have a list of values try: xml_file_list = request.FILES.pop("xml_submission_file", []) if len(xml_file_list) != 1: return OpenRosaResponseBadRequest(u"There should be a single XML submission file.") media_files = request.FILES.values() submission_report = process_submission(interviewer, xml_file_list[0], media_files=media_files) logger.info(submission_report) context = Context({ 'message' : settings.ODK_SUBMISSION_SUCCESS_MSG, 'instanceID' : u'uuid:%s' % submission_report.instance_id, 'formid' : submission_report.form_id, 'submissionDate' : submission_date, 'markedAsCompleteDate' : submission_date }) t = loader.get_template('odk/submission.xml') audit = {} audit_log( Actions.SUBMISSION_CREATED, request.user, interviewer, _("'%(interviewer)s' Submitted XML for form '%(id_string)s'. Desc: '%(desc)s'") % { "interviewer": interviewer.name, "desc" : submission_report.description, "id_string": submission_report.form_id }, audit, request) response = BaseOpenRosaResponse(t.render(context)) response.status_code = 201 response['Location'] = request.build_absolute_uri(request.path) return response except NotEnoughHouseholds: desc = 'Not enough households' audit_log( Actions.SUBMISSION_REQUESTED, request.user, interviewer, _("Failed attempted to submit XML for form for interviewer: '%(interviewer)s'. desc: '%(desc)s'") % { "interviewer": interviewer.name, "desc" : desc }, {'desc' : desc}, request, logging.WARNING) return OpenRosaRequestForbidden(u"Not Enough Households") except HouseholdNumberAlreadyExists: desc = 'House number already exists' audit_log( Actions.SUBMISSION_REQUESTED, request.user, interviewer, _("Failed attempted to submit XML for form for interviewer: '%(interviewer)s'. desc: '%(desc)s'") % { "interviewer": interviewer.name, "desc" : desc }, {'desc' : desc}, request, logging.WARNING) # return OpenRosaRequestConflict(u'Household Number Already exists') return OpenRosaResponseNotAllowed(u'Household Number Already exists') except Exception, ex: audit_log( Actions.SUBMISSION_REQUESTED, request.user, interviewer, _("Failed attempted to submit XML for form for interviewer: '%(interviewer)s'. desc: '%(desc)s'") % { "interviewer": interviewer.name, "desc" : str(ex) }, {'desc' : str(ex)}, request, logging.WARNING) return OpenRosaServerError(u"An error occurred. Please try again")
def download_houselist_xform(request): interviewer = request.user allocation = get_survey_allocation(interviewer) response = OpenRosaResponseNotFound() if allocation: survey = allocation.survey survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(interviewer, survey) householdlist_xform = get_household_list_xform(interviewer, survey, survey_listing.listing) form_id = 'allocation-%s'% allocation.id audit = { "xform": form_id } audit_log( Actions.FORM_XML_DOWNLOADED, request.user, interviewer, _("'%(interviewer)s' Downloaded XML for form '%(id_string)s'.") % { "interviewer": interviewer.name, "id_string": form_id }, audit, request) response = response_with_mimetype_and_name('xml', 'household_listing-%s' % survey.pk, show_date=False, full_mime='text/xml') response.content = householdlist_xform return response