示例#1
0
def new(request, household_id):
    member_form = HouseholdMemberForm()
    breadcrumbs = [('Households', reverse('list_household_page')),]
    try:
        household = Household.objects.get(id=household_id)
        breadcrumbs.append(('Household', reverse('view_household_page', args=(household_id, ))),)
        if request.method == 'POST':
            member_form = HouseholdMemberForm(data=request.POST)
            interviewer = household.last_registrar
            survey = SurveyAllocation.get_allocation(interviewer)
            if member_form.is_valid():
                household_member = member_form.save(commit=False)
                household_member.household = household
                household_member.registrar = household.last_registrar
                household_member.survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(interviewer,
                                                                                                      survey)
                household_member.registration_channel = WebAccess.choice_name()
                household_member.save()
                messages.success(request, 'Household member successfully created.')
                return HttpResponseRedirect('/households/%s/'%(str(household_id)))
    except Household.DoesNotExist:
        messages.error(request, 'There are  no households currently registered  for this ID.')
        return HttpResponseRedirect('/households/')
    request.breadcrumbs(breadcrumbs)
    return render(request, 'household_member/new.html', {'member_form': member_form, 'button_label': 'Create'})
示例#2
0
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()
示例#3
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')
示例#4
0
def process_submission(interviewer, xml_file, media_files=[], request=None):
    """
    extract surveys and for this xml file and for specified household member
    """
    media_files = dict([(os.path.basename(f.name), f) for f in media_files])
    reports = []
    xml_blob = xml_file.read()
    survey_tree = _get_tree_from_blob(xml_blob)
    form_id = _get_form_id(survey_tree)
    instance_id = _get_instance_id(survey_tree)
    description = ''
    if form_id.startswith('alloc-'):
        survey_allocation = _get_allocation(survey_tree)
        survey = survey_allocation.survey
    else:
        survey = _get_survey(survey_tree)
        survey_allocation = get_survey_allocation(interviewer)
    household = None
    member = None
    survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(
        interviewer, survey)
    if _get_form_type(survey_tree) == LISTING:
        description = LISTING_DESC
        households = save_household_list(interviewer, survey, survey_tree,
                                         survey_listing)
        survey_allocation.stage = SurveyAllocation.SURVEY
        survey_allocation.save()
        for household in households:
            submission = ODKSubmission.objects.create(interviewer=interviewer,
                                                      survey=survey,
                                                      form_id=form_id,
                                                      description=LISTING_DESC,
                                                      instance_id=instance_id,
                                                      household_member=member,
                                                      household=household,
                                                      xml=etree.tostring(
                                                          survey_tree,
                                                          pretty_print=True))
    elif _get_form_type(survey_tree) == NON_RESPONSE:
        description = NON_RESPONSE_DESC
        non_responses = save_nonresponse_answers(interviewer, survey,
                                                 survey_tree, survey_listing)
        for non_response in non_responses:
            submission = ODKSubmission.objects.create(
                interviewer=interviewer,
                survey=survey,
                form_id=form_id,
                description=NON_RESPONSE_DESC,
                instance_id=instance_id,
                household_member=member,
                household=non_response.household,
                xml=etree.tostring(survey_tree, pretty_print=True))
    else:
        description = SURVEY_DESC
        save_survey_questions.delay(survey_allocation, xml_blob, media_files)
    return SubmissionReport(form_id, instance_id, description)
