Пример #1
0
 def __init__(self, *args, **kwargs):
     include_ea = kwargs.pop('include_ea', False)
     super(LocationsFilterForm, self).__init__(*args, **kwargs)
     data = self.data
     if not isinstance(self.data, QueryDict):
         self.data = QueryDict('', mutable=True)
         self.data.update(data)
     self.data._mutable = True
     last_selected_pk = None
     largest_unit = LocationType.largest_unit()
     locations = Location.objects.none()
     for location_type in LocationType.in_between():
         kw = {'type': location_type}
         parent_selection = data.get(location_type.parent.name, None)
         if (locations
                 and parent_selection) or location_type == largest_unit:
             if parent_selection:
                 last_selected_pk = data.get(location_type.name,
                                             None) or parent_selection
                 kw['parent__pk'] = parent_selection
             locations = Location.objects.filter(**kw).only(
                 'id', 'name').order_by('name')
         else:
             self.data[location_type.name] = ''
             locations = Location.objects.none()
         # choices = [(loc.pk, loc.name) for loc in locations]
         # choices.insert(0, ('', '--- Select %s ---' % location_type.name))
         self.fields[location_type.name] = forms.ModelChoiceField(
             queryset=locations)  # forms.ChoiceField(choices=choices)
         self.fields[location_type.name].required = False
         self.fields[
             location_type.
             name].empty_label = '-- Select %s --' % location_type.name
         self.fields[location_type.name].widget.attrs[
             'class'] = 'location_filter ea_filters chzn-select'
         # self.fields[location_type.name].widget.attrs['style'] = 'width: 100px;'
     if last_selected_pk:
         self.last_location_selected = Location.objects.get(
             pk=last_selected_pk)
     if include_ea:
         if self.last_location_selected:
             eas = EnumerationArea.objects.filter(
                 locations__in=get_leaf_locs(loc=self.last_location_selected
                                             )).distinct().order_by('name')
         else:
             eas = EnumerationArea.objects.none()
         choices = [(ea.pk, ea.name) for ea in eas]
         choices.insert(0, ('', '--- Select EA ---'))
         self.fields['enumeration_area'] = forms.ModelChoiceField(
             queryset=eas)  # ChoiceField(choices=choices)
         self.fields['enumeration_area'].widget.attrs[
             'class'] = 'location_filter chzn-select'
         # self.fields['enumeration_area'].widget.attrs['style'] = 'width: 100px;'
         self.fields['enumeration_area'].required = False
     self.data._mutable = False
Пример #2
0
def list_households(request):
    locations_filter = LocationsFilterForm(data=request.GET, include_ea=True)
    enumeration_areas = locations_filter.get_enumerations()
    households = Household.objects.filter(listing__ea__in=enumeration_areas).order_by('house_number')
    search_fields = ['house_number', 'listing__ea__name', 'last_registrar__name', 'listing__initial_survey__name', ]
    if request.GET.has_key('q'):
        households = get_filterset(households, request.GET['q'], search_fields)
    return render(request, 'households/index.html',
                  {'households': households, 
                   'locations_filter' : locations_filter, 
                   'location_filter_types' : LocationType.in_between(),
                   'Largest Unit' : LocationType.largest_unit(),
                   'placeholder': 'house no, ea, survey, interviewer',
                   'request': request})
Пример #3
0
 def __init__(self, *args, **kwargs):
     include_ea = kwargs.pop('include_ea', False)
     super(LocationsFilterForm, self).__init__(*args, **kwargs)
     data = self.data
     if not isinstance(self.data, QueryDict):
         self.data = QueryDict('', mutable=True)
         self.data.update(data)
     self.data._mutable = True
     last_selected_pk = None
     largest_unit = LocationType.largest_unit()
     locations = Location.objects.none()
     for location_type in LocationType.in_between():
         kw = {'type': location_type}
         parent_selection = data.get(location_type.parent.name, None)
         if (locations and parent_selection) or location_type == largest_unit:
             if parent_selection:
                 last_selected_pk = data.get(
                     location_type.name, None) or parent_selection
                 kw['parent__pk'] = parent_selection
             locations = Location.objects.filter(**kw).only('id', 'name').order_by('name')
         else:
             self.data[location_type.name] = ''
             locations = Location.objects.none()
         # choices = [(loc.pk, loc.name) for loc in locations]
         # choices.insert(0, ('', '--- Select %s ---' % location_type.name))
         self.fields[location_type.name] = forms.ModelChoiceField(
             queryset=locations)  # forms.ChoiceField(choices=choices)
         self.fields[location_type.name].required = False
         self.fields[location_type.name].empty_label = '-- Select %s --' % location_type.name
         self.fields[location_type.name].widget.attrs['class'] = 'location_filter ea_filters chzn-select'
         # self.fields[location_type.name].widget.attrs['style'] = 'width: 100px;'
     if last_selected_pk:
         self.last_location_selected = Location.objects.get(pk=last_selected_pk)
     if include_ea:
         if self.last_location_selected:
             eas = EnumerationArea.objects.filter(
                 locations__in=get_leaf_locs(loc=self.last_location_selected)).distinct().order_by('name')
         else:
             eas = EnumerationArea.objects.none()
         choices = [(ea.pk, ea.name) for ea in eas]
         choices.insert(0, ('', '--- Select EA ---'))
         self.fields['enumeration_area'] = forms.ModelChoiceField(
             queryset=eas)  # ChoiceField(choices=choices)
         self.fields['enumeration_area'].widget.attrs[
             'class'] = 'location_filter chzn-select'
         # self.fields['enumeration_area'].widget.attrs['style'] = 'width: 100px;'
         self.fields['enumeration_area'].required = False
     self.data._mutable = False
Пример #4
0
def show(request, survey_id, batch_id):
    batch = Batch.objects.get(id=batch_id)
    prime_location_type = LocationType.largest_unit()
    locations = Location.objects.filter(
        type=prime_location_type).order_by('name')
    batch_location_ids = batch.open_locations.values_list('location_id',
                                                          flat=True)
    if request.GET.has_key('status'):
        if request.GET['status'] == 'open':
            locations = locations.filter(id__in=batch_location_ids)
        else:
            locations = locations.exclude(id__in=batch_location_ids)
    request.breadcrumbs([
        ('Surveys', reverse('survey_list_page')),
        (batch.survey.name,
         reverse('batch_index_page', args=(batch.survey.pk, ))),
    ])
    open_locations = locations.filter(id__in=batch_location_ids)
    context = {
        'batch': batch,
        'locations': locations,
        'open_locations': open_locations,
        'non_response_active_locations':
        batch.get_non_response_active_locations()
    }
    return render(request, 'batches/show.html', context)
def edit(request, ea_id):
    ea = get_object_or_404(EnumerationArea, pk=ea_id)
    if request.GET:
        data = request.GET
    else:
        data = dict([(loc.type.name, loc.pk) for loc in ea.parent_locations()])
    locations_filter = LocationsFilterForm(data=data)
    enumeration_area_form = EnumerationAreaForm(
        instance=ea, locations=locations_filter.get_locations())
    if request.method == 'POST':
        enumeration_area_form = EnumerationAreaForm(
            data=request.POST,
            instance=ea,
            locations=locations_filter.get_locations())

        if enumeration_area_form.is_valid():
            enumeration_area_form.save()
            messages.success(request, "Enumeration Area successfully Changed.")
            return HttpResponseRedirect(
                reverse('enumeration_area_home', args=()))
        messages.error(request, "Enumeration area was not Changed.")
    request.breadcrumbs([
        ('Enumeration Areas', reverse('enumeration_area_home')),
    ])
    return render(
        request, 'enumeration_area/new.html', {
            'enumeration_area_form': enumeration_area_form,
            'locations_filter': locations_filter,
            'title': 'New Enumeration Area',
            'button_label': 'Create',
            'action': reverse('edit_enumeration_area_page', args=(ea_id, )),
            'location_filter_types': LocationType.in_between(),
        })
def new(request):
    locations_filter = LocationsFilterForm(data=request.GET)
    enumeration_area_form = EnumerationAreaForm(
        locations=locations_filter.get_locations())
    if request.method == 'POST':
        enumeration_area_form = EnumerationAreaForm(data=request.POST)
        if enumeration_area_form.is_valid():
            enumeration_area_form.save()
            messages.success(request, "Enumeration Area successfully created.")
            return HttpResponseRedirect(
                reverse('enumeration_area_home', args=()))
        messages.error(request, "Enumeration area was not created.")
    request.breadcrumbs([
        ('Enumeration Areas', reverse('enumeration_area_home')),
    ])
    return render(
        request, 'enumeration_area/new.html', {
            'enumeration_area_form': enumeration_area_form,
            'locations_filter': locations_filter,
            'title': 'New Enumeration Area',
            'button_label': 'Create',
            'action': reverse('new_enumeration_area_page', args=()),
            'location_filter_types': LocationType.in_between(),
            'redirect_url': '/enumeration_area'
        })
Пример #7
0
 def get_parent(self):
     largest_loc_type = LocationType.largest_unit()
     locations = Location.objects.filter(
         type=largest_loc_type).order_by('name')
     return {
         largest_loc_type.slug: locations
     }  #self.sorted_by_hierarchy({largest_loc_type.slug: locations }) if locations else {}
Пример #8
0
 def formatted_responses(self):
     _formatted_responses = []
     headers = [loc_type.name.upper() for loc_type in
                LocationType.objects.exclude(name__iexact='country')
                if loc_type != LocationType.smallest_unit()]
     headers.extend([entry.upper() for entry in self.HEADERS])
     _formatted_responses.append(','.join(headers))
     interviewer_records = []
     for interviewer in self.interviewers:
         info = {}
         info['mobile_numbers'] = ','.join(
             [access.user_identifier for access in interviewer.ussd_access])
         info['odk_id'] = ','.join(
             [access.user_identifier for access in interviewer.odk_access])
         row = [str(loc.name) for loc in interviewer.ea.parent_locations()]
         row.extend(['"%s"' % str(getattr(
             interviewer,
             entry,
             info.get(entry, ''))) for entry in self.HEADERS])
         interviewer_records.append(row)
     interviewer_records = sorted(
         interviewer_records, key=operator.itemgetter(*range(len(headers))))
     _formatted_responses.extend(
         map(lambda row: ','.join(row), interviewer_records))
     return _formatted_responses
Пример #9
0
 def formatted_responses(self):
     _formatted_responses = []
     headers = [
         loc_type.name.upper() for loc_type in LocationType.objects.exclude(
             name__iexact='country')
         if loc_type != LocationType.smallest_unit()
     ]
     headers.extend([entry.upper() for entry in self.HEADERS])
     _formatted_responses.append(','.join(headers))
     interviewer_records = []
     for interviewer in self.interviewers:
         info = {}
         info['mobile_numbers'] = ','.join(
             [access.user_identifier for access in interviewer.ussd_access])
         info['odk_id'] = ','.join(
             [access.user_identifier for access in interviewer.odk_access])
         row = [str(loc.name) for loc in interviewer.ea.parent_locations()]
         row.extend([
             '"%s"' % str(getattr(interviewer, entry, info.get(entry, '')))
             for entry in self.HEADERS
         ])
         interviewer_records.append(row)
     interviewer_records = sorted(
         interviewer_records, key=operator.itemgetter(*range(len(headers))))
     _formatted_responses.extend(
         map(lambda row: ','.join(row), interviewer_records))
     return _formatted_responses
Пример #10
0
def new(request):
    locations_filter = LocationsFilterForm(data=request.GET)
    enumeration_area_form = EnumerationAreaForm(
        locations=locations_filter.get_locations())
    if request.method == 'POST':
        enumeration_area_form = EnumerationAreaForm(data=request.POST)
        if enumeration_area_form.is_valid():
            enumeration_area_form.save()
            messages.success(request, "Enumeration Area successfully created.")
            return HttpResponseRedirect(
                reverse('enumeration_area_home', args=()))
        messages.error(request, "Enumeration area was not created.")
    request.breadcrumbs([
        ('Enumeration Areas', reverse('enumeration_area_home')),
    ])
    return render(request,
                  'enumeration_area/new.html',
                  {'enumeration_area_form': enumeration_area_form,
                   'locations_filter': locations_filter,
                   'title': 'New Enumeration Area',
                   'button_label': 'Create',
                   'action': reverse('new_enumeration_area_page',
                                     args=()),
                   'location_filter_types': LocationType.in_between(),
                   'redirect_url': '/enumeration_area'})
Пример #11
0
def _view_qset_data(request,
                    model_class,
                    interviews,
                    title,
                    disabled_fields=[]):
    params = request.GET if request.method == 'GET' else request.POST
    survey_filter = SurveyResultsFilterForm(model_class,
                                            disabled_fields=disabled_fields,
                                            data=params)
    locations_filter = LocationsFilterForm(data=request.GET, include_ea=True)
    selected_qset = None
    survey = None
    items_per_page = int(params.get('max_display_per_page', 50))
    try:
        page_index = int(params.get('page', 1)) - 1
    except BaseException:
        page_index = 0
    if survey_filter.is_valid():
        interviews = survey_filter.get_interviews(interviews=interviews)
        selected_qset = survey_filter.cleaned_data['question_set']
        survey = survey_filter.cleaned_data['survey']
    if locations_filter.is_valid():
        interviews = interviews.filter(
            ea__in=locations_filter.get_enumerations())
    search_fields = [
        'ea__name',
        'survey__name',
        'question_set__name',
        'answer__as_text',
    ]
    if 'q' in request.GET:
        interviews = get_filterset(interviews, request.GET['q'], search_fields)
    context = {
        'survey_filter': survey_filter,
        'interviews': interviews,
        'locations_filter': locations_filter,
        'location_filter_types': LocationType.in_between(),
        'placeholder': 'Response, EA, Survey, %s' % model_class.verbose_name(),
        'selected_qset': selected_qset,
        'model_class': model_class,
        'items_per_page': items_per_page,
        'max_display_per_page': items_per_page,
        'title': title
    }
    if selected_qset and survey:
        # page_start = page_index * items_per_page
        # interviews = interviews[page_start: page_start + items_per_page]()
        download_service = ResultsDownloadService(
            selected_qset,
            survey=survey,
            interviews=interviews,
            page_index=page_index,
            items_per_page=items_per_page)
        df = download_service.get_interview_answers()
        context['report'] = mark_safe(
            df.to_html(classes='table table-striped\
                    dataTable table-bordered table-hover table-sort',
                       max_rows=items_per_page))
    return render(request, 'question_set/view_all_data.html', context)
Пример #12
0
 def clean_headers_location_type_order(self, headers):
     headers = UploadService.remove_trailing('Name', in_array=headers, exclude='Code')
     headers = [header.upper() for header in headers]
     if headers[0] == 'COUNTRY':
         headers.pop(0)
     location_types = [loc.name.upper() for loc in LocationType.all().exclude(name__iexact='COUNTRY')]
     if not location_types == headers:
         raise ValidationError('Location types not in order. Please refer to input file format.')
Пример #13
0
def _create_or_edit(request, action_text, interviewer=None):
    request.breadcrumbs([
        ('Interviewers', reverse('interviewers_page')),
    ])
    title = 'New Interviewer'
    odk_instance = None
    data = request.GET
    if request.POST and request.POST.get('ea'):
        ea = get_object_or_404(EnumerationArea, pk=request.POST['ea'])
        data = dict([(loc.type.name, loc.pk) for loc in ea.parent_locations()])
    if interviewer:
        extra = 0
        title = 'Edit Interviewer'
        odk_accesses = interviewer.odk_access
        if odk_accesses.exists():
            odk_instance = odk_accesses[0]
        data = data or dict([(loc.type.name, loc.pk) for loc in interviewer.ea.parent_locations()])
    else:
        extra = 1
    locations_filter = LocationsFilterForm(data=data)
    if data:
        eas = locations_filter.get_enumerations()
    else:
        eas = EnumerationArea.objects.none()
    USSDAccessFormSet = inlineformset_factory(Interviewer, USSDAccess, form=USSDAccessForm, extra=extra)
    ussd_access_form = USSDAccessFormSet(prefix='ussd_access', instance=interviewer)
    response = None
    redirect_url = reverse('interviewers_page')
    odk_access_form = ODKAccessForm(instance=odk_instance)
    if request.method == 'POST':
        interviewer_form = InterviewerForm(eas, data=request.POST, instance=interviewer)
        ussd_access_form = USSDAccessFormSet(request.POST, prefix='ussd_access', instance=interviewer)
#         odk_access_form = ODKAccessFormSet(request.POST, prefix='odk_access', instance=interviewer)
        odk_access_form = ODKAccessForm(request.POST, instance=odk_instance)
        if interviewer_form.is_valid() and ussd_access_form.is_valid() and odk_access_form.is_valid():
            interviewer = interviewer_form.save()
            ussd_access_form.instance = interviewer
            ussd_access_form.save()
            odk_access = odk_access_form.save(commit=False) 
            odk_access.interviewer = interviewer
            odk_access.save()
            messages.success(request, "Interviewer successfully %sed." % action_text )
            return HttpResponseRedirect(redirect_url)
    else:
        interviewer_form = InterviewerForm(eas, instance=interviewer)
    loc_types = LocationType.in_between()
    return response or render(request, 'interviewers/interviewer_form.html', {'country_phone_code': COUNTRY_PHONE_CODE,
                                                                  'form': interviewer_form,
                                                                  'ussd_access_form' : ussd_access_form,
                                                                  'odk_access_form' : odk_access_form, 
                                                                  'title': title,
                                                                  'id': "create-interviewer-form",
                                                                  'class': 'interviewer-form',
                                                                  'button_label': "Save",
                                                                  'cancel_url': redirect_url,
                                                                  'locations_filter': locations_filter,
                                                                  'location_filter_types' : loc_types,
                                                                  'loading_text': "Creating..."})
Пример #14
0
def download(request):
    survey_batch_filter_form = SurveyBatchFilterForm(data=request.GET)
    locations_filter = LocationsFilterForm(data=request.GET)
    last_selected_loc = locations_filter.last_location_selected
    if request.GET and request.GET.get('action'):
        survey_batch_filter_form = SurveyBatchFilterForm(data=request.GET)
        if survey_batch_filter_form.is_valid():
            batch = survey_batch_filter_form.cleaned_data['batch']
            survey = survey_batch_filter_form.cleaned_data['survey']
            multi_option = survey_batch_filter_form.cleaned_data[
                'multi_option']
            restricted_to = None
            if last_selected_loc:
                restricted_to = [
                    last_selected_loc,
                ]
            if request.GET.get('action') == 'Email Spreadsheet':
                composer = ResultComposer(
                    request.user,
                    ResultsDownloadService(batch=batch,
                                           survey=survey,
                                           restrict_to=restricted_to,
                                           multi_display=multi_option))
                send_mail.delay(composer)
                messages.warning(
                    request,
                    "Email would be sent to you shortly. This could take a while."
                )
            else:
                download_service = ResultsDownloadService(
                    batch=batch,
                    survey=survey,
                    restrict_to=restricted_to,
                    multi_display=multi_option)
                file_name = '%s%s' % (
                    '%s-%s-' %
                    (last_selected_loc.type.name, last_selected_loc.name)
                    if last_selected_loc else '',
                    batch.name if batch else survey.name)
                generate_result_link.delay(request.user, download_service,
                                           file_name)
                messages.warning(
                    request,
                    "Download is in progress. Download link would be available to you shortly"
                )
    loc_types = LocationType.in_between()
    return render(
        request, 'aggregates/download_excel.html', {
            'survey_batch_filter_form':
            survey_batch_filter_form,
            'locations_filter':
            locations_filter,
            'export_url':
            '%s?%s' % (reverse('excel_report'), request.META['QUERY_STRING']),
            'location_filter_types':
            loc_types
        })
Пример #15
0
def all_locs(request, batch_id):
    batch = Batch.objects.get(id=batch_id)
    action = request.POST['action']
    locations = Location.objects.filter(type=LocationType.largest_unit()).order_by('name')
    if action.lower() == 'open all':
        for location in locations:
            batch.open_for_location(location)
    if action.lower() == 'close all':
        for location in locations:
            batch.close_for_location(location)
    return HttpResponseRedirect(reverse('batch_show_page', args=(batch.survey.id, batch_id, )))
Пример #16
0
def _view_qset_data(request, model_class, interviews,title, disabled_fields=[]):
    params = request.GET if request.method == 'GET' else request.POST
    survey_filter = SurveyResultsFilterForm(
        model_class, disabled_fields=disabled_fields, data=params)
    locations_filter = LocationsFilterForm(data=request.GET, include_ea=True)
    selected_qset = None
    survey = None
    items_per_page = int(params.get('max_display_per_page', 50))
    try:
        page_index = int(params.get('page', 1)) - 1
    except BaseException:
        page_index = 0
    if survey_filter.is_valid():
        interviews = survey_filter.get_interviews(interviews=interviews)
        selected_qset = survey_filter.cleaned_data['question_set']
        survey = survey_filter.cleaned_data['survey']
    if locations_filter.is_valid():
        interviews = interviews.filter(ea__in=locations_filter.get_enumerations())
    search_fields = [
        'ea__name',
        'survey__name',
        'question_set__name',
        'answer__as_text',
    ]
    if 'q' in request.GET:
        interviews = get_filterset(interviews, request.GET['q'], search_fields)
    context = {
        'survey_filter': survey_filter,
        'interviews': interviews,
        'locations_filter': locations_filter,
        'location_filter_types': LocationType.in_between(),
        'placeholder': 'Response, EA, Survey, %s' % model_class.verbose_name(),
        'selected_qset': selected_qset,
        'model_class': model_class,
        'items_per_page': items_per_page,
        'max_display_per_page': items_per_page,
        'title':title}
    if selected_qset and survey:
        # page_start = page_index * items_per_page
        # interviews = interviews[page_start: page_start + items_per_page]()
        download_service = ResultsDownloadService(
            selected_qset,
            survey=survey,
            interviews=interviews,
            page_index=page_index,
            items_per_page=items_per_page)
        df = download_service.get_interview_answers()
        context['report'] = mark_safe(
            df.to_html(
                classes='table table-striped\
                    dataTable table-bordered table-hover table-sort',
                max_rows=items_per_page))
    return render(request, 'question_set/view_all_data.html', context)
Пример #17
0
def add_to_redis():
    r_server = redis.Redis()
    survey_all = Survey.objects.all().values_list("id")
    for id in survey_all:
        key = settings.SURVEY_REDIS_KEY%{'survey_id':str(id[0])}
        survey = Survey.objects.get(id=id[0])
        location_type = LocationType.largest_unit()
        expire_time = (settings.RESULT_REFRESH_FREQ*60+30)*60
        completion_rates = BatchSurveyCompletionRates(location_type).get_completion_formatted_for_json(survey)
        json_dump = json.dumps(completion_rates, cls=DjangoJSONEncoder)
        r_server.set(key, json_dump)
        r_server.expire(key,expire_time)
Пример #18
0
def index(request):
    locations_filter = LocationsFilterForm(data=request.GET)
    enumeration_areas = locations_filter.get_enumerations()
    search_fields = ['name', 'locations__name', ]
    if 'q' in request.GET:
        enumeration_areas = get_filterset(
            enumeration_areas, request.GET['q'], search_fields)
    loc_types = LocationType.in_between()
    context = {'enumeration_areas': enumeration_areas,
               'locations_filter': locations_filter,
               'location_filter_types': loc_types,
               'placeholder': 'name, location name'}
    return render(request, "enumeration_area/index.html", context)
Пример #19
0
def index(request):
    locations_filter = LocationsFilterForm(data=request.GET)
    enumeration_areas = locations_filter.get_enumerations()
    search_fields = ['name', 'locations__name', ]
    if request.GET.has_key('q'):
        enumeration_areas = get_filterset(enumeration_areas, request.GET['q'], search_fields)
    loc_types = LocationType.in_between()
    context = {'enumeration_areas': enumeration_areas,
               'locations_filter' : locations_filter,
               'location_filter_types' : loc_types,
               'placeholder': 'name, location name',
               'max_display_per_page': settings.MAX_DISPLAY_PER_PAGE}
    return render(request, "enumeration_area/index.html", context)
Пример #20
0
def index(request):
    locations_filter = LocationsFilterForm(data=request.GET)
    enumeration_areas = locations_filter.get_enumerations()
    search_fields = ['name', 'locations__name', ]
    if request.GET.has_key('q'):
        enumeration_areas = get_filterset(enumeration_areas, request.GET['q'], search_fields)
    loc_types = LocationType.in_between()
    context = {'enumeration_areas': enumeration_areas,
               'locations_filter' : locations_filter,
               'location_filter_types' : loc_types,
               'placeholder': 'name, location name',
               'max_display_per_page': settings.MAX_DISPLAY_PER_PAGE}
    return render(request, "enumeration_area/index.html", context)
Пример #21
0
def list_households(request):
    locations_filter = LocationsFilterForm(data=request.GET, include_ea=True)
    enumeration_areas = locations_filter.get_enumerations()
    households = Household.objects.filter(
        listing__ea__in=enumeration_areas).order_by('house_number')
    search_fields = [
        'house_number',
        'listing__ea__name',
        'last_registrar__name',
        'listing__initial_survey__name',
    ]
    if request.GET.has_key('q'):
        households = get_filterset(households, request.GET['q'], search_fields)
    return render(
        request, 'households/index.html', {
            'households': households,
            'locations_filter': locations_filter,
            'location_filter_types': LocationType.in_between(),
            'Largest Unit': LocationType.largest_unit(),
            'placeholder': 'house no, ea, survey, interviewer',
            'request': request
        })
Пример #22
0
 def test_get_download_analysis_page(self):
     indicator, data = self._test_create_indicator()
     self.assertEquals(self.qset.id, indicator.question_set.id)
     self._bulk_answer_questions()
     url = reverse('download_indicator_analysis', args=(indicator.id, ))
     response = self.client.get(url)
     self.assertTrue(response.status_code, 200)
     variables = IndicatorVariable.objects.all()
     response_text = response.content.lower()
     for variable in variables:
         self.assertIn(variable.name, response_text)
     for location in Location.objects.filter(type=LocationType.largest_unit()):
         self.assertIn(location.name.lower(), response_text)
 def test_get_download_analysis_page(self):
     indicator, data = self._test_create_indicator()
     self.assertEquals(self.qset.id, indicator.question_set.id)
     self._bulk_answer_questions()
     url = reverse('download_indicator_analysis', args=(indicator.id, ))
     response = self.client.get(url)
     self.assertTrue(response.status_code, 200)
     variables = IndicatorVariable.objects.all()
     response_text = response.content.lower()
     for variable in variables:
         self.assertIn(variable.name, response_text)
     for location in Location.objects.filter(
             type=LocationType.largest_unit()):
         self.assertIn(location.name.lower(), response_text)
Пример #24
0
 def clean_headers_location_type_order(self, headers):
     headers = UploadService.remove_trailing('Name',
                                             in_array=headers,
                                             exclude='Code')
     headers = [header.upper() for header in headers]
     if headers[0] == 'COUNTRY':
         headers.pop(0)
     location_types = [
         loc.name.upper()
         for loc in LocationType.all().exclude(name__iexact='COUNTRY')
     ]
     if not location_types == headers:
         raise ValidationError(
             'Location types not in order. Please refer to input file format.'
         )
Пример #25
0
 def get_locations(self):
     status = None
     try:
         if self.is_valid():
             status = int(self.cleaned_data.get('status') or 0)
     except forms.ValidationError:
         pass
     prime_location_type = LocationType.largest_unit()
     locations = Location.objects.filter(type=prime_location_type).order_by('name')
     batch_location_ids = self.batch.open_locations.values_list('location_id', flat=True)
     if status and status == self.OPEN:
         return locations.filter(id__in=batch_location_ids)
     elif status and status == self.CLOSED:
         return locations.exclude(id__in=batch_location_ids)
     return locations
Пример #26
0
def all_locs(request, batch_id):
    batch = Batch.objects.get(id=batch_id)
    action = request.POST['action']
    locations = Location.objects.filter(
        type=LocationType.largest_unit()).order_by('name')
    if action.lower() == 'open all':
        for location in locations:
            batch.open_for_location(location)
    if action.lower() == 'close all':
        for location in locations:
            batch.close_for_location(location)
    return HttpResponseRedirect(
        reverse('batch_show_page', args=(
            batch.survey.id,
            batch_id,
        )))
Пример #27
0
 def get_locations(self):
     status = None
     try:
         if self.is_valid():
             status = int(self.cleaned_data.get('status') or 0)
     except forms.ValidationError:
         pass
     prime_location_type = LocationType.largest_unit()
     locations = Location.objects.filter(
         type=prime_location_type).order_by('name')
     batch_location_ids = self.batch.open_locations.values_list(
         'location_id', flat=True)
     if status and status == self.OPEN:
         return locations.filter(id__in=batch_location_ids)
     elif status and status == self.CLOSED:
         return locations.exclude(id__in=batch_location_ids)
     return locations
Пример #28
0
def index(request):
    locations_filter = LocationsFilterForm(data=request.GET)
    enumeration_areas = locations_filter.get_enumerations()
    search_fields = [
        'name',
        'locations__name',
    ]
    if 'q' in request.GET:
        enumeration_areas = get_filterset(enumeration_areas, request.GET['q'],
                                          search_fields)
    loc_types = LocationType.in_between()
    context = {
        'enumeration_areas': enumeration_areas,
        'locations_filter': locations_filter,
        'location_filter_types': loc_types,
        'placeholder': 'name, location name'
    }
    return render(request, "enumeration_area/index.html", context)
Пример #29
0
 def get_result_json():
     """Basically if survey is sampled and we are\
         now in batch collection phase, display percentage completion.
     For listing or census data collection, just show count
     :return:
     """
     survey = get_object_or_404(Survey, pk=survey_id)
     country_type = LocationType.objects.get(parent__isnull=True)
     hierachy_count = country_type.get_descendant_count()
     if hasattr(settings, 'MAP_ADMIN_LEVEL'):
         location_type = country_type.get_descendants()[settings.MAP_ADMIN_LEVEL - 1]
     else:
         location_type = LocationType.largest_unit()
     divider = 1.0
     completion_rates = {}
     has_sampling = survey.has_sampling
     is_open = survey.is_open()
     parent_loc = 'locations'
     for i in range(hierachy_count - location_type.level):    # fetches direct descendants from ea__locations
         parent_loc = '%s__parent' % parent_loc
     total_interviews = dict(Interview.objects.filter(**{'survey': survey
                                                         }).values_list('ea__%s__name' % parent_loc
                                                                        ).annotate(total=Count('id', distinct=True)))
     total_eas = dict(EnumerationArea.objects.all().values_list('%s__name' % parent_loc
                                                                ).annotate(total=Count('id', distinct=True)))
     active_eas = dict(Interview.objects.filter(**{'survey': survey,
                                                   }).values_list('ea__%s__name' % parent_loc
                                                        ).annotate(total=Count('ea', distinct=True)))
     # basically get interviews count
     for location in location_type.locations.all():
         type_total_eas = total_eas.get(location.name, 0)
         type_total_interviews = total_interviews.get(location.name, 0)
         type_active_eas = active_eas.get(location.name, 0)
         indicator_value = float(type_total_interviews) / type_total_eas
         completion_rates[location.name.upper()] = {
             'value': '{0:.2f}'.format(indicator_value),
             'total_eas': type_total_eas,
             'active_eas': type_active_eas,
             'is_open': survey.is_open_for(location),
             'per_active_ea': '{0:.2f}'.format(float(type_total_interviews)/(type_active_eas or 1.0)),
             'total_interviews': type_total_interviews,
         }
     return json.dumps(completion_rates, cls=DjangoJSONEncoder)
Пример #30
0
def list_interviewers(request):
    params = request.GET
    locations_filter = LocationsFilterForm(data=request.GET, include_ea=True)
    if locations_filter.is_valid():
        interviewers = Interviewer.objects.filter(ea__in=locations_filter.get_enumerations()).order_by('name')
    else:
        interviewers = Interviewer.objects.all()
    search_fields = ['name', 'intervieweraccess__user_identifier']
    if request.GET.has_key('q'):
        interviewers = get_filterset(interviewers, request.GET['q'], search_fields)
    if params.has_key('status'):
        interviewers = interviewers.filter(is_blocked=ast.literal_eval(params['status']))
    loc_types = LocationType.in_between()
    return render(request, 'interviewers/index.html',
                  {'interviewers': interviewers,
                   'locations_filter' : locations_filter,
                   'location_filter_types' : loc_types,
                   'placeholder': 'name, mobile numbers, odk id',
                   'request': request})
Пример #31
0
def show(request, survey_id, batch_id):
    batch = Batch.objects.get(id=batch_id)
    prime_location_type = LocationType.largest_unit()
    locations = Location.objects.filter(type=prime_location_type).order_by('name')
    batch_location_ids = batch.open_locations.values_list('location_id', flat=True)
    if request.GET.has_key('status'):
        if request.GET['status'] == 'open':
            locations = locations.filter(id__in=batch_location_ids)       
        else:
            locations = locations.exclude(id__in=batch_location_ids)     
    request.breadcrumbs([
        ('Surveys', reverse('survey_list_page')),
        (batch.survey.name, reverse('batch_index_page', args=(batch.survey.pk, ))),
    ])
    open_locations = locations.filter(id__in=batch_location_ids)
    context = {'batch': batch,
               'locations': locations,
               'open_locations': open_locations,
               'non_response_active_locations': batch.get_non_response_active_locations()}
    return render(request, 'batches/show.html', context)
Пример #32
0
 def get_result_json():
     """Basically fetch all indicators as per the map level info
     :return:
     """
     indicator_details = {}
     country = Location.country()
     if hasattr(settings, 'MAP_ADMIN_LEVEL'):
         location_type = country.get_descendants()[settings.MAP_ADMIN_LEVEL - 1]
     else:
         location_type = LocationType.largest_unit()
     if report_level is None:
         lreport_level = location_type.level
     else:
         lreport_level = report_level
     for indicator in indicators:
         indicator_df = indicator.get_data(country, report_level=lreport_level).fillna(0)
         indicator_df.index = indicator_df.index.str.upper()
         indicator_details[indicator.name] = indicator_df.transpose(
         ).to_dict()
     return json.dumps(indicator_details, cls=DjangoJSONEncoder)
Пример #33
0
def edit(request, ea_id):
    locations_filter = LocationsFilterForm(data=request.GET)
    ea = get_object_or_404(EnumerationArea, pk=ea_id)
    enumeration_area_form = EnumerationAreaForm(instance=ea, locations=locations_filter.get_locations())
    if request.method == 'POST':
        enumeration_area_form = EnumerationAreaForm(data=request.POST, instance=ea)

        if enumeration_area_form.is_valid():
            enumeration_area_form.save()
            messages.success(request, "Enumeration Area successfully Changed.")
            return HttpResponseRedirect(reverse('enumeration_area_home', args=()))
        messages.error(request, "Enumeration area was not Changed.")
    request.breadcrumbs([
        ('Enumeration Areas', reverse('enumeration_area_home')),
#         (_('%s %s') % (action.title(),model.title()),'/crud/%s/%s' % (model,action)),
    ])
    return render(request, 'enumeration_area/new.html',
                  {'enumeration_area_form': enumeration_area_form, 'locations_filter' : locations_filter, 'title': 'New Enumeration Area', 'button_label': 'Create',
                   'action': reverse('edit_enumeration_area_page', args=(ea_id, )), 
                   'location_filter_types' : LocationType.in_between(),
                   })
Пример #34
0
def list_interviewers(request):
    params = request.GET
    locations_filter = LocationsFilterForm(data=request.GET, include_ea=True)
    if locations_filter.is_valid():
        interviewers = Interviewer.objects.filter(
            ea__in=locations_filter.get_enumerations())
    else:
        interviewers = Interviewer.objects.all()
    search_fields = ['name', 'intervieweraccess__user_identifier']
    if 'q' in request.GET:
        interviewers = get_filterset(
            interviewers, request.GET['q'], search_fields)
    if 'status' in params:
        interviewers = interviewers.filter(
            is_blocked=ast.literal_eval(params['status']))
    loc_types = LocationType.in_between()
    return render(request, 'interviewers/index.html',
                  {'interviewers': interviewers.order_by('name'),
                   'locations_filter': locations_filter,
                   'location_filter_types': loc_types,
                   'placeholder': 'name, mobile numbers, odk id',
                   'request': request})
Пример #35
0
def download(request):
    request_data = request.GET if request.method == 'GET' else request.POST
    survey_batch_filter_form = SurveyBatchFilterForm(data=request_data)
    locations_filter = LocationsFilterForm(data=request_data)
    last_selected_loc = locations_filter.last_location_selected
    if request_data and request_data.get('action'):
        survey_batch_filter_form = SurveyBatchFilterForm(data=request_data)
        if survey_batch_filter_form.is_valid():
            batch = survey_batch_filter_form.cleaned_data['batch']
            survey = survey_batch_filter_form.cleaned_data['survey']
            multi_option = \
                survey_batch_filter_form.cleaned_data['multi_option']
            restricted_to = None
            if last_selected_loc:
                restricted_to = [
                    last_selected_loc,
                ]
            if request_data.get('action') == 'Email Spreadsheet':
                composer = ResultComposer(
                    request.user,
                    ResultsDownloadService(batch,
                                           survey=survey,
                                           restrict_to=restricted_to,
                                           multi_display=multi_option))
                send_mail.delay(composer)
                messages.warning(
                    request, "Email would be sent to\
                        you shortly. This could take a while.")
            else:
                download_service = ResultsDownloadService(
                    batch,
                    survey=survey,
                    restrict_to=restricted_to,
                    multi_display=multi_option)
                file_name = '%s%s' % (
                    '%s-%s-' %
                    (last_selected_loc.type.name, last_selected_loc.name)
                    if last_selected_loc else '',
                    batch.name if batch else survey.name)
                reports_df = download_service.generate_interview_reports()
                response = HttpResponse(content_type='application/zip')
                string_buf = StringIO()
                reports_df.to_csv(string_buf, columns=reports_df.columns[1:])
                string_buf.seek(0)
                file_contents = string_buf.read()
                string_buf.close()
                zip_file = InMemoryZip()
                zip_file = zip_file.append("%s.csv" % file_name, file_contents)
                response['Content-Disposition'] = 'attachment;\
                    filename=%s.zip' % file_name
                response.write(zip_file.read())
                # exclude interview id
                if not request.is_ajax():
                    messages.info(request, "Download successfully downloaded")
                return response
    loc_types = LocationType.in_between()
    return render(
        request, 'aggregates/download_excel.html', {
            'survey_batch_filter_form':
            survey_batch_filter_form,
            'locations_filter':
            locations_filter,
            'export_url':
            '%s?%s' % (reverse('excel_report'), request.META['QUERY_STRING']),
            'location_filter_types':
            loc_types
        })
Пример #36
0
def view(request):
    location_type = LocationType.largest_unit()
    locations = Location.objects.filter(type=location_type).order_by('name')
    return render(request, 'bulk_sms/index.html', {'locations': locations})
Пример #37
0
def __get_parent_level_locations():
    return Location.objects.filter(type=LocationType.largest_unit())
Пример #38
0
 def selected_type_is_second_lowest(self):
     return self.selected_location.type == LocationType.smallest_unit().parent
Пример #39
0
 def get_all_location_types(self):
     types = LocationType.objects.exclude(name__iexact='country').exclude(
         pk=LocationType.smallest_unit().pk)
     if not self.level:
         return list(types)
     return types.filter(level__lte=self.level)[1:]
Пример #40
0
 def get_all_location_types(self):
     types = LocationType.objects.exclude(name__iexact='country').exclude(pk=LocationType.smallest_unit().pk)
     if not self.level:
         return list(types)
     return types.filter(level__lte=self.level)[1:]
Пример #41
0
 def get_parent(self):
     largest_loc_type = LocationType.largest_unit()
     locations = Location.objects.filter(type=largest_loc_type).order_by('name')
     return {largest_loc_type.slug: locations } #self.sorted_by_hierarchy({largest_loc_type.slug: locations }) if locations else {}
Пример #42
0
def __get_parent_level_locations():
    return Location.objects.filter(type=LocationType.largest_unit())
Пример #43
0
def _create_or_edit(
        request,
        action_text,
        interviewer_id=None,
        interviewer=None):
    extra = 1
    request.breadcrumbs([
        ('Interviewers', reverse('interviewers_page')),
    ])
    extra = 1
    title = 'New Interviewer'
    odk_instance = None
    # loc_data = request.GET if request.method == 'GET' else request.POST
    data = request.GET if request.method == 'GET' else request.POST
    if request.POST and request.POST.get('ea'):
        ea = get_object_or_404(EnumerationArea, pk=request.POST['ea'])
        data = dict([(loc.type.name, loc.pk) for loc in ea.parent_locations()])
    if interviewer:
        title = 'Edit Interviewer'
        odk_accesses = interviewer.odk_access
        if odk_accesses.exists():
            odk_instance = odk_accesses[0]
        data = data or dict([(loc.type.name, loc.pk)
                             for loc in interviewer.ea.parent_locations()])
        if interviewer.ussd_access.exists():
            extra = 0
    locations_filter = LocationsFilterForm(data=data)
    eas = EnumerationArea.objects.none()
    USSDAccessFormSet = inlineformset_factory(Interviewer, USSDAccess, form=USSDAccessForm, extra=extra)
    ussd_access_form = USSDAccessFormSet(prefix='ussd_access', instance=interviewer)
    response = None
    redirect_url = reverse('interviewers_page')
    odk_access_form = ODKAccessForm(instance=odk_instance)
    if request.method == 'POST':
        interviewer_form = InterviewerForm(eas, data=request.POST, instance=interviewer)
        ussd_access_form = USSDAccessFormSet(
            request.POST,
            prefix='ussd_access',
            instance=interviewer)
        odk_access_form = ODKAccessForm(request.POST, instance=odk_instance)
        if interviewer_form.is_valid() and odk_access_form.is_valid():
            interviewer = interviewer_form.save()
            ussd_access_form.instance = interviewer
            if ussd_access_form.is_valid():
                ussd_access_form.save()
                odk_access = odk_access_form.save(commit=False)
                odk_access.interviewer = interviewer
                odk_access.save()
                messages.success(
                    request, "Interviewer successfully %sed." % action_text)
            return HttpResponseRedirect(redirect_url)
    else:
        interviewer_form = InterviewerForm(eas, instance=interviewer)
    loc_types = LocationType.in_between()
    return response or render(request, 'interviewers/interviewer_form.html', {
        'form': interviewer_form,
        'ussd_access_form': ussd_access_form,
        'odk_access_form': odk_access_form,
        'title': title,
        'id': "create-interviewer-form",
        'class': 'interviewer-form',
        'button_label': "Save",
        'cancel_url': redirect_url,
        'locations_filter': locations_filter,
        'location_filter_types': loc_types,
        'loading_text': "Creating...",
        'mode': action_text,
        'interviewer_id': interviewer_id
    })
Пример #44
0
def completion_json(request, survey_id):
    survey = Survey.objects.get(id=survey_id)
    location_type = LocationType.largest_unit()
    completion_rates = BatchSurveyCompletionRates(location_type).get_completion_formatted_for_json(survey)
    json_dump = json.dumps(completion_rates, cls=DjangoJSONEncoder)
    return HttpResponse(json_dump, mimetype='application/json')
Пример #45
0
 def selected_type_is_second_lowest(self):
     return self.selected_location.type == LocationType.smallest_unit(
     ).parent
Пример #46
0
 def get_result_json():
     print "Getting data from DB"
     survey = Survey.objects.get(id=survey_id)
     location_type = LocationType.largest_unit()
     completion_rates = BatchSurveyCompletionRates(location_type).get_completion_formatted_for_json(survey)
     return json.dumps(completion_rates, cls=DjangoJSONEncoder)
Пример #47
0
def download(request):
    request_data = request.GET if request.method == 'GET' else request.POST
    survey_batch_filter_form = SurveyBatchFilterForm(data=request_data)
    locations_filter = LocationsFilterForm(data=request_data)
    last_selected_loc = locations_filter.last_location_selected
    if request_data and request_data.get('action'):
        survey_batch_filter_form = SurveyBatchFilterForm(data=request_data)
        if survey_batch_filter_form.is_valid():
            batch = survey_batch_filter_form.cleaned_data['batch']
            survey = survey_batch_filter_form.cleaned_data['survey']
            multi_option = \
                survey_batch_filter_form.cleaned_data['multi_option']
            restricted_to = None
            if last_selected_loc:
                restricted_to = [last_selected_loc, ]
            if request_data.get('action') == 'Email Spreadsheet':
                composer = ResultComposer(
                    request.user,
                    ResultsDownloadService(
                        batch,
                        survey=survey,
                        restrict_to=restricted_to,
                        multi_display=multi_option))
                send_mail.delay(composer)
                messages.warning(
                    request, "Email would be sent to\
                        you shortly. This could take a while.")
            else:
                download_service = ResultsDownloadService(
                    batch,
                    survey=survey,
                    restrict_to=restricted_to,
                    multi_display=multi_option)
                file_name = '%s%s' % ('%s-%s-' % (
                    last_selected_loc.type.name,
                    last_selected_loc.name) if last_selected_loc else '',
                    batch.name if batch else survey.name)
                reports_df = download_service.generate_interview_reports()
                response = HttpResponse(content_type='application/zip')
                string_buf = StringIO()
                reports_df.to_csv(string_buf, columns=reports_df.columns[1:])
                string_buf.seek(0)
                file_contents = string_buf.read()
                string_buf.close()
                zip_file = InMemoryZip()
                zip_file = zip_file.append("%s.csv" % file_name, file_contents)
                response['Content-Disposition'] = 'attachment;\
                    filename=%s.zip' % file_name
                response.write(zip_file.read())
                # exclude interview id
                if not request.is_ajax():
                    messages.info(request, "Download successfully downloaded")
                return response
    loc_types = LocationType.in_between()
    return render(request,
                  'aggregates/download_excel.html',
                  {'survey_batch_filter_form': survey_batch_filter_form,
                   'locations_filter': locations_filter,
                   'export_url': '%s?%s' % (reverse('excel_report'),
                                            request.META['QUERY_STRING']),
                   'location_filter_types': loc_types})
Пример #48
0
def view(request):
    location_type = LocationType.largest_unit()
    locations = Location.objects.filter(type=location_type).order_by('name')
    return render(request, 'bulk_sms/index.html', {'locations': locations})