Exemplo n.º 1
0
 def get_handler():
     """Return a form for creating a category."""
     form_data = request.session.pop('form_data', None)
     if form_data is None:
         form = forms.CategoryForm()
     else:
         form = forms.CategoryForm(json.loads(form_data))
     return render(request, 'elts/category-create-form.html', {'form': form})
Exemplo n.º 2
0
 def get_handler():
     """Return a form for updating category ``category_id_``."""
     form_data = request.session.pop('form_data', None)
     if form_data is None:
         form = forms.CategoryForm(instance = category_)
     else:
         form = forms.CategoryForm(json.loads(form_data))
     return render(
         request,
         'elts/category-id-update-form.html',
         {'category': category_, 'form': form}
     )
Exemplo n.º 3
0
 def test_has_tags(self):
     """Create a CategoryForm and set ``tags``."""
     form = forms.CategoryForm({
         'name': factories.category_name(),
         'tags': [tag.id for tag in factories.category_tags()],
     })
     self.assertTrue(form.is_valid())
Exemplo n.º 4
0
    def put_handler():
        """Update category ``category_id_``.

        If update succeeds, redirect user to ``category_id`` view. Otherwise,
        redirect user to ``category_id_update_form``.

        """
        form = forms.CategoryForm(request.POST, instance = category_)
        if form.is_valid():
            form.save()
            return http.HttpResponseRedirect(
                reverse('elts.views.category_id', args = [category_id_])
            )
        else:
            request.session['form_data'] = json.dumps(form.data)
            return http.HttpResponseRedirect(
                reverse(
                    'elts.views.category_id_update_form',
                    args = [category_id_]
                )
            )
Exemplo n.º 5
0
    def post_handler():
        """Create a new category.

        If creation suceeds, redirect user to ``category_id`` view. Otherwise,
        redirect uesr to ``category_create_form`` view.

        """
        form = forms.CategoryForm(request.POST)
        if form.is_valid():
            # See CategoryForm docstring for explanation of this procedure.
            new_category = form.save(commit = False)
            new_category.user = request.user
            new_category.save()
            form.save_m2m()
            return http.HttpResponseRedirect(
                reverse('elts.views.category_id', args = [new_category.id])
            )
        else:
            request.session['form_data'] = json.dumps(form.data)
            return http.HttpResponseRedirect(
                reverse('elts.views.category_create_form')
            )
Exemplo n.º 6
0
 def test_valid(self):
     """Create a valid CategoryForm."""
     form = forms.CategoryForm({'name': factories.category_name()})
     self.assertTrue(form.is_valid())
Exemplo n.º 7
0
 def test_invalid_name(self):
     """Create a CategoryForm and set an invalid value for ``name``."""
     form = forms.CategoryForm({'name': factories.invalid_category_name()})
     self.assertFalse(form.is_valid())
Exemplo n.º 8
0
 def test_missing_name(self):
     """Create a CategoryForm without setting ``name``."""
     form = forms.CategoryForm({})
     self.assertFalse(form.is_valid())