示例#5
0
def save(request, instance=None):
    head = None
    if instance:
        handler = reverse('edit_household_page', args=(instance.pk, ))
        head = instance.get_head()
        heading = 'Edit Household'
        cancel_url = reverse('view_household_page', args=(instance.pk, ))
    else:
        handler = reverse('new_household_page')
        heading = 'New Household'
        cancel_url = reverse('list_household_page')
    locations_filter = LocationsFilterForm(data=request.GET, include_ea=True)
    householdform = HouseholdForm(instance=instance, eas=locations_filter.get_enumerations())
    headform = HouseholdHeadForm(instance=head)
    if request.method == 'POST':
        householdform = HouseholdForm(data=request.POST, instance=instance)
        headform = HouseholdHeadForm(data=request.POST, instance=head)
        if householdform.is_valid():
            household = householdform.save(commit=False)
            interviewer = household.last_registrar
            survey = SurveyAllocation.get_allocation(interviewer)
            if survey:
                survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(interviewer, survey)
                household.listing = survey_listing.listing
                household.save()
                householdform = HouseholdForm()
                # import pdb; pdb.set_trace()
                if headform.is_valid():
                    head = headform.save(commit=False)
                    head.household = household
                    head.registrar = interviewer
                    head.survey_listing = survey_listing
                    head.registration_channel = WebAccess.choice_name()
                    head.save()
                    if household.head_desc is not head.surname:
                        household.head_desc = head.surname
                        household.save()
                    messages.info(request, 'Household %s saved successfully' % household.house_number)
                    return HttpResponseRedirect(reverse('view_household_page', args=(household.pk, )))
                handler = reverse('new_household_page')
            else:
                messages.error(request, 'No open survey for %s' % interviewer.name)
    context = {
               'headform': headform,
               'householdform': householdform,
               'action': handler,
                'cancel_url' : cancel_url,
               'heading':heading,
               'id': "create-household-form",
               'button_label': "Save",
               'loading_text': "Creating...",
               'locations_filter' : locations_filter}
    request.breadcrumbs([
        ('Households', reverse('list_household_page')),
    ])
    return render(request, 'households/new.html', context)
示例#6
0
def _get_or_create_household_member(survey_allocation, survey_tree):
    interviewer, survey = survey_allocation.interviewer, survey_allocation.survey
    house_number = _get_household_house_number(survey_tree)
    survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(
        interviewer, survey)
    house_listing = survey_listing.listing
    try:
        household = Household.objects.get(listing=house_listing,
                                          house_number=house_number)
    except Household.DoesNotExist:
        physical_addr = ''
        try:
            physical_addr = _get_household_physical_addr(survey_tree)
        except IndexError:
            pass
        household = Household.objects.create(
            last_registrar=interviewer,
            listing=house_listing,
            registration_channel=ODKAccess.choice_name(),
            house_number=house_number,
            physical_address=physical_addr)
    #now time for member details
    MALE = '1'
    IS_HEAD = '1'
    mem_attrs = _get_member_attrs(survey_tree)
    kwargs = {}
    kwargs['surname'] = mem_attrs.get('surname')
    kwargs['first_name'] = mem_attrs['firstName']
    kwargs['gender'] = (mem_attrs['sex'] == MALE)
    date_of_birth = current_val = datetime.now() - relativedelta(
        years=int(mem_attrs['age']))
    kwargs['date_of_birth'] = date_of_birth
    #datetime.strptime(mem_attrs['dateOfBirth'], '%Y-%m-%d')
    kwargs['household'] = household
    kwargs['registrar'] = interviewer
    kwargs['registration_channel'] = ODKAccess.choice_name()
    kwargs['survey_listing'] = survey_listing
    if not household.get_head() and mem_attrs['isHead'] == IS_HEAD:
        # kwargs['occupation'] = mem_attrs.get('occupation', '')
        # kwargs['level_of_education'] = mem_attrs.get('levelOfEducation', '')
        # resident_since = datetime.strptime(mem_attrs.get('residentSince', '1900-01-01'), '%Y-%m-%d')
        # kwargs['resident_since']=resident_since
        head = HouseholdHead.objects.create(**kwargs)
        if household.head_desc is not head.surname:
            household.head_desc = head.surname
            household.save()
        return head
    else:
        return HouseholdMember.objects.create(**kwargs)
