Пример #1
0
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')
Пример #2
0
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")