def test_answer_types_available(self): known_answer_types = [ NumericalAnswer, AutoResponse, MultiSelectAnswer, TextAnswer, MultiChoiceAnswer, AudioAnswer, VideoAnswer, ImageAnswer, GeopointAnswer ] for answer_type in known_answer_types: self.assertIn(answer_type.choice_name(), Answer.answer_types())
def test_logic_form_does_not_have_options_for_non_multi_type_questions(self): answer_types = Answer.answer_types() for answer_type in answer_types: if answer_type not in [MultiSelectAnswer.choice_name(), MultiChoiceAnswer.choice_name()]: q = Question.objects.create(identifier=answer_type, text="text", answer_type=answer_type, qset_id=self.qset.id, response_validation_id=1) l = LogicForm(q) self.assertFalse(l.fields.get('option'))
def test_reload_answer_access(self): AnswerAccessDefinition.objects.all().delete() self.assertEquals(AnswerAccessDefinition.objects.count(), 0) AnswerAccessDefinition.reload_answer_categories() self.assertTrue(AnswerAccessDefinition.objects.count() > 0) # chech for each access type has an entry channels = [USSDAccess.choice_name(), ODKAccess.choice_name(), WebAccess.choice_name()] allowed_channels = AnswerAccessDefinition.objects.values_list('channel', flat=True) for channel in channels: self.assertIn(channel, allowed_channels) self.assertTrue(len(AnswerAccessDefinition.answer_types(channel)) > 0) answer_types = Answer.answer_types() for answer_type in [VideoAnswer, AudioAnswer, ImageAnswer]: self.assertNotIn(answer_type.choice_name(), AnswerAccessDefinition.answer_types(USSDAccess.choice_name()))
def test_logic_form_does_not_have_options_for_non_multi_type_questions( self): answer_types = Answer.answer_types() for answer_type in answer_types: if answer_type not in [ MultiSelectAnswer.choice_name(), MultiChoiceAnswer.choice_name() ]: q = Question.objects.create(identifier=answer_type, text="text", answer_type=answer_type, qset_id=self.qset.id, response_validation_id=1) l = LogicForm(q) self.assertFalse(l.fields.get('option'))
def test_reload_answer_access(self): AnswerAccessDefinition.objects.all().delete() self.assertEquals(AnswerAccessDefinition.objects.count(), 0) AnswerAccessDefinition.reload_answer_categories() self.assertTrue(AnswerAccessDefinition.objects.count() > 0) # chech for each access type has an entry channels = [ USSDAccess.choice_name(), ODKAccess.choice_name(), WebAccess.choice_name() ] allowed_channels = AnswerAccessDefinition.objects.values_list( 'channel', flat=True) for channel in channels: self.assertIn(channel, allowed_channels) self.assertTrue( len(AnswerAccessDefinition.answer_types(channel)) > 0) answer_types = Answer.answer_types() for answer_type in [VideoAnswer, AudioAnswer, ImageAnswer]: self.assertNotIn( answer_type.choice_name(), AnswerAccessDefinition.answer_types(USSDAccess.choice_name()))
def get_interview_answers(self): report = [] member_reports = OrderedDict() val_list_args = [ 'interview__ea__locations__name', 'interview__ea__name', 'interview__householdmember__household__house_number', 'interview__householdmember__surname', 'interview__householdmember__first_name', 'interview__householdmember__date_of_birth', 'interview__householdmember__gender', ] parent_loc = 'interview__ea__locations' for i in range(LocationType.objects.count() - 2): parent_loc = '%s__parent' % parent_loc val_list_args.insert(0, '%s__name'%parent_loc) filter_args = {} if self.locations: filter_args['interview__ea__locations__in'] = self.locations if self.specific_households: filter_args['interview__householdmember__household__in'] = self.specific_households for answer_type in Answer.answer_types(): query_args = list(val_list_args) value = 'value' if answer_type in [MultiChoiceAnswer.choice_name(), ]: value = 'value__order' if self.multi_display == self.AS_LABEL: value = 'value__order' else: value = 'value__text' query_args.append(value) answer_class = Answer.get_class(answer_type) # print 'using query_args ', query_args answer_data = answer_class.objects.filter(interview__batch=self.batch, **filter_args).\ values_list('interview__householdmember__pk', 'question__pk', *query_args).\ order_by('interview__ea__locations', 'interview__ea', 'interview__householdmember__household').distinct( 'interview__ea__locations', 'interview__ea', 'interview__householdmember__household', 'interview__householdmember__pk', 'question__pk' ) answer_data = list(answer_data) # print 'answer data ', len(answer_data) #now grab member reports for data in answer_data: hm_pk, question_pk = data[:2] report_data = list(data[2:]) hm_data = member_reports.get(hm_pk, None) if hm_data is None: report_data.insert(-3, str(dateutils.relativedelta(datetime.utcnow().date(), report_data[-3]).years)) report_data[-3] = report_data[-3].strftime(settings.DATE_FORMAT) report_data[-2] = 'M' if report_data[-2] else 'F' member_details = [ unicode(md).encode('utf8') for md in report_data[:-1]] hm_data = OrderedDict([('mem_details' , member_details), ]) hm_data[question_pk] = unicode(report_data[-1]).encode('utf8') member_reports[hm_pk] = hm_data for hm in member_reports.values(): answers = hm['mem_details'] for question in self.questions: answers.append(hm.get(question.pk, '')) report.append(answers) return report
def test_answer_types_available(self): known_answer_types = [NumericalAnswer, AutoResponse, MultiSelectAnswer, TextAnswer, MultiChoiceAnswer, AudioAnswer, VideoAnswer, ImageAnswer, GeopointAnswer ] for answer_type in known_answer_types: self.assertIn(answer_type.choice_name(), Answer.answer_types())
def get_interview_answers(self): report = [] member_reports = OrderedDict() val_list_args = [ 'interview__ea__locations__name', 'interview__ea__name', 'interview__householdmember__household__house_number', 'interview__householdmember__surname', 'interview__householdmember__first_name', 'interview__householdmember__date_of_birth', 'interview__householdmember__gender', ] parent_loc = 'interview__ea__locations' for i in range(LocationType.objects.count() - 2): parent_loc = '%s__parent' % parent_loc val_list_args.insert(0, '%s__name' % parent_loc) filter_args = {} if self.locations: filter_args['interview__ea__locations__in'] = self.locations if self.specific_households: filter_args[ 'interview__householdmember__household__in'] = self.specific_households for answer_type in Answer.answer_types(): query_args = list(val_list_args) value = 'value' if answer_type in [ MultiChoiceAnswer.choice_name(), MultiSelectAnswer.choice_name() ]: value = 'value__text' if self.multi_display == self.AS_LABEL: value = 'value__order' query_args.append(value) answer_class = Answer.get_class(answer_type) # print 'using query_args ', query_args answer_data = answer_class.objects.filter(interview__batch=self.batch, **filter_args).\ values_list('interview__householdmember__pk', 'question__pk', *query_args).\ order_by('interview__ea__locations', 'interview__ea', 'interview__householdmember__household', 'interview__householdmember__pk', 'pk', 'loop_id') # .distinct( # 'interview__ea__locations', # 'interview__ea', # 'interview__householdmember__household', # #'interview__householdmember__pk', # #'question__pk' # ) answer_data = list(answer_data) # print 'answer data ', len(answer_data) #now grab member reports for data in answer_data: hm_pk, question_pk = data[:2] report_data = list(data[2:]) hm_data = member_reports.get(hm_pk, None) if hm_data is None: report_data.insert( -3, str( dateutils.relativedelta(datetime.utcnow().date(), report_data[-3]).years)) report_data[-3] = report_data[-3].strftime( settings.DATE_FORMAT) report_data[-2] = 'M' if report_data[-2] else 'F' member_details = [ unicode(md).encode('utf8') for md in report_data[:-1] ] hm_data = OrderedDict([ ('mem_details', member_details), ]) hm_question_data = hm_data.get(question_pk, []) hm_question_data.append( unicode(report_data[-1]).encode('utf8')) hm_data[question_pk] = hm_question_data member_reports[hm_pk] = hm_data for hm in member_reports.values(): answers = hm.pop('mem_details', []) #for question_pk, response_data in hm.items(): loop_starters = self.batch.loop_starters() loop_enders = self.batch.loop_enders() started = False dept = 0 for question in self.questions: # loop_extras = [] # boundary = question.loop_boundary() # if question in loop_starters: # loop_extras = [] # loop_questions = self.batch.loop_backs_questions()[boundary] # for i in range(1, settings.LOOP_QUESTION_REPORT_DEPT): # for q in loop_questions: # question_responses = hm.get(question.pk, []) # if len(question_responses) > i: # resp = question_responses[i] # else: # resp = '' # loop_extras.append(resp) # import pdb; pdb.set_trace() # if question in loop_enders: # answers.extend(loop_extras) answers.append(' > '.join(hm.get(question.pk, [ '', ]))) report.append(answers) return report