コード例 #1
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)
コード例 #2
0
ファイル: test_section_form.py プロジェクト: testvidya11/ejrf
    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)
コード例 #3
0
ファイル: test_section_form.py プロジェクト: eJRF/ejrf
    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)
コード例 #4
0
ファイル: test_section_form.py プロジェクト: testvidya11/ejrf
    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)
コード例 #5
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)
コード例 #6
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)