def test_04_name_blank(self): form = StoreForm(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])
def store_delete(request, pk): if request.method not in ['GET', 'POST']: return Http405() context = _get_context() context['title'] = 'Delete an Existing Store' template = 'simple/delete_form.html' store = get_object_or_404(Store, pk=pk, created_by=request.user) if request.method == 'POST': form = StoreForm(request.POST, instance=store) if form.is_valid(): store.deleted = True store.save() return HttpResponseRedirect( reverse_lazy('simple:deleted', kwargs={'pk': pk})) else: context['form'] = form return render(request, template, context) else: initial = { 'created_by': request.user, 'name': store.name, 'location': store.location } context['form'] = StoreForm(initial=initial) return render(request, template, context)
def store_create(request): if request.method not in ['GET', 'POST']: return Http405() context = _get_context() context['title'] = 'Create a Store' template = 'simple/create_form.html' if request.method == 'POST': form = StoreForm(data=request.POST) if form.is_valid(): form.cleaned_data.update({'created_by': request.user}) try: store = Store(**form.cleaned_data) store.save() store.link = reverse_lazy('simple:store-detail', kwargs={'pk': store.pk}) store.ulink = reverse_lazy('simple:store-update', kwargs={'pk': store.pk}) store.dlink = reverse_lazy('simple:store-delete', kwargs={'pk': store.pk}) return HttpResponseRedirect( reverse_lazy('simple:created', kwargs={'pk': store.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': 'myStore', 'location': 'El Cerrito'} context['form'] = StoreForm(initial=initial) return render(request, template, context)
def test_02_name_too_long(self): form = StoreForm(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])
def test_05_object(self): form = StoreForm(data=self.data_valid) self.assertIn('name', str(form)) self.assertEqual('<StoreForm bound=True, valid=True, fields=(name;location)>', repr(form))
def test_01_name_valid(self): form = StoreForm(data=self.data_valid) self.assertTrue(form.is_valid()) self.assertTrue(form.is_bound) self.assertEqual(self.data_valid['name'], form.data['name'])