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 can_start_survey(interviewer): return SurveyAllocation.can_start_batch(interviewer)
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 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)