示例#7
0
def _get_or_create_household_member(interviewer, survey, survey_tree):
    house_number = _get_household_house_number(survey_tree)
    survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(interviewer, survey)
    house_listing = survey_listing.listing
    try:
        household = Household.objects.get(listing=house_listing, house_number=house_number)
    except Household.DoesNotExist:
        physical_addr = ""
        try:
            physical_addr = _get_household_physical_addr(survey_tree)
        except IndexError:
            pass
        household = Household.objects.create(
            last_registrar=interviewer,
            listing=house_listing,
            registration_channel=ODKAccess.choice_name(),
            house_number=house_number,
            physical_address=physical_addr,
        )
    # now time for member details
    MALE = "1"
    IS_HEAD = "1"
    mem_attrs = _get_member_attrs(survey_tree)
    kwargs = {}
    kwargs["surname"] = mem_attrs.get("surname")
    kwargs["first_name"] = mem_attrs["firstName"]
    kwargs["gender"] = mem_attrs["sex"] == MALE
    date_of_birth = current_val = datetime.now() - relativedelta(years=int(mem_attrs["age"]))
    kwargs["date_of_birth"] = date_of_birth
    # datetime.strptime(mem_attrs['dateOfBirth'], '%Y-%m-%d')
    kwargs["household"] = household
    kwargs["registrar"] = interviewer
    kwargs["registration_channel"] = ODKAccess.choice_name()
    kwargs["survey_listing"] = survey_listing
    if not household.get_head() and mem_attrs["isHead"] == IS_HEAD:
        # kwargs['occupation'] = mem_attrs.get('occupation', '')
        # kwargs['level_of_education'] = mem_attrs.get('levelOfEducation', '')
        # resident_since = datetime.strptime(mem_attrs.get('residentSince', '1900-01-01'), '%Y-%m-%d')
        # kwargs['resident_since']=resident_since
        head = HouseholdHead.objects.create(**kwargs)
        if household.head_desc is not head.surname:
            household.head_desc = head.surname
            household.save()
        return head
    else:
        return HouseholdMember.objects.create(**kwargs)
示例#8
0
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
示例#9
0
def new(request, household_id):
    member_form = HouseholdMemberForm()
    breadcrumbs = [
        ('Households', reverse('list_household_page')),
    ]
    try:
        household = Household.objects.get(id=household_id)
        breadcrumbs.append(
            ('Household', reverse('view_household_page',
                                  args=(household_id, ))), )
        if request.method == 'POST':
            member_form = HouseholdMemberForm(data=request.POST)
            interviewer = household.last_registrar
            survey = SurveyAllocation.get_allocation(interviewer)
            if member_form.is_valid():
                household_member = member_form.save(commit=False)
                household_member.household = household
                household_member.registrar = household.last_registrar
                household_member.survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(
                    interviewer, survey)
                household_member.registration_channel = WebAccess.choice_name()
                household_member.save()
                messages.success(request,
                                 'Household member successfully created.')
                return HttpResponseRedirect('/households/%s/' %
                                            (str(household_id)))
    except Household.DoesNotExist:
        messages.error(
            request,
            'There are  no households currently registered  for this ID.')
        return HttpResponseRedirect('/households/')
    request.breadcrumbs(breadcrumbs)
    return render(request, 'household_member/new.html', {
        'member_form': member_form,
        'button_label': 'Create'
    })
