def test_create_answers(self): qset = self.qset self._create_test_non_group_questions(qset) geo_question = Question.objects.filter(answer_type=GeopointAnswer.choice_name()).first() latitude = '23.2' longitiude = '-30.34' altitude = '100' precision = '5' # Test ODK Geopoint GeopointAnswer.create(self.interview, geo_question, ' '.join([latitude, longitiude, altitude, precision])) self.assertEquals(GeopointAnswer.objects.count(), 1)
def test_create_answers(self): qset = self.qset self._create_test_non_group_questions(qset) geo_question = Question.objects.filter( answer_type=GeopointAnswer.choice_name()).first() latitude = '23.2' longitiude = '-30.34' altitude = '100' precision = '5' # Test ODK Geopoint GeopointAnswer.create( self.interview, geo_question, ' '.join([latitude, longitiude, altitude, precision])) self.assertEquals(GeopointAnswer.objects.count(), 1)
def clean_value(self): if self.cleaned_data.get('value'): if question.answer_type == MultiChoiceAnswer.choice_name(): try: self.cleaned_data['value'] = question.options.get( order=self.cleaned_data['value']) except QuestionOption.DoesNotExist: raise ValidationError('Please select a valid option') if question.answer_type == GeopointAnswer.choice_name(): float_entries = self.cleaned_data['value'].split(' ') valid = False try: map(lambda entry: float(entry), float_entries) if len(float_entries) == 4: valid = True except BaseException: pass if not valid: raise ValidationError( 'Please enter in format: lat[space]long[space]altitude[space]precision' ) # validate the response if the last question has validation if interview.last_question and interview.last_question.response_validation: response_validation = interview.last_question.response_validation if response_validation.validate( self.cleaned_data['value'], interview.last_question) is False: raise ValidationError( response_validation.dconstraint_message) return self.cleaned_data['value']
def clean_value(self): if self.cleaned_data.get('value'): if question.answer_type == MultiChoiceAnswer.choice_name(): try: self.cleaned_data['value'] = question.options.get( order=self.cleaned_data['value']) except QuestionOption.DoesNotExist: raise ValidationError('Please select a valid option') if question.answer_type == GeopointAnswer.choice_name(): float_entries = self.cleaned_data['value'].split(' ') valid = False try: map(lambda entry: float(entry), float_entries) if len(float_entries) == 4: valid = True except BaseException: pass if not valid: raise ValidationError( 'Please enter in format: lat[space]long[space]altitude[space]precision') # validate the response if the last question has validation if interview.last_question and interview.last_question.response_validation: response_validation = interview.last_question.response_validation if response_validation.validate(self.cleaned_data['value'], interview.last_question) is False: raise ValidationError(response_validation.dconstraint_message) return self.cleaned_data['value']
def full_clean(self): try: return super(AnswerForm, self).full_clean() except ValueError: if question.answer_type == GeopointAnswer.choice_name(): self.cleaned_data['value'] = self.data['value'] else: raise
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 _create_test_non_group_questions(self, qset): # Basically create questions for this question set which is not having groups self._create_ussd_non_group_questions(qset) # Multiselect questions data = { 'answer_type': MultiSelectAnswer.choice_name(), 'text': 'multi select answer text', 'identifier': 'multi2_select_identifier_%s' % random.randint(1, 100), 'qset': qset.id, 'options': ['Y', 'N', 'MB'] } self._save_question(qset, data) # Date answer data = { 'answer_type': DateAnswer.choice_name(), 'text': 'date answer text', 'identifier': 'date2_identifier_%s' % random.randint(1, 100), 'qset': qset.id, } self._save_question(qset, data) # Geopoint answer data = { 'answer_type': GeopointAnswer.choice_name(), 'text': 'geo point text', 'identifier': 'geo2_identifier_%s' % random.randint(1, 100), 'qset': qset.id } self._save_question(qset, data) # Image answer data = { 'answer_type': ImageAnswer.choice_name(), 'text': 'image answer text', 'identifier': 'image2_identifier_%s' % random.randint(1, 100), 'qset': qset.id } self._save_question(qset, data) # Audio answer data = { 'answer_type': AudioAnswer.choice_name(), 'text': 'audio answer text', 'identifier': 'audio2_identifier_%s' % random.randint(1, 100), 'qset': qset.id } self._save_question(qset, data) # Video answer data = { 'answer_type': VideoAnswer.choice_name(), 'text': 'video answer text', 'identifier': 'video2_identifier_%s' % random.randint(1, 100), 'qset': qset.id } self._save_question(qset, data) self.qset.refresh_from_db()
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 test_answer_validators(self): validators = [ # supported validators 'starts_with', 'ends_with', 'equals', 'between', 'less_than', 'greater_than', 'contains', ] for func in Answer.validators(): self.assertIn(func.__name__, validators) # test Numeric Answer Validators validators = [ # supported validators 'equals', 'between', 'less_than', 'greater_than', ] for func in NumericalAnswer.validators(): self.assertIn(func.__name__, validators) # test Text Answer Validators validators = [ # supported validators 'starts_with', 'ends_with', 'equals', 'contains', ] for func in TextAnswer.validators(): self.assertIn(func.__name__, validators) # Multichoice validators = [ # supported validators 'equals', ] for func in MultiChoiceAnswer.validators(): self.assertIn(func.__name__, validators) # test Multiselect Answer Validators validators = [ # supported validators 'equals', 'contains', ] for func in MultiSelectAnswer.validators(): self.assertIn(func.__name__, validators) # test Date Answer Validators validators = [ # supported validators 'equals', 'between', 'less_than', 'greater_than', ] for func in DateAnswer.validators(): self.assertIn(func.__name__, validators) # GeoPoint Answer validators = [ # supported validators 'equals', ] for func in GeopointAnswer.validators(): self.assertIn(func.__name__, validators) # file answers should return no validators for answer_class in [VideoAnswer, AudioAnswer, ImageAnswer]: self.assertEquals(len(answer_class.validators()), 0)
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!')