def __init__(self, request, access, *args, **kwargs): super(AddMoreLoopForm, self).__init__(request, access, *args, **kwargs) self.fields['value'] = forms.ChoiceField(choices=self.CHOICES, widget=forms.RadioSelect) if self.access.choice_name() == USSDAccess.choice_name(): self.fields['value'].widget = forms.NumberInput() if access.choice_name() == USSDAccess.choice_name(): self.fields['value'].label = '' else: self.fields['value'].label = 'Answer'
def __init__(self, *args, **kwargs): super(AnswerForm, self).__init__(*args, **kwargs) # self.fields['uid'] = forms.CharField(initial=access.user_identifier, widget=forms.HiddenInput) if question.answer_type == DateAnswer.choice_name(): self.fields['value'] = forms.DateField( label='Answer', input_formats=[ settings.DATE_FORMAT, ], widget=forms.DateInput(attrs={ 'placeholder': 'Answer', 'class': 'datepicker' }, format=settings.DATE_FORMAT)) if question.answer_type == GeopointAnswer.choice_name(): model_field = get_form_field_no_validation(forms.CharField) self.fields['value'] = model_field( label='Answer', widget=forms.TextInput( attrs={ 'placeholder': 'Lat[space4]Long[space4' 'Altitude[space4]Precision' })) if question.answer_type == MultiChoiceAnswer.choice_name(): self.fields['value'] = forms.ChoiceField( choices=[(opt.order, opt.text) for opt in question.options.all()], widget=forms.RadioSelect) self.fields['value'].empty_label = None if access.choice_name() == USSDAccess.choice_name(): self.fields['value'].widget = forms.NumberInput() if question.answer_type == MultiSelectAnswer.choice_name(): self.fields['value'] = forms.ModelMultipleChoiceField( queryset=question.options.all(), widget=forms.CheckboxSelectMultiple) accept_types = { AudioAnswer.choice_name(): 'audio/*', VideoAnswer.choice_name(): 'video/*', ImageAnswer.choice_name(): 'image/*' } if question.answer_type in [ AudioAnswer.choice_name(), VideoAnswer.choice_name(), ImageAnswer.choice_name() ]: self.fields['value'].widget.attrs = { 'accept': accept_types.get(question.answer_type, '|'.join(accept_types.values())) } if access.choice_name() == USSDAccess.choice_name(): self.fields['value'].label = '' else: self.fields['value'].label = 'Answer'
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 _respond(self, message): ongoing_interview = self._ongoing_interview try: response = ongoing_interview.respond( reply=message, channel=USSDAccess.choice_name()) except: response = '\n'.join([ERROR, self.intro()]) self._ongoing_interview = ongoing_interview #probably something may have happened to the interview instance in db if ongoing_interview.is_closed(): house_member = ongoing_interview.householdmember house_member.batch_completed(ongoing_interview.batch) batches = self._pending_batches batches.pop(0) #pop off the present batch self._pending_batches = batches if self._pending_batches: #start next batch and respond interview, created = Interview.objects.get_or_create( interviewer=self.interviewer, householdmember=house_member, batch=self._pending_batches[0], interview_channel=self.access, ea=self.enumeration_area) if created: self._ongoing_interview = interview return self.intro() else: task = ConfirmContinue(self.access) task._ongoing_interview = interview task._pending_batches = batches return task.intro() else: return EndMemberSurvey.get_new(house_member, self.access) else: return response
def _respond(self, message): pending_prompts = self.pending_prompts if pending_prompts: attrs = pending_prompts.keys() extractor = self.extractor().get(attrs[0], None) if extractor and hasattr(extractor, '__call__'): attr_val = extractor(message) else: attr_val = message if attr_val is None: return '\n'.join([INVALID_INPUT, self.intro()]) else: hm_details = self.hm_details hm_details[attrs[0]] = attr_val self.hm_details = hm_details pending_prompts.popitem( 0 ) #whatever the case, we've treated this one. Hence pop it self.pending_prompts = pending_prompts if not pending_prompts: #no more prompts if hm_details.get(self.IS_HEAD): hm = HouseholdHead.objects.create( household=self._household, surname=hm_details.get(self.SURNAME), first_name=hm_details.get(self.FIRSTNAME), date_of_birth=hm_details.get(self.DOB), gender=hm_details.get(self.GENDER), survey_listing=self.survey_listing, registrar=self.interviewer, registration_channel=USSDAccess.choice_name()) else: hm = HouseholdMember.objects.create( household=self._household, surname=hm_details.get(self.SURNAME), first_name=hm_details.get(self.FIRSTNAME), date_of_birth=hm_details.get(self.DOB), gender=hm_details.get(self.GENDER), survey_listing=self.survey_listing, registrar=self.interviewer, registration_channel=USSDAccess.choice_name()) task = StartInterview(self.access) task._house_member = hm return task.intro() else: self.reset() return self.intro()
def __init__(self, request, access, *args, **kwargs): super(SurveyAllocationForm, self).__init__(request, access, *args, **kwargs) self.CHOICES = [(idx + 1, sa.allocation_ea.name) for idx, sa in enumerate(self.interviewer.unfinished_assignments.order_by('allocation_ea__name'))] self.fields['value'] = forms.ChoiceField(choices=self.CHOICES,widget=forms.RadioSelect) if self.access.choice_name() == USSDAccess.choice_name(): self.fields['value'].widget = forms.NumberInput() self.fields['value'].label = 'Select EA' self.order_fields(['value', 'test_data'])
def __init__(self, *args, **kwargs): super(AnswerForm, self).__init__(*args, **kwargs) # self.fields['uid'] = forms.CharField(initial=access.user_identifier, widget=forms.HiddenInput) if question.answer_type == DateAnswer.choice_name(): self.fields['value'] = forms.DateField( label='Answer', input_formats=[ settings.DATE_FORMAT, ], widget=forms.DateInput( attrs={ 'placeholder': 'Answer', 'class': 'datepicker'}, format=settings.DATE_FORMAT)) if question.answer_type == GeopointAnswer.choice_name(): model_field = get_form_field_no_validation(forms.CharField) self.fields['value'] = model_field(label='Answer', widget=forms.TextInput( attrs={'placeholder': 'Lat[space4]Long[space4' 'Altitude[space4]Precision'})) if question.answer_type == MultiChoiceAnswer.choice_name(): self.fields['value'] = forms.ChoiceField(choices=[(opt.order, opt.text) for opt in question.options.all()], widget=forms.RadioSelect) self.fields['value'].empty_label = None if access.choice_name() == USSDAccess.choice_name(): self.fields['value'].widget = forms.NumberInput() if question.answer_type == MultiSelectAnswer.choice_name(): self.fields['value'] = forms.ModelMultipleChoiceField( queryset=question.options.all(), widget=forms.CheckboxSelectMultiple) accept_types = {AudioAnswer.choice_name(): 'audio/*', VideoAnswer.choice_name(): 'video/*', ImageAnswer.choice_name(): 'image/*' } if question.answer_type in [ AudioAnswer.choice_name(), VideoAnswer.choice_name(), ImageAnswer.choice_name()]: self.fields['value'].widget.attrs = { 'accept': accept_types.get( question.answer_type, '|'.join( accept_types.values()))} if access.choice_name() == USSDAccess.choice_name(): self.fields['value'].label = '' else: self.fields['value'].label = 'Answer'
def __init__(self, request, access, survey, allocation_ea, *args, **kwargs): super(ReferenceInterviewForm, self).__init__(request, access, *args, **kwargs) self.survey = survey self.random_samples = ListingSample.get_or_create_samples(survey, allocation_ea).order_by('interview__created') choices = [(idx + 1, sample.get_display_label()) for idx, sample in enumerate(self.random_samples)] self.fields['value'] = forms.ChoiceField(choices=choices, widget=forms.RadioSelect) if self.access.choice_name() == USSDAccess.choice_name(): self.fields['value'].widget = forms.NumberInput() self.listing_form = survey.preferred_listing.listing_form if survey.preferred_listing else survey.listing_form self.fields['value'].label = 'Select %s' % self.listing_form.name
def _respond(self, message): next_house = self.next_house household = Household.objects.create( house_number=next_house, listing=self.house_listing, last_registrar=self.interviewer, registration_channel=USSDAccess.choice_name(), physical_address=message) self.next_house = next_house + 1 task = RegisterMember(self.access) task._household = household return task.intro()
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_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 __init__(self, survey, *args, **kwargs): super(SamplingCriterionForm, self).__init__(*args, **kwargs) self.fields['survey'].initial = survey.pk self.fields['survey'].widget = forms.HiddenInput() if survey.listing_form: self.fields['listing_question'].queryset = survey.listing_form.questions.filter( answer_type__in=[ defin.answer_type for defin in AnswerAccessDefinition.objects.filter( channel=USSDAccess.choice_name())]) self.fields['listing_question'].empty_label = 'Code - Question' self.order_fields(['listing_question', 'validation_test', 'options', 'value', 'min', 'max', 'survey']) if self.data.get('listing_question', []): options = QuestionOption.objects.filter( question__pk=self.data['listing_question']) self.fields['options'].choices = [ (opt.text, opt.text) for opt in options]
def _respond(self, message): if self.hl_prompts.keys()[0] in ['head_sex', 'register_another' ] and message not in ['1', '2']: return self.intro() if len(self.hl_prompts) > 0: hl_prompts = self.hl_prompts prompt = hl_prompts.popitem(0) self.hl_prompts = hl_prompts results = self.hl_results results[prompt[0]] = message self.hl_results = results if prompt[0] == 'head_sex': #save on last house attribute next_house = self.next_house Household.objects.create( house_number=next_house, listing=self.house_listing, last_registrar=self.interviewer, registration_channel=USSDAccess.choice_name(), physical_address=self.hl_results['physical_address'], head_desc=self.hl_results['head_desc'], head_sex=self.hl_results['head_sex']) self.next_house = next_house + 1 if prompt[0] == 'register_another': if message == str(ListHouseholds.REGISTER_ANOTHER): return self.restart_listing() else: survey_allocation = self.survey_allocation survey_allocation.stage = SurveyAllocation.SURVEY survey_allocation.save() self.survey_allocation = survey_allocation self.interviewer.generate_survey_households( self.ongoing_survey) return Start(self.access).intro() #back to main menu return self.hl_prompts.values()[0] else: return Start(self.access).intro()
def handle(self, *args, **kwargs): self.stdout.write('Creating permissions....') content_type = ContentType.objects.get_for_model(User) Permission.objects.get_or_create(codename='can_enter_data', name='Can enter data', content_type=content_type) Permission.objects.get_or_create(codename='can_view_batches', name='Can view Batches', content_type=content_type) Permission.objects.get_or_create(codename='can_view_interviewers', name='Can view Interviewers', content_type=content_type) Permission.objects.get_or_create(codename='can_view_aggregates', name='Can view Aggregates', content_type=content_type) Permission.objects.get_or_create(codename='view_completed_survey', name='Can view Completed Surveys', content_type=content_type) Permission.objects.get_or_create(codename='can_view_households', name='Can view Households', content_type=content_type) Permission.objects.get_or_create(codename='can_view_locations', name='Can view Locations', content_type=content_type) Permission.objects.get_or_create(codename='can_view_users', name='Can view Users', content_type=content_type) Permission.objects.get_or_create(codename='can_view_household_groups', name='Can view Household Groups', content_type=content_type) self.stdout.write('Permissions.') self.stdout.write('Creating answer definition... ') #ussd definition AnswerAccessDefinition.objects.get_or_create(channel=USSDAccess.choice_name(), answer_type=NumericalAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=USSDAccess.choice_name(), answer_type=TextAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=USSDAccess.choice_name(), answer_type=MultiChoiceAnswer.choice_name()) #ODK definition AnswerAccessDefinition.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=NumericalAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=TextAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=MultiChoiceAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=MultiSelectAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=ImageAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=GeopointAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=DateAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=AudioAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=VideoAnswer.choice_name()) #web form definition AnswerAccessDefinition.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=NumericalAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=TextAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=MultiChoiceAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=MultiSelectAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=ImageAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=GeopointAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=DateAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=AudioAnswer.choice_name()) AnswerAccessDefinition.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=VideoAnswer.choice_name()) self.stdout.write('Successfully imported!')
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_fields(self): ussd = USSDAccess() fields = [str(item.attname) for item in ussd._meta.fields] self.assertEqual(10, len(fields)) for field in ['intervieweraccess_ptr_id', 'aggregator']: self.assertIn(field, fields)