def test_save(self): post = {'comments0': 'abc', 'comments1': 'def'} files = {'file0': SimpleUploadedFile('0.txt', 'text/plain', 'abc'), 'file1': SimpleUploadedFile('1.txt', 'text/plain', 'def')} f = MultipleFileUploadForm(self.user, [0, 1], post, files) self.assert_(f.is_valid()) f.save() self.assertEquals(File.objects.count(), 3) self.assertEquals(os.path.basename(File.objects.get(pk=2).file.name), '0.txt') self.assertEquals(File.objects.get(pk=2).comments, 'abc') self.assertEquals(os.path.basename(File.objects.get(pk=3).file.name), '1.txt') self.assertEquals(File.objects.get(pk=3).comments, 'def')
def add_file(request, user_id): user = get_object_or_404(User, pk=user_id) # Only superusers or the owner can upload files to that profile. if not request.user.is_superuser and request.user != user: raise Http404 if request.method == "POST": xs = sorted(get_field_numbers(request.FILES)) new_post = select_fields(request.POST, xs) form = MultipleFileUploadForm(user, xs, new_post, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect(reverse("file_list")) else: form = SingleFileUploadForm(0) return render(request, "app/add_file.html", {"form": form, "user_id": user_id})