def addfiletype(request): if request.method == 'POST': form = forms.SourceFileTypeForm(request.POST) if form.is_valid(): form.save() return redirect('docserver-filetypes') else: form = forms.SourceFileTypeForm() ret = {"form": form, "mode": "add"} return render(request, 'docserver/addfiletype.html', ret)
def editfiletype(request, slug): ft = get_object_or_404(models.SourceFileType, slug=slug) if request.method == 'POST': form = forms.SourceFileTypeForm(request.POST, instance=ft) if form.is_valid(): form.save() return redirect('docserver-filetypes') else: form = forms.SourceFileTypeForm(instance=ft) ret = {"form": form, "mode": "edit"} return render(request, 'docserver/addfiletype.html', ret)
def test_add_filetype(self): self.assertEqual(0, models.SourceFileType.objects.all().count()) formdata = "slug=myslug&extension=ext&mimetype=application%2Ffoo&name=thename" data = QueryDict(formdata) f = forms.SourceFileTypeForm(data) f.save() self.assertEqual(1, models.SourceFileType.objects.all().count()) sf = models.SourceFileType.objects.get() self.assertEqual("application/foo", sf.mimetype)
def test_add_existing_slug(self): models.SourceFileType.objects.create(slug="aslug", extension="x", mimetype="x", name="some filetype") formdata = "slug=aslug&extension=ext&mimetype=application%2Ffoo&name=thename" data = QueryDict(formdata) f = forms.SourceFileTypeForm(data) self.assertFalse(f.is_valid())