def new(request, household_id): member_form = HouseholdMemberForm() breadcrumbs = [('Households', reverse('list_household_page')),] try: household = Household.objects.get(id=household_id) breadcrumbs.append(('Household', reverse('view_household_page', args=(household_id, ))),) if request.method == 'POST': member_form = HouseholdMemberForm(data=request.POST) interviewer = household.last_registrar survey = SurveyAllocation.get_allocation(interviewer) if member_form.is_valid(): household_member = member_form.save(commit=False) household_member.household = household household_member.registrar = household.last_registrar household_member.survey_listing = SurveyHouseholdListing.get_or_create_survey_listing(interviewer, survey) household_member.registration_channel = WebAccess.choice_name() household_member.save() messages.success(request, 'Household member successfully created.') return HttpResponseRedirect('/households/%s/'%(str(household_id))) except Household.DoesNotExist: messages.error(request, 'There are no households currently registered for this ID.') return HttpResponseRedirect('/households/') request.breadcrumbs(breadcrumbs) return render(request, 'household_member/new.html', {'member_form': member_form, 'button_label': 'Create'})
def reload_answer_categories(cls): from survey.models import USSDAccess, ODKAccess, WebAccess cls.objects.get_or_create(channel=USSDAccess.choice_name(), answer_type=AutoResponse.choice_name()) cls.objects.get_or_create(channel=USSDAccess.choice_name(), answer_type=NumericalAnswer.choice_name()) cls.objects.get_or_create(channel=USSDAccess.choice_name(), answer_type=TextAnswer.choice_name()) cls.objects.get_or_create(channel=USSDAccess.choice_name(), answer_type=MultiChoiceAnswer.choice_name()) # ODK definition cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=AutoResponse.choice_name()) cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=NumericalAnswer.choice_name()) cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=TextAnswer.choice_name()) cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=MultiChoiceAnswer.choice_name()) cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=MultiSelectAnswer.choice_name()) cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=ImageAnswer.choice_name()) cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=GeopointAnswer.choice_name()) cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=DateAnswer.choice_name()) cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=AudioAnswer.choice_name()) cls.objects.get_or_create(channel=ODKAccess.choice_name(), answer_type=VideoAnswer.choice_name()) # web form definition cls.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=NumericalAnswer.choice_name()) cls.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=TextAnswer.choice_name()) cls.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=MultiChoiceAnswer.choice_name()) cls.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=MultiSelectAnswer.choice_name()) cls.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=ImageAnswer.choice_name()) cls.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=GeopointAnswer.choice_name()) cls.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=DateAnswer.choice_name()) cls.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=AudioAnswer.choice_name()) cls.objects.get_or_create(channel=WebAccess.choice_name(), answer_type=VideoAnswer.choice_name())
def __init__(self, is_edit=False, eas=[], survey=None, *args, **kwargs): super(HouseholdForm, self).__init__(*args, **kwargs) self.is_editing = is_edit self.fields['registration_channel'].initial = WebAccess.choice_name() if eas: self.fields[ 'last_registrar'].queryset = Interviewer.objects.filter( ea__pk__in=[ea.pk for ea in eas])
class QuestionSetForm(ModelForm): access_channels = forms.MultipleChoiceField( widget=forms.CheckboxSelectMultiple( attrs={'class': 'access_channels'}), choices=[ opt for opt in QuestionSetChannel.ACCESS_CHANNELS if not opt[0] == WebAccess.choice_name() ]) def __init__(self, *args, **kwargs): if kwargs.get('instance'): initial = kwargs.setdefault('initial', {}) initial['access_channels'] = [ c.channel for c in kwargs['instance'].access_channels.all() ] #self.fields['validation'] = kwargs['instance'] super(QuestionSetForm, self).__init__(*args, **kwargs) class Meta: model = model_class fields = [ 'name', 'description', ] widgets = { 'name': forms.TextInput( attrs={ 'size': 29, 'title': 'Your name', 'style': 'height: 2em;width:231px;' }), 'description': forms.Textarea(attrs={ "rows": 5, "cols": 30 }), } def clean_name(self): name = self.cleaned_data['name'].strip() if self.instance is None and model_class.objects.filter( name=name).exists(): raise ValidationError('Name already exists') return name def save(self, commit=True, **kwargs): question_set = super(QuestionSetForm, self).save(commit=commit) bc = QuestionSetChannel.objects.filter(qset=question_set) bc.delete() for val in kwargs['access_channels']: QuestionSetChannel.objects.create(qset=question_set, channel=val) return question_set
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()))
class BatchForm(ModelForm): access_channels = forms.MultipleChoiceField( widget=forms.CheckboxSelectMultiple( attrs={'class': 'access_channels'}), choices=[ opt for opt in BatchChannel.ACCESS_CHANNELS if not opt[0] == WebAccess.choice_name() ]) def __init__(self, *args, **kwargs): if kwargs.get('instance'): initial = kwargs.setdefault('initial', {}) initial['access_channels'] = [ c.channel for c in kwargs['instance'].access_channels.all() ] forms.ModelForm.__init__(self, *args, **kwargs) class Meta: model = Batch fields = [ 'name', 'description', 'survey', ] widgets = { 'description': forms.Textarea(attrs={ "rows": 3, "cols": 30 }), 'survey': forms.HiddenInput(), } def save(self, commit=True, **kwargs): batch = super(BatchForm, self).save(commit=commit) bc = BatchChannel.objects.filter(batch=batch) bc.delete() for val in kwargs['access_channels']: BatchChannel.objects.create(batch=batch, channel=val)
def new(request, household_id): member_form = HouseholdMemberForm() breadcrumbs = [ ('Households', reverse('list_household_page')), ] try: household = Household.objects.get(id=household_id) breadcrumbs.append( ('Household', reverse('view_household_page', args=(household_id, ))), ) if request.method == 'POST': member_form = HouseholdMemberForm(data=request.POST) interviewer = household.last_registrar survey = SurveyAllocation.get_allocation(interviewer) if member_form.is_valid(): household_member = member_form.save(commit=False) household_member.household = household household_member.registrar = household.last_registrar household_member.survey_listing = SurveyHouseholdListing.get_or_create_survey_listing( interviewer, survey) household_member.registration_channel = WebAccess.choice_name() household_member.save() messages.success(request, 'Household member successfully created.') return HttpResponseRedirect('/households/%s/' % (str(household_id))) except Household.DoesNotExist: messages.error( request, 'There are no households currently registered for this ID.') return HttpResponseRedirect('/households/') request.breadcrumbs(breadcrumbs) return render(request, 'household_member/new.html', { 'member_form': member_form, 'button_label': 'Create' })
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, is_edit=False, eas=[], survey=None, *args, **kwargs): super(HouseholdForm, self).__init__(*args, **kwargs) self.is_editing = is_edit self.fields['registration_channel'].initial = WebAccess.choice_name() if eas: self.fields['last_registrar'].queryset = Interviewer.objects.filter(ea__pk__in=[ea.pk for ea in eas])