def resource_edit_step2_view(request, resource_id): if request.user.is_anonymous: # TODO use contrib.messages return render( request, 'orb/login_required.html', { 'message': _('You need to be logged in to add a resource.'), }) resource = get_object_or_404(Resource, pk=resource_id) # check if owner of this resource if not resource_can_edit(resource, request.user): raise Http404() if request.method == 'POST': form = ResourceStep2Form(request.POST, request.FILES, request=request) if form.is_valid(): title = form.cleaned_data.get("title") # add file and url if request.FILES.has_key('file'): rf = ResourceFile(resource=resource, create_user=request.user, update_user=request.user) rf.file = request.FILES["file"] if title: rf.title = title rf.save() url = form.cleaned_data.get("url") if url: ru = ResourceURL(resource=resource, create_user=request.user, update_user=request.user) ru.url = url if title: ru.title = title ru.save() initial = {} form = ResourceStep2Form(initial=initial, request=request) resource_files = ResourceFile.objects.filter(resource=resource) resource_urls = ResourceURL.objects.filter(resource=resource) return render( request, 'orb/resource/edit_step2.html', { 'form': form, 'resource': resource, 'resource_files': resource_files, 'resource_urls': resource_urls, })
def test_valid_url(self): """Form should be valid with only a URL Unicode is used for good measure to test encoding validation """ form = ResourceStep2Form(data={ "url": u"http://www.wvi.org/publication/manual-para-madres-gu%C3%ADas" }) self.assertTrue(form.is_valid())
def test_file_invalid_type(self): """Form should be invalid if file type is blacklisted""" form = ResourceStep2Form(files={"file": self.uploaded_file}) self.assertFalse(form.is_valid())
def test_file_exceeds_maxsize(self): """Form should be invalid if file size exceeds maxsize""" self.assertEqual(settings.TASK_UPLOAD_FILE_MAX_SIZE, 90) form = ResourceStep2Form(files={"file": self.uploaded_file}) self.assertFalse(form.is_valid())
def test_valid_file(self): """Form should be valid with only a file""" form = ResourceStep2Form(files={"file": self.uploaded_file}) self.assertTrue(form.is_valid())
def test_invalid_url(self): """Form shoudl reject invalid URLs""" form = ResourceStep2Form(data={"url": u"htp://example.com/olé"}) self.assertFalse(form.is_valid())
def test_valid_missing_url_file(self): """Form should be invalid when missing both file and URL""" form = ResourceStep2Form(data={"title": u"¡Olé!"}) self.assertFalse(form.is_valid())