示例#10
0
def process_submission(interviewer, xml_file, media_files=[], request=None):
    """
    extract surveys and for this xml file and for specified household member
    """
    media_files = dict([(os.path.basename(f.name), f) for f in media_files])
    reports = []
    survey_tree = _get_tree(xml_file)
    form_id = _get_form_id(survey_tree)
    survey = _get_survey(survey_tree)
    survey_allocation = get_survey_allocation(interviewer)
    household = None
    member = None
    survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(interviewer, survey)
    if _get_form_type(survey_tree) == LISTING:
        households = save_household_list(interviewer, survey, survey_tree, survey_listing)
        survey_allocation.stage = SurveyAllocation.SURVEY
        survey_allocation.save()
        for household in households:
            submission = ODKSubmission.objects.create(
                interviewer=interviewer,
                survey=survey,
                form_id=form_id,
                description=LISTING_DESC,
                instance_id=_get_instance_id(survey_tree),
                household_member=member,
                household=household,
                xml=etree.tostring(survey_tree, pretty_print=True),
            )
    elif _get_form_type(survey_tree) == NON_RESPONSE:
        non_responses = save_nonresponse_answers(interviewer, survey, survey_tree, survey_listing)
        for non_response in non_responses:
            submission = ODKSubmission.objects.create(
                interviewer=interviewer,
                survey=survey,
                form_id=form_id,
                description=NON_RESPONSE_DESC,
                instance_id=_get_instance_id(survey_tree),
                household_member=member,
                household=non_response.household,
                xml=etree.tostring(survey_tree, pretty_print=True),
            )
    else:
        member = _get_or_create_household_member(interviewer, survey, survey_tree)
        response_dict = _get_responses(interviewer, survey_tree, survey)
        treated_batches = {}
        interviews = {}
        if response_dict:
            for (b_id, q_id), answer in response_dict.items():
                question = Question.objects.get(pk=q_id)
                batch = treated_batches.get(b_id, Batch.objects.get(pk=b_id))
                if answer is not None:
                    if question.answer_type in [
                        AudioAnswer.choice_name(),
                        ImageAnswer.choice_name(),
                        VideoAnswer.choice_name(),
                    ]:
                        answer = media_files.get(answer, None)
                    interview = interviews.get(b_id, None)
                    if interview is None:
                        interview, _ = Interview.objects.get_or_create(
                            interviewer=interviewer,
                            householdmember=member,
                            batch=batch,
                            interview_channel=interviewer.odk_access[0],
                            ea=interviewer.ea,
                        )

                        interviews[b_id] = interview
                    created = record_interview_answer(interview, question, answer)
                if b_id not in treated_batches.keys():
                    treated_batches[b_id] = batch
            map(
                lambda batch: member.batch_completed(batch), treated_batches.values()
            )  # create batch completion for question batches
            member.survey_completed()
            if member.household.has_completed(survey):
                map(lambda batch: member.household.batch_completed(batch, interviewer), treated_batches.values())
                member.household.survey_completed(survey, interviewer)
        household = member.household
        submission = ODKSubmission.objects.create(
            interviewer=interviewer,
            survey=survey,
            form_id=form_id,
            description=SURVEY_DESC,
            instance_id=_get_instance_id(survey_tree),
            household_member=member,
            household=household,
            xml=etree.tostring(survey_tree, pretty_print=True),
        )
        #    execute.delay(submission.save_attachments, media_files)
        submission.save_attachments(media_files.values())
    return submission
示例#11
0
def save(request, instance=None):
    head = None
    if instance:
        handler = reverse('edit_household_page', args=(instance.pk, ))
        head = instance.get_head()
        heading = 'Edit Household'
        cancel_url = reverse('view_household_page', args=(instance.pk, ))
    else:
        handler = reverse('new_household_page')
        heading = 'New Household'
        cancel_url = reverse('list_household_page')
    locations_filter = LocationsFilterForm(data=request.GET, include_ea=True)
    householdform = HouseholdForm(instance=instance,
                                  eas=locations_filter.get_enumerations())
    headform = HouseholdHeadForm(instance=head)
    if request.method == 'POST':
        householdform = HouseholdForm(data=request.POST, instance=instance)
        headform = HouseholdHeadForm(data=request.POST, instance=head)
        if householdform.is_valid():
            household = householdform.save(commit=False)
            interviewer = household.last_registrar
            survey = SurveyAllocation.get_allocation(interviewer)
            if survey:
                survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(
                    interviewer, survey)
                household.listing = survey_listing.listing
                household.save()
                householdform = HouseholdForm()
                # import pdb; pdb.set_trace()
                if headform.is_valid():
                    head = headform.save(commit=False)
                    head.household = household
                    head.registrar = interviewer
                    head.survey_listing = survey_listing
                    head.registration_channel = WebAccess.choice_name()
                    head.save()
                    if household.head_desc is not head.surname:
                        household.head_desc = head.surname
                        household.save()
                    messages.info(
                        request, 'Household %s saved successfully' %
                        household.house_number)
                    return HttpResponseRedirect(
                        reverse('view_household_page', args=(household.pk, )))
                handler = reverse('new_household_page')
            else:
                messages.error(request,
                               'No open survey for %s' % interviewer.name)
    context = {
        'headform': headform,
        'householdform': householdform,
        'action': handler,
        'cancel_url': cancel_url,
        'heading': heading,
        'id': "create-household-form",
        'button_label': "Save",
        'loading_text': "Creating...",
        'locations_filter': locations_filter
    }
    request.breadcrumbs([
        ('Households', reverse('list_household_page')),
    ])
    return render(request, 'households/new.html', context)
示例#12
0
 def survey_listing(self):
     return SurveyHouseholdListing.get_or_create_survey_listing(
         self.interviewer, self.ongoing_survey)