コード例 #1
0
ファイル: sections.py プロジェクト: remo4sam/ejrf
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)
コード例 #2
0
ファイル: test_section_form.py プロジェクト: testvidya11/ejrf
    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())
コード例 #3
0
ファイル: sections.py プロジェクト: testvidya11/ejrf
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)
コード例 #4
0
ファイル: test_section_form.py プロジェクト: testvidya11/ejrf
    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())
コード例 #5
0
ファイル: test_section_form.py プロジェクト: testvidya11/ejrf
    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'])
コード例 #6
0
ファイル: test_section_form.py プロジェクト: testvidya11/ejrf
    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'])
コード例 #7
0
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)
コード例 #8
0
ファイル: test_section_form.py プロジェクト: testvidya11/ejrf
 def test_valid(self):
     subsection_form = SubSectionForm(initial={'section': self.section.id},
                                      data=self.form_data)
     self.assertTrue(subsection_form.is_valid())
コード例 #9
0
ファイル: test_section_form.py プロジェクト: testvidya11/ejrf
 def test_valid(self):
     subsection_form = SubSectionForm(initial={'section': self.section.id}, data=self.form_data)
     self.assertTrue(subsection_form.is_valid())