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'})
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)
def respond(self, request, session_data): access = self.access # check if there is any active interview,\ #if yes, ask interview last question interview = session_data.get('interview', None) # if interview is Non show select EA form if interview is None and access.interviewer.unfinished_assignments.exists(): interviewer = access.interviewer survey = interviewer.unfinished_assignments.first().survey if SurveyAllocation.can_start_batch(interviewer, survey=survey) and survey.is_open() is False: return self._render_deny_template(request, access, 'interviews/no-open-survey.html') elif access.interviewer.unfinished_assignments.exists() is False: return self._render_deny_template(request, access, 'interviews/no-ea-left.html') return super(OnlineInterview, self).respond(request, session_data)
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' })
def survey_allocation(self): return SurveyAllocation.get_allocation_details(self.interviewer)
def get_survey_allocation(interviewer): return SurveyAllocation.get_allocation_details(interviewer)
def get_survey(interviewer): return SurveyAllocation.get_allocation(interviewer)
def can_start_survey(interviewer): return SurveyAllocation.can_start_batch(interviewer)
def get_survey_allocation(interviewer): '''Just helper function to put additional layer of abstraction to allocation retrival @param: interviewer. Interviewer to which to get survey allocation ''' return SurveyAllocation.get_allocation_details(interviewer)
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)
def start_interview(self, request, session_data): """Steps: 1. Select EA 2. 2.0. Select Random sample if survey has sampling and listing is completed. 2.1. Select Batch if survey is ready for \ batch collection, else skip this step and \ select available listing/batch 3. Move to interview questions. This func is expected to be called only when survey is open :param self: :param request: :param session_data: :return: """ access = self.access interviewer = access.interviewer request_data = request.GET if request.method == 'GET' else request.POST context = {} if '_ref_interview' in session_data: # if the user is trying to select a random sample... interview = session_data['_ref_interview'] interview_form = ReferenceInterviewForm(request, access, interview.survey, interview.ea, data=request_data) if interview_form.is_valid(): session_data['ref_interview'] = interview_form.cleaned_data['value'] del session_data['_ref_interview'] else: return self._render_init_form( request, interview_form) if '_interview' in session_data: # basically if the user is trying to select a batch survey = session_data['_interview'].survey interview_form = SelectBatchForm( request, access, survey, data=request_data) if interview_form.is_valid(): batch = interview_form.cleaned_data['value'] interview = session_data['_interview'] interview.question_set = batch interview.last_question = batch.g_first_question del session_data['_interview'] return self.init_responses(request, interview, session_data) elif 'interview' in session_data: # though the interview value might be None interview_form = SurveyAllocationForm( request, access, data=request_data) if interview_form.is_valid(): interview = interview_form.save(commit=False) interview.interviewer = interviewer interview.interview_channel = access survey = interview.survey survey_allocation = interview_form.selected_allocation() interview.ea = survey_allocation.allocation_ea if interview.survey.has_sampling and (SurveyAllocation.can_start_batch(interviewer) is False): # batch not yet ready # go straight to listing form return self._initiate_listing(request, interview, survey, session_data) elif interview.survey.has_sampling and 'ref_interview' not in session_data: # basically request the interviewer to choose listing form if before starting batch questions session_data['_ref_interview'] = interview interview_form = ReferenceInterviewForm(request, access, survey, survey_allocation.allocation_ea) elif survey_allocation.open_batches() > 0: # ready for batch collection # ask user to select the batch if batch is more than one if len(survey_allocation.open_batches()) > 1: session_data['_interview'] = interview # semi formed, ask user to choose batch interview_form = SelectBatchForm(request, access, survey) else: batch = survey_allocation.open_batches()[0] interview.question_set = batch interview.last_question = batch.g_first_question return self.init_responses(request, interview, session_data) else: interview_form = SurveyAllocationForm(request, access) session_data['interview'] = None return self._render_init_form(request, interview_form)
def start_interview(self, request, session_data): """Steps: 1. Select EA 2. 2.0. Select Random sample if survey has sampling and listing is completed. 2.1. Select Batch if survey is ready for \ batch collection, else skip this step and \ select available listing/batch 3. Move to interview questions. This func is expected to be called only when survey is open. To do: Refactor this function soon. Though it's currently well tested. :param self: :param request: :param session_data: :return: """ access = self.access interviewer = access.interviewer request_data = request.GET if request.method == 'GET' else request.POST context = {} if '_started_batch' in session_data: interview = session_data['_started_batch'] survey_allocation = session_data['_started_batch_assignment'] interview_form = SelectBatchOrListingForm(request, access, data=request_data) del session_data['_started_batch'] del session_data['_started_batch_assignment'] if interview_form.is_valid(): if interview_form.cleaned_data[ 'value'] == SelectBatchOrListingForm.LISTING: return self._initiate_listing(request, interview, interview.survey, session_data) else: # okay to start the batch, you need to choosing the sampled interview session_data['_ref_interview'] = interview session_data[ '_ref_interview_assignment'] = survey_allocation survey = survey_allocation.survey interview_form = ReferenceInterviewForm( request, access, survey, survey_allocation.allocation_ea) session_data['interview'] = None return self._render_init_form(request, interview_form) if '_ref_interview' in session_data: # if the user is trying to select a random sample... interview = session_data['_ref_interview'] survey_allocation = session_data['_ref_interview_assignment'] interview_form = ReferenceInterviewForm(request, access, interview.survey, interview.ea, data=request_data) if interview_form.is_valid(): session_data['ref_interview'] = interview_form.cleaned_data[ 'value'] del session_data['_ref_interview'] del session_data['_ref_interview_assignment'] if survey_allocation.open_batches() > 0: return self._attempt_batch(request, interview, interview.survey, session_data, survey_allocation) else: return self._render_init_form(request, interview_form) if '_interview' in session_data: # basically if the user is trying to select a batch survey = session_data['_interview'].survey survey_allocation = session_data['_interview_assignment'] interview_form = SelectBatchForm(request, access, survey_allocation, data=request_data) if interview_form.is_valid(): batch = interview_form.cleaned_data['value'] interview = session_data['_interview'] interview.question_set = batch interview.last_question = batch.g_first_question del session_data['_interview'] return self.init_responses(request, interview, session_data) elif 'interview' in session_data: # though the interview value might be None interview_form = SurveyAllocationForm(request, access, data=request_data) if interview_form.is_valid(): interview = interview_form.save(commit=False) interview.interviewer = interviewer interview.interview_channel = access survey = interview.survey survey_allocation = interview_form.selected_allocation() interview.ea = survey_allocation.allocation_ea if interview.survey.has_sampling and ( SurveyAllocation.can_start_batch(interviewer) is False): # batch not yet ready # go straight to listing form return self._initiate_listing(request, interview, survey, session_data) elif interview.survey.has_sampling and 'started_batch' not in session_data: # ask if user should start batch or not session_data['_started_batch'] = interview session_data[ '_started_batch_assignment'] = survey_allocation interview_form = SelectBatchOrListingForm(request, access) elif survey_allocation.open_batches( ) > 0: # ready for batch collection # ask user to select the batch if batch is more than one return self._attempt_batch(request, interview, survey, session_data, survey_allocation) # might need to show message when no batch is open else: interview_form = SurveyAllocationForm(request, access) session_data['interview'] = None return self._render_init_form(request, interview_form)
def clean_registrar(self): if SurveyAllocation.get_allocation(self.cleaned_data['last_registrar']) is None: raise ValidationError("No open survey available for this Interviewer yet.") return self.cleaned_data['registrar']
def clean_registrar(self): if SurveyAllocation.get_allocation( self.cleaned_data['last_registrar']) is None: raise ValidationError( "No open survey available for this Interviewer yet.") return self.cleaned_data['registrar']