def new_investigator(request): investigator = InvestigatorForm(label_suffix='') selected_location = None selected_ea = None response = None if request.method == 'POST': investigator = InvestigatorForm(data=request.POST, label_suffix='') selected_location = Location.objects.get( id=request.POST['location']) if contains_key( request.POST, 'location') else None selected_ea = EnumerationArea.objects.get( id=request.POST['ea']) if contains_key(request.POST, 'ea') else None action_text = "registered." redirect_url = "/investigators/" response = _process_form(investigator, request, action_text, redirect_url) return response or render( request, 'investigators/investigator_form.html', { 'country_phone_code': COUNTRY_PHONE_CODE, 'locations': LocationWidget(selected_location, ea=selected_ea), 'form': investigator, 'action': "/investigators/new/", 'title': 'New Investigator', 'id': "create-investigator-form", 'class': 'investigator-form', 'button_label': "Create", 'cancel_url': '/investigators/', 'loading_text': "Creating..." })
def edit_household(request, household_id): household_selected = Household.objects.get(id=household_id) selected_location = household_selected.location selected_ea = household_selected.ea response = None uid = household_selected.uid household_form = set_household_form(is_edit=True, instance=household_selected) investigators = Investigator.objects.filter(ea=selected_ea, is_blocked=False) investigator_form = {'value': '', 'text': '', 'options': investigators, 'error': ''} if request.method == 'POST': uid = request.POST.get('uid', None) selected_location = Location.objects.get(id=request.POST['location']) if contains_key(request.POST, 'location') else None selected_ea = EnumerationArea.objects.get(id=request.POST['ea']) if contains_key(request.POST, 'ea') else None response, household_form, investigator, investigator_form = create(request, selected_location, instance=household_selected, is_edit=True, uid=uid) return response or render(request, 'households/new.html', {'selected_location': selected_location, 'locations': LocationWidget(selected_location, ea=selected_ea), 'investigator_form': investigator_form, 'householdform': household_form, 'action': "/households/%s/edit/" % household_id, 'id': "add-household-form", 'button_label': "Save", 'heading': "Edit Household", 'uid': uid, 'loading_text': "Updating...", 'is_edit': True})
def edit_investigator(request, investigator_id): response = None investigator = Investigator.objects.get(id=investigator_id) selected_location = investigator.location selected_ea = investigator.ea investigator_form = InvestigatorForm(instance=investigator, initial= {'confirm_mobile_number':investigator.mobile_number}) if request.method == 'POST': investigator_form = InvestigatorForm(data=request.POST, instance=investigator) action_text = "edited." redirect_url = "/investigators/" selected_location = Location.objects.get(id=request.POST['location']) if contains_key(request.POST, 'location') else None selected_ea = EnumerationArea.objects.get(id=request.POST['ea']) if contains_key(request.POST, 'ea') else None response = _process_form(investigator_form, request, action_text, redirect_url) context = { 'action': '/investigators/%s/edit/' % investigator_id, 'country_phone_code': COUNTRY_PHONE_CODE, 'title': 'Edit Investigator', 'id': 'edit-investigator-form', 'class': 'investigator-form', 'button_label': 'Save', 'cancel_url': '/investigators/', 'loading_text': 'Saving...', 'form': investigator_form, 'locations': LocationWidget(selected_location, ea=selected_ea)} return response or render(request, 'investigators/investigator_form.html', context)
def completed_investigator(request): batch = None survey = None params = request.POST if contains_key(params, 'survey'): survey = Survey.objects.get(id=params['survey']) if contains_key(params, 'batch'): batch = Batch.objects.get(id=params['batch']) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="investigator.csv"' data = Investigator.generate_completion_report(survey, batch=batch) writer = csv.writer(response) for row in data: writer.writerow(row) return response
def simple_indicator(request, indicator_id): hierarchy_limit = 2 selected_location = None first_level_location_analyzed = Location.objects.filter( type__name__iexact="country")[0] indicator = Indicator.objects.get(id=indicator_id) formula = indicator.formula.all() if not formula: messages.error(request, "No formula was found in this indicator") return HttpResponseRedirect(reverse("list_indicator_page")) params = request.GET if contains_key(params, 'location'): first_level_location_analyzed = Location.objects.get( id=params['location']) selected_location = first_level_location_analyzed formula = formula[0] indicator_service = SimpleIndicatorService(formula, first_level_location_analyzed) data_series, locations = indicator_service.get_location_names_and_data_series( ) context = { 'request': request, 'data_series': data_series, 'tabulated_data': indicator_service.tabulated_data_series(), 'location_names': locations, 'indicator': indicator, 'locations': LocationWidget(selected_location, level=hierarchy_limit) } return render(request, 'formula/simple_indicator.html', context)
def simple_indicator(request, indicator_id): hierarchy_limit = 2 selected_location = None first_level_location_analyzed = Location.objects.filter(type__name__iexact="country")[0] indicator = Indicator.objects.get(id=indicator_id) formula = indicator.formula.all() if not formula: messages.error(request, "No formula was found in this indicator") return HttpResponseRedirect(reverse("list_indicator_page")) params = request.GET if contains_key(params, "location"): first_level_location_analyzed = Location.objects.get(id=params["location"]) selected_location = first_level_location_analyzed formula = formula[0] indicator_service = SimpleIndicatorService(formula, first_level_location_analyzed) data_series, locations = indicator_service.get_location_names_and_data_series() context = { "request": request, "data_series": data_series, "tabulated_data": indicator_service.tabulated_data_series(), "location_names": locations, "indicator": indicator, "locations": LocationWidget(selected_location, level=hierarchy_limit), } return render(request, "formula/simple_indicator.html", context)
def list_weights(request): location_weights = LocationWeight.objects.all() surveys = Survey.objects.all() survey = None selected_location = None params = request.GET or request.POST location_filter_form = LocationsFilterForm(data=params) if contains_key(params, 'survey'): survey = Survey.objects.get(id=params['survey']) location_weights = location_weights.filter(survey=survey) if location_filter_form.last_location_selected: location_weights = location_weights.filter( location=location_filter_form.last_location_selected) location_types = LocationType.objects.exclude(name__iexact="country") context = { 'location_weights': location_weights, 'location_types': location_types, 'locations_filter': location_filter_form, 'surveys': surveys, 'selected_survey': survey, 'action': reverse('list_weights_page'), 'request': request } return render(request, 'locations/weights/index.html', context)
def is_valid(params): if not contains_key(params, 'batch'): return False if is_not_digit_nor_empty(params, 'location'): return False if is_not_digit_nor_empty(params, 'ea'): return False return True
def completed_interviewer(request): batch = None survey = None params = request.POST or request.GET if contains_key(params, 'survey'): survey = Survey.objects.get(id=params['survey']) if contains_key(params, 'batch'): batch = Batch.objects.get(id=params['batch']) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="interviewer.csv"' header = ['Interviewer', 'Access Channels'] header.extend(LocationType.objects.exclude( name__iexact='country').values_list('name', flat=True)) data = [header] data.extend(survey.generate_completion_report(batch=batch)) writer = csv.writer(response) for row in data: writer.writerow(row) return response
def completed_interviewer(request): batch = None survey = None params = request.POST or request.GET if contains_key(params, 'survey'): survey = Survey.objects.get(id=params['survey']) if contains_key(params, 'batch'): batch = Batch.objects.get(id=params['batch']) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="interviewer.csv"' header = ['Interviewer', 'Access Channels'] header.extend( LocationType.objects.exclude(name__iexact='country').values_list( 'name', flat=True)) data = [header] data.extend(survey.generate_completion_report(batch=batch)) writer = csv.writer(response) for row in data: writer.writerow(row) return response
def list_weights(request): location_weights = LocationWeight.objects.all() surveys = Survey.objects.all() survey = None selected_location = None params = request.GET if contains_key(params, 'survey'): survey = Survey.objects.get(id=params['survey']) location_weights = location_weights.filter(survey=survey) if contains_key(params, 'location'): selected_location = Location.objects.get(id=params['location']) location_weights = location_weights.filter(location=selected_location) location_types = LocationTypeDetails.get_ordered_types().exclude(name__iexact="country") context = {'location_weights': location_weights, 'location_types': location_types, 'location_data': LocationWidget(selected_location), 'surveys': surveys, 'selected_survey': survey, 'action': 'list_weights_page', 'request': request} return render(request, 'locations/weights/index.html', context)
def new_investigator(request): investigator = InvestigatorForm(label_suffix='') selected_location = None selected_ea = None response = None if request.method == 'POST': investigator = InvestigatorForm(data=request.POST, label_suffix='') selected_location = Location.objects.get(id=request.POST['location']) if contains_key(request.POST, 'location') else None selected_ea = EnumerationArea.objects.get(id=request.POST['ea']) if contains_key(request.POST, 'ea') else None action_text = "registered." redirect_url = "/investigators/" response = _process_form(investigator, request, action_text, redirect_url) return response or render(request, 'investigators/investigator_form.html', {'country_phone_code': COUNTRY_PHONE_CODE, 'locations': LocationWidget(selected_location, ea=selected_ea), 'form': investigator, 'action': "/investigators/new/", 'title': 'New Investigator', 'id': "create-investigator-form", 'class': 'investigator-form', 'button_label': "Create", 'cancel_url': '/investigators/', 'loading_text': "Creating..."})
def edit_investigator(request, investigator_id): response = None investigator = Investigator.objects.get(id=investigator_id) selected_location = investigator.location selected_ea = investigator.ea investigator_form = InvestigatorForm( instance=investigator, initial={'confirm_mobile_number': investigator.mobile_number}) if request.method == 'POST': investigator_form = InvestigatorForm(data=request.POST, instance=investigator) action_text = "edited." redirect_url = "/investigators/" selected_location = Location.objects.get( id=request.POST['location']) if contains_key( request.POST, 'location') else None selected_ea = EnumerationArea.objects.get( id=request.POST['ea']) if contains_key(request.POST, 'ea') else None response = _process_form(investigator_form, request, action_text, redirect_url) context = { 'action': '/investigators/%s/edit/' % investigator_id, 'country_phone_code': COUNTRY_PHONE_CODE, 'title': 'Edit Investigator', 'id': 'edit-investigator-form', 'class': 'investigator-form', 'button_label': 'Save', 'cancel_url': '/investigators/', 'loading_text': 'Saving...', 'form': investigator_form, 'locations': LocationWidget(selected_location, ea=selected_ea) } return response or render(request, 'investigators/investigator_form.html', context)
def new(request): selected_location = None selected_ea = None response = None householdform = set_household_form(data=None) investigator_form = {'value': '', 'text': '', 'options': '', 'error': ''} month_choices = {'selected_text': '', 'selected_value': ''} year_choices = {'selected_text': '', 'selected_value': ''} if request.method == 'POST': selected_ea = EnumerationArea.objects.get(id=request.POST['ea']) if contains_key(request.POST, 'ea') else None if selected_ea: selected_location = selected_ea.locations.all() if selected_location: selected_location = selected_location[0] response, householdform, investigator, investigator_form = create(request, selected_ea) month_choices = {'selected_text': MONTHS[int(request.POST['resident_since_month'])][1], 'selected_value': request.POST['resident_since_month']} year_choices = {'selected_text': request.POST['resident_since_year'], 'selected_value': request.POST['resident_since_year']} household_head = householdform['householdHead'] del householdform['householdHead'] context = {'selected_location': selected_location, 'locations': LocationWidget(selected_location, ea=selected_ea), 'investigator_form': investigator_form, 'headform': household_head, 'householdform': householdform, 'months_choices': household_head.resident_since_month_choices(month_choices), 'years_choices': household_head.resident_since_year_choices(year_choices), 'action': "/households/new/", 'heading': "New Household", 'id': "create-household-form", 'button_label': "Create", 'loading_text': "Creating..."} return response or render(request, 'households/new.html', context)
def has_valid_condition(data): if contains_key(data, 'conditions'): corresponding_condition = GroupCondition.objects.filter( id=int(data['conditions'])) return corresponding_condition return False
def test_contains_key(self): self.assertTrue(contains_key({'bla': '1'}, 'bla')) self.assertFalse(contains_key({'haha': '1'}, 'bla')) self.assertFalse(contains_key({'bla': '-1'}, 'bla')) self.assertFalse(contains_key({'bla': ''}, 'bla')) self.assertFalse(contains_key({'bla': 'NOT_A_DIGIT'}, 'bla'))
def is_valid(params): if contains_key(params, 'location') and contains_key(params, 'batch'): return True if params.has_key('location') and params['location'] == '': return True return False
def has_valid_condition(data): if contains_key(data, 'conditions'): corresponding_condition = GroupCondition.objects.filter(id=int(data['conditions'])) return corresponding_condition return False
def is_valid(params): if contains_key(params, 'location') and contains_key(params, 'batch'): return True if 'location' in params and params['location'] == '': return True return False