Exemplo n.º 1
0
 def test_04_name_blank(self):
     form = WidgetForm(data=self.data_blank)
     self.assertFalse(form.is_valid())
     self.assertTrue(form.is_bound)
     self.assertEqual(1, len(form.errors['name']))
     self.assertEqual(u'This field is required.',
                      form.errors['name'][0])
Exemplo n.º 2
0
def widget_update(request, pk):
    if request.method not in ['GET', 'POST']:
        return Http405()
    template = 'simple/update_form.html'
    context = _get_context()
    context['title'] = 'Update an Existing Widget'
    widget = get_object_or_404(Widget,
                               pk=pk,
                               created_by=request.user,
                               deleted=False)
    if request.method == 'POST':
        form = WidgetForm(request.POST)
        if form.is_valid():
            print('WidgetForm cleaned_data: {}'.format(form.cleaned_data))
            widget.name = form.cleaned_data['name']
            widget.sku = form.cleaned_data['sku']
            widget.cost = form.cleaned_data['cost']
            widget.save()
            return HttpResponseRedirect(
                reverse_lazy('simple:updated', kwargs={'pk': pk}))
        else:
            context['form'] = form
            return render(request, template, context)
    else:
        initial = {
            'created_by': request.user,
            'name': widget.name,
            'sku': widget.sku,
            'cost': widget.cost
        }
        context['form'] = WidgetForm(initial=initial)
        return render(request, template, context)
Exemplo n.º 3
0
def widget_create(request):
    if request.method not in ['GET', 'POST']:
        return Http405()
    context = _get_context()
    context['title'] = 'Create a Widget'
    template = 'simple/create_form.html'
    if request.method == 'POST':
        form = WidgetForm(data=request.POST)
        if form.is_valid():
            form.cleaned_data.update({'created_by': request.user})
            try:
                widget = Widget(**form.cleaned_data)
                widget.save()
                widget.link = reverse_lazy('simple:widget-detail',
                                           kwargs={'pk': widget.pk})
                widget.ulink = reverse_lazy('simple:widget-update',
                                            kwargs={'pk': widget.pk})
                widget.dlink = reverse_lazy('simple:widget-delete',
                                            kwargs={'pk': widget.pk})
                return HttpResponseRedirect(
                    reverse_lazy('simple:created', kwargs={'pk': widget.pk}))
            except IntegrityError as e:
                context['form'] = _duplicate_key(form)
                return render(request, template, context)
        else:
            context['form'] = form
            return render(request, template, context)
    else:
        initial = {'name': 'myWidget', 'sku': '111-111-11', 'cost': 0.00}
        context['form'] = WidgetForm(initial=initial)
        return render(request, template, context)
Exemplo n.º 4
0
 def test_02_name_too_long(self):
     form = WidgetForm(data=self.data_long)
     self.assertFalse(form.is_valid())
     self.assertTrue(form.is_bound)
     self.assertEqual(1, len(form.errors['name']))
     errmsg = u'Ensure this value has at most 20 characters (it has 21).'
     self.assertEqual(errmsg,
                      form.errors['name'][0])
Exemplo n.º 5
0
def widget_delete(request, pk):
    if request.method not in ['GET', 'POST']:
        return Http405()
    context = _get_context()
    context['title'] = 'Delete an Existing Widget'
    template = 'simple/delete_form.html'
    if request.method == 'POST':
        form = WidgetForm(request.POST)
        if form.is_valid():
            try:
                widget = Widget.objects.get(pk=pk, created_by=request.user)
                widget.deleted = True
                widget.save()
                return HttpResponseRedirect(
                    reverse_lazy('simple:deleted', kwargs={'pk': pk}))
            except Widget.DoesNotExist as e:
                return HttpResponseRedirect(
                    reverse_lazy('simple:non-existent', kwargs={'pk': pk}))
            except IntegrityError as e:
                context['form'] = _duplicate_key(form)
                return render(request, template, context)
        else:
            context['form'] = form
            return render(request, template, context)
    else:
        widget = Widget.objects.get(pk=pk,
                                    created_by=request.user,
                                    deleted=False)
        initial = {
            'created_by': request.user,
            'name': widget.name,
            'sku': widget.sku,
            'cost': widget.cost
        }
        context['form'] = WidgetForm(initial=initial)
        return render(request, template, context)
Exemplo n.º 6
0
 def test_05_object(self):
     form = WidgetForm(data=self.data_valid)
     self.assertIn('name', str(form))
     self.assertEqual('<WidgetForm bound=True, valid=True, fields=(name;sku;cost)>',
                      repr(form))
Exemplo n.º 7
0
 def test_01_name_valid(self):
     form = WidgetForm(data=self.data_valid)
     self.assertTrue(form.is_valid())
     self.assertTrue(form.is_bound)
     self.assertEqual(self.data_valid['name'],
                      form.data['name'])