def __init__(self, *args, **kwargs): super(TemplateForm, self).__init__(*args, **kwargs) instance = kwargs.get('instance', None) if instance: self.help_text = ' and '.join( AnswerAccessDefinition.access_channels( instance.answer_type)) self.fields['answer_type'].help_text = self.help_text self.fields['answer_type'].choices = [ (name, name) for name in AnswerAccessDefinition.answer_types( USSDAccess.choice_name()) if name != AutoResponse.choice_name() ] self.fields['answer_type'].choices.insert( 0, ('', '----Select Answer Type -----')) # key,val pair of supported access channels for each answer type self.answer_map = {} # not much needed since we are only restricting to USSD access definitions = AnswerAccessDefinition.objects.filter() for definiton in definitions: self.answer_map[definiton.answer_type] = self.answer_map.get( definiton.answer_type, []) self.answer_map[definiton.answer_type].append( definiton.channel) self.order_fields(['module', 'identifier', 'text', 'answer_type'])
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_group_next_question(question, proposed_next): next_question = proposed_next present_question_group = question.group if hasattr(question, 'group') else None if next_question and AnswerAccessDefinition.is_valid(access.choice_name(), next_question.answer_type) is False: next_question = _get_group_next_question(question, next_question.next_question(reply)) # I hope the next line is not so confusing! # Basically it means treat only if the next question belongs to a different group from the present. # That's if present has a group if hasattr(next_question, 'group') and present_question_group != next_question.group: question_group = next_question.group if question_group: qset = QuestionSet.get(pk=next_question.qset.pk) valid_group = True for condition in question_group.group_conditions.all(): # we are interested in the qset param list with same identifier name as condition.test_question test_question = qset.parameter_list.questions.get(identifier=condition.test_question.identifier) param_value = '' # use answer.as value if session_data[ANSWERS][-1].get(test_question.identifier, None): # last answer entry param_value = session_data[ANSWERS][-1][test_question.identifier] answer_class = Answer.get_class(condition.test_question.answer_type) validator = getattr(answer_class, condition.validation_test, None) if validator is None: raise ValueError('unsupported validator defined on listing question') try: slogger.debug('parm val: %s, params: %s' % (param_value, condition.test_params)) is_valid = validator(param_value, *condition.test_params) except: is_valid = True if is_valid is False: valid_group = False break # fail if any condition fails if valid_group is False: next_question = _get_group_next_question(question, next_question.next_question(reply)) return next_question
def __init__(self, *args, **kwargs): super(QuestionTemplateForm, self).__init__(*args, **kwargs) self.fields.keyOrder = ['module', 'text', 'identifier', 'group', 'answer_type'] instance = kwargs.get('instance', None) if instance: self.help_text = ' and '.join(AnswerAccessDefinition.access_channels(instance.answer_type)) self.fields['answer_type'].help_text = self.help_text self.answer_map = {} definitions = AnswerAccessDefinition.objects.all() for defi in definitions: self.answer_map[defi.answer_type] = self.answer_map.get(defi.answer_type, []) self.answer_map[defi.answer_type].append(defi.channel)
def __init__(self, *args, **kwargs): super(TemplateForm, self).__init__(*args, **kwargs) instance = kwargs.get('instance', None) if instance: self.help_text = ' and '.join( AnswerAccessDefinition.access_channels( instance.answer_type)) self.fields['answer_type'].help_text = self.help_text self.fields['answer_type'].choices = [(name, name) for name in AnswerAccessDefinition.answer_types(USSDAccess.choice_name()) if name != AutoResponse.choice_name()] self.fields['answer_type'].choices.insert(0,('','----Select Answer Type -----')) # key,val pair of supported access channels for each answer type self.answer_map = {} # not much needed since we are only restricting to USSD access definitions = AnswerAccessDefinition.objects.filter() for definiton in definitions: self.answer_map[definiton.answer_type] = self.answer_map.get( definiton.answer_type, []) self.answer_map[definiton.answer_type].append( definiton.channel) self.order_fields(['module', 'identifier', 'text', 'answer_type'])
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_group_next_question(question, proposed_next): next_question = proposed_next present_question_group = question.group if hasattr( question, 'group') else None if next_question and AnswerAccessDefinition.is_valid( access.choice_name(), next_question.answer_type) is False: next_question = _get_group_next_question( question, next_question.next_question(reply)) # I hope the next line is not so confusing! # Basically it means treat only if the next question belongs to a different group from the present. # That's if present has a group if hasattr( next_question, 'group') and present_question_group != next_question.group: question_group = next_question.group if question_group: qset = QuestionSet.get(pk=next_question.qset.pk) valid_group = True for condition in question_group.group_conditions.all(): # we are interested in the qset param list with same identifier name as condition.test_question test_question = qset.parameter_list.questions.get( identifier=condition.test_question.identifier) param_value = '' # use answer.as value if session_data[ANSWERS][-1].get( test_question.identifier, None): # last answer entry param_value = session_data[ANSWERS][-1][ test_question.identifier] answer_class = Answer.get_class( condition.test_question.answer_type) validator = getattr(answer_class, condition.validation_test, None) if validator is None: raise ValueError( 'unsupported validator defined on listing question' ) try: slogger.debug('parm val: %s, params: %s' % (param_value, condition.test_params)) is_valid = validator(param_value, *condition.test_params) except: is_valid = True if is_valid is False: valid_group = False break # fail if any condition fails if valid_group is False: next_question = _get_group_next_question( question, next_question.next_question(reply)) return next_question
def __init__(self, batch, data=None, initial=None, parent_question=None, instance=None): super(QuestionForm, self).__init__(data=data, initial=initial, instance=instance) self.fields['identifier'].label = "Variable name" self.fields['batch'].widget = forms.HiddenInput() self.fields['batch'].initial = batch.pk self.batch = batch #depending on type of ussd/odk access of batch restrict the answer type self.fields['answer_type'].choices = [choice for choice in self.fields['answer_type'].choices \ if choice[0] in batch.answer_types or choice[0] == '' ] if instance: self.help_text = ' and '.join(AnswerAccessDefinition.access_channels(instance.answer_type)) self.fields['answer_type'].help_text = self.help_text self.answer_map = {} definitions = AnswerAccessDefinition.objects.all() for defi in definitions: self.answer_map[defi.answer_type] = self.answer_map.get(defi.answer_type, []) self.answer_map[defi.answer_type].append(defi.channel) self.parent_question = parent_question
def __init__( self, qset, data=None, initial=None, parent_question=None, instance=None, prev_question=None): super(QuestionForm, self).__init__( data=data, initial=initial, instance=instance) self.fields['identifier'].label = "Variable name" self.fields['qset'].widget = forms.HiddenInput() self.fields['qset'].initial = qset.pk self.qset = qset self.prev_question = prev_question # depending on type of ussd/odk access of qset restrict the answer # type self.fields['answer_type'].choices = [ choice for choice in self.fields['answer_type'].choices if choice[0] in qset.answer_types] self.fields['answer_type'].choices.insert( 0, ('', '----Select Answer Type----')) if instance: self.help_text = ' and '.join(AnswerAccessDefinition.access_channels(instance.answer_type)) self.fields['answer_type'].help_text = self.help_text self.answer_map = {} definitions = AnswerAccessDefinition.objects.all() for defi in definitions: self.answer_map[defi.answer_type] = self.answer_map.get(defi.answer_type, []) self.answer_map[defi.answer_type].append(defi.channel) self.fields['response_validation'].icons = {'add': {'data-toggle': "modal", 'data-target': "#add_validation", 'id': 'add_validation_button', 'title': 'Add Validation'}, } self.parent_question = parent_question self.order_fields(['module', 'group', 'identifier', 'text', 'answer_type', 'mandatory'])
def handle(self, *args, **kwargs): #self.stdout.write('Creating permissions....') content_type = ContentType.objects.get_for_model(User) can_enter_data, _ = Permission.objects.get_or_create( codename='can_enter_data', name='Can enter data', content_type=content_type) can_view_batches, _ = Permission.objects.get_or_create( codename='can_view_batches', name='Can view Batches', content_type=content_type) can_view_interviewer, _ = Permission.objects.get_or_create( codename='can_view_interviewers', name='Can view Interviewers', content_type=content_type) can_view_aggregates, _ = Permission.objects.get_or_create( codename='can_view_aggregates', name='Can view Aggregates', content_type=content_type) can_view_com_surveys, _ = Permission.objects.get_or_create( codename='view_completed_survey', name='Can view Completed Surveys', content_type=content_type) can_view_households, _ = Permission.objects.get_or_create( codename='can_view_househs', name='Can view Households', content_type=content_type) can_view_locations, _ = Permission.objects.get_or_create( codename='can_view_locations', name='Can view Locations', content_type=content_type) can_view_users, _ = Permission.objects.get_or_create( codename='can_view_users', name='Can view Users', content_type=content_type) can_receive_email, _ = Permission.objects.get_or_create( codename='can_receive_email', name='Can Receive Email', content_type=content_type) can_have_super_powers, _ = Permission.objects.get_or_create( codename='can_have_super_powers', name='Can Have Super Powers', content_type=content_type) #self.stdout.write('Permissions created.') #self.stdout.write('Creating some groups...') group, _ = Group.objects.get_or_create(name='Administrator') group.permissions.add(can_enter_data) group.permissions.add(can_view_aggregates) group.permissions.add(can_view_batches) group.permissions.add(can_view_com_surveys) group.permissions.add(can_view_households) group.permissions.add(can_view_interviewer) group.permissions.add(can_view_locations) group.permissions.add(can_view_users) group.permissions.add(can_receive_email) group.permissions.add(can_have_super_powers) group, _ = Group.objects.get_or_create(name='Researcher') group.permissions.add(can_enter_data) group.permissions.add(can_view_aggregates) group.permissions.add(can_view_batches) group.permissions.add(can_view_com_surveys) group.permissions.add(can_view_households) group.permissions.add(can_view_interviewer) group.permissions.add(can_view_locations) group.permissions.add(can_receive_email) group, _ = Group.objects.get_or_create(name='Supervisor') group.permissions.add(can_enter_data) group.permissions.add(can_view_aggregates) group.permissions.add(can_view_batches) group.permissions.add(can_view_com_surveys) group.permissions.add(can_view_households) group.permissions.add(can_view_locations) group, _ = Group.objects.get_or_create(name='Data collector') group.permissions.add(can_enter_data) group.permissions.add(can_view_batches) group, _ = Group.objects.get_or_create(name='Viewer') group.permissions.add(can_view_aggregates) group.permissions.add(can_view_batches) group.permissions.add(can_view_com_surveys) group.permissions.add(can_receive_email) group, _ = Group.objects.get_or_create(name='Data Email Reports') group.permissions.add(can_receive_email) #self.stdout.write('Created groups.') #self.stdout.write('Creating answer definition... ') # ussd definition AnswerAccessDefinition.reload_answer_categories()
def setUpTestData(cls): super(Base, cls).setUpTestData() setup_test_environment() AnswerAccessDefinition.reload_answer_categories()