def test_empty_description_is_invalid(self): data = self.form_data.copy() data['description'] = '' subsection_form = SubSectionForm(initial={'section': self.section.id}, data=data) self.assertTrue(subsection_form.is_valid())
class NewSubSection(RegionAndPermissionRequiredMixin, CreateView): permission_required = 'auth.can_edit_questionnaire' def __init__(self, **kwargs): super(NewSubSection, self).__init__(**kwargs) self.object = SubSection self.form_class = SubSectionForm self.template_name = "sections/subsections/new.html" def get_context_data(self, **kwargs): context = super(NewSubSection, self).get_context_data(**kwargs) context['btn_label'] = "CREATE" return context def post(self, request, *args, **kwargs): questionnaire_id = kwargs.get('questionnaire_id') section_id = kwargs.get('section_id') self.section = Section.objects.get(id=section_id) self.form = SubSectionForm(instance=SubSection(section=self.section), data=request.POST) self.referer_url = reverse('questionnaire_entry_page', args=(questionnaire_id, section_id)) if self.form.is_valid(): return self._form_valid() def _form_valid(self): subsection = self.form.save(commit=False) subsection.order = SubSection.get_next_order(self.section.id) subsection.region = self.request.user.user_profile.region subsection.save() messages.success(self.request, "Subsection successfully created." ) return HttpResponseRedirect(self.referer_url)
class NewSubSection(PermissionRequiredMixin, CreateView): permission_required = 'auth.can_edit_questionnaire' def __init__(self, **kwargs): super(NewSubSection, self).__init__(**kwargs) self.object = SubSection self.form_class = SubSectionForm self.template_name = "sections/subsections/new.html" def get_context_data(self, **kwargs): context = super(NewSubSection, self).get_context_data(**kwargs) context['btn_label'] = "CREATE" return context def post(self, request, *args, **kwargs): questionnaire_id = kwargs.get('questionnaire_id') section_id = kwargs.get('section_id') section = Section.objects.get(id=section_id) self.form = SubSectionForm(instance=SubSection(section=section), data=request.POST) self.referer_url = reverse('questionnaire_entry_page', args=(questionnaire_id, section_id)) if self.form.is_valid(): return self._form_valid() return self._form_invalid() def _form_valid(self): self.form.save() messages.success(self.request, "Subsection successfully created." ) return HttpResponseRedirect(self.referer_url) def _form_invalid(self): messages.error(self.request, "Subsection NOT created. See errors below." ) context = {'id': "new-subsection-modal", 'form': self.form, 'btn_label': "CREATE", } return self.render_to_response(context)
def test_empty_title_is_invalid(self): data = self.form_data.copy() data['title'] = '' subsection_form = SubSectionForm(initial={'section': self.section.id}, data=data) self.assertFalse(subsection_form.is_valid()) message = 'This field is required.' self.assertEqual([message], subsection_form.errors['title'])
def test_save_increment_order(self): existing_subs = SubSection.objects.create(title="subsection 1", section=self.section, order=1) data = self.form_data.copy() subsection_form = SubSectionForm(instance=SubSection(section=self.section), data=data) subsection_form.save() new_subs = SubSection.objects.filter(section=self.section, **data) self.failUnless(new_subs) self.assertEqual(1, new_subs.count()) self.assertEqual(existing_subs.order + 1, new_subs[0].order)
def post(self, request, *args, **kwargs): questionnaire_id = kwargs.get('questionnaire_id') section_id = kwargs.get('section_id') section = Section.objects.get(id=section_id) self.form = SubSectionForm(instance=SubSection(section=section), data=request.POST) self.referer_url = reverse('questionnaire_entry_page', args=(questionnaire_id, section_id)) if self.form.is_valid(): return self._form_valid() return self._form_invalid()
def test_save_does_not_increment_order_if_instance_of_section_is_given_and_it_has_order(self): subsection_order = 1 existing_subs = SubSection.objects.create(title="subsection 1", section=self.section, order=subsection_order) data = self.form_data.copy() subsection_form = SubSectionForm(instance=existing_subs, data=data) subsection_form.save() new_subs = SubSection.objects.filter(section=self.section, **data) self.failUnless(new_subs) self.assertEqual(1, new_subs.count()) self.assertEqual(subsection_order, new_subs[0].order)
def test_save_increment_order(self): existing_subs = SubSection.objects.create(title="subsection 1", section=self.section, order=1) data = self.form_data.copy() subsection_form = SubSectionForm( instance=SubSection(section=self.section), data=data) subsection_form.save() new_subs = SubSection.objects.filter(section=self.section, **data) self.failUnless(new_subs) self.assertEqual(1, new_subs.count()) self.assertEqual(existing_subs.order + 1, new_subs[0].order)
def post(self, request, *args, **kwargs): questionnaire_id = kwargs.get('questionnaire_id') section_id = kwargs.get('section_id') self.section = Section.objects.get(id=section_id) self.form = SubSectionForm(instance=SubSection(section=self.section), data=request.POST) self.referer_url = reverse('questionnaire_entry_page', args=(questionnaire_id, section_id)) if self.form.is_valid(): return self._form_valid()
class NewSubSection(PermissionRequiredMixin, CreateView): permission_required = 'auth.can_edit_questionnaire' def __init__(self, **kwargs): super(NewSubSection, self).__init__(**kwargs) self.object = SubSection self.form_class = SubSectionForm self.template_name = "sections/subsections/new.html" def get_context_data(self, **kwargs): context = super(NewSubSection, self).get_context_data(**kwargs) context['btn_label'] = "CREATE" return context def post(self, request, *args, **kwargs): questionnaire_id = kwargs.get('questionnaire_id') section_id = kwargs.get('section_id') section = Section.objects.get(id=section_id) self.form = SubSectionForm(instance=SubSection(section=section), data=request.POST) self.referer_url = reverse('questionnaire_entry_page', args=(questionnaire_id, section_id)) if self.form.is_valid(): return self._form_valid() return self._form_invalid() def _form_valid(self): self.form.save() messages.success(self.request, "Subsection successfully created.") return HttpResponseRedirect(self.referer_url) def _form_invalid(self): messages.error(self.request, "Subsection NOT created. See errors below.") context = { 'id': "new-subsection-modal", 'form': self.form, 'btn_label': "CREATE", } return self.render_to_response(context)
def get(self, request, *args, **kwargs): questionnaire = Questionnaire.objects.get( id=self.kwargs['questionnaire_id']) section = Section.objects.get(id=self.kwargs['section_id']) initial = { 'status': 'Draft', 'country': self.request.user.user_profile.country } required_answers = 'show' in request.GET formsets = QuestionnaireEntryFormService(section, initial=initial, highlight=required_answers) printable = 'printable' in request.GET preview = 'preview' in request.GET context = { 'questionnaire': questionnaire, 'section': section, 'printable': printable, 'preview': preview, 'formsets': formsets, 'ordered_sections': questionnaire.sections.order_by('order'), 'form': SectionForm(initial={'questionnaire': questionnaire}), 'action': reverse('new_section_page', args=(questionnaire.id, )), 'subsection_form': SubSectionForm(), 'subsection_action': reverse('new_subsection_page', args=(questionnaire.id, section.id)) } return self.render_to_response(context)
def test_valid(self): subsection_form = SubSectionForm(initial={'section': self.section.id}, data=self.form_data) self.assertTrue(subsection_form.is_valid())