def test_upload_form_both(self): image_test_url = 'https://upload.wikimedia.org/wikipedia/commons/e/e0/JPEG_example_JPG_RIP_050.jpg' with open("media/images/test/Slowpoke.jpeg", "rb") as image_test_file: form_data = {'url_file':image_test_url} form_files = {'upload_file':File(image_test_file)} form = ImageUploadForm(data=form_data, files=form_files) self.assertFalse(form.is_valid())
def upload_image(request): if request.method == "POST": context = RequestContext(request) auth = Author.objects.get(user=request.user) form = ImageUploadForm(request.POST, request.FILES) access = request.POST['access'] if form.is_valid(): url = "/media/Images/" + str(form.cleaned_data['image']) image = Image(image=form.cleaned_data['image'],url=url, visibility=access, author=auth) image.save() return HttpResponseRedirect("/images/")
def image_upload(request, source_id): """ View for uploading images to a source. Should be improved later to have AJAX uploading. """ source = get_object_or_404(Source, id=source_id) uploadedImages = [] if request.method == 'POST': imageForm = ImageUploadForm(request.POST, request.FILES) optionsForm = ImageUploadOptionsForm(request.POST, source=source) # Need getlist instead of simply request.FILES, in order to handle # multiple files. imageFiles = request.FILES.getlist('files') if imageForm.is_valid() and optionsForm.is_valid(): resultDict = image_upload_process( imageFiles=imageFiles, imageOptionsForm=optionsForm, annotationOptionsForm=None, source=source, currentUser=request.user, annoFile=None ) if resultDict['error']: messages.error(request, resultDict['message']) transaction.rollback() else: uploadedImages = resultDict['uploadedImages'] messages.success(request, resultDict['message']) else: messages.error(request, msg_consts.FORM_ERRORS) else: imageForm = ImageUploadForm() optionsForm = ImageUploadOptionsForm(source=source) return render_to_response('images/image_upload.html', { 'source': source, 'imageForm': imageForm, 'optionsForm': optionsForm, 'uploadedImages': uploadedImages, }, context_instance=RequestContext(request) )
def upload(request): """ "" The homepage for Posts. Displays the 5 most current entries and their "" entire accompanining posts. """ if request.method == 'POST': form = ImageUploadForm(request.POST, request.FILES) if form.is_valid(): filename, extension = os.path.splitext(request.FILES['file'].name) if extension in ('.jpg', '.jpeg', '.png', '.gif'): image = Image(user=request.user, filename=filename, extension=extension) image.save() image.save_image(request.FILES['file']) return HttpResponseRedirect("/images/")
def upload(request, gallery_uuid=None): context = {} if request.method == 'POST': form = ImageUploadForm(request.POST, request.FILES) if form.is_valid(): # fill some stuff if not gallery_uuid: gallery = request.user.galleries.get(title="Default") else: galleries = Gallery.objects.notdefault(user=request.user) gallery = get_object_or_404(galleries, uuid=gallery_uuid) obj = form.save(commit=False) obj.gallery = gallery obj.user = request.user obj.save() form.save_m2m() # fix for missing tags return redirect("upload_success", obj_uuid=obj.uuid) else: form = ImageUploadForm() context['form'] = form return render(request, "upload.html", context)
def annotation_import(request, source_id): """ Upload images and import their annotations from a text file. Should be improved later to have AJAX uploading. """ source = get_object_or_404(Source, id=source_id) uploadedImages = [] if request.method == 'POST': annotationForm = AnnotationImportForm(request.POST, request.FILES) imageForm = ImageUploadForm(request.POST, request.FILES) imageOptionsForm = ImageUploadOptionsForm(request.POST, source=source) annotationOptionsForm = AnnotationImportOptionsForm(request.POST) # Need getlist instead of simply request.FILES, in order to handle # multiple files. imageFiles = request.FILES.getlist('files') # TODO: imageForm.is_valid() just validates the first image file. # Make sure all image files are checked to be valid images. if (annotationForm.is_valid() and imageForm.is_valid() and annotationOptionsForm.is_valid() and imageOptionsForm.is_valid() ): annoFile = request.FILES['annotations_file'] resultDict = image_upload_process( imageFiles=imageFiles, imageOptionsForm=imageOptionsForm, annotationOptionsForm=annotationOptionsForm, source=source, currentUser=request.user, annoFile=annoFile ) if resultDict['error']: messages.error(request, resultDict['message']) transaction.rollback() revision_context_manager.invalidate() else: uploadedImages = resultDict['uploadedImages'] messages.success(request, resultDict['message']) else: messages.error(request, 'Please correct the errors below.') else: annotationForm = AnnotationImportForm() imageForm = ImageUploadForm() annotationOptionsForm = AnnotationImportOptionsForm() imageOptionsForm = ImageUploadOptionsForm(source=source) return render_to_response('images/image_and_annotation_upload.html', { 'source': source, 'annotationForm': annotationForm, 'imageForm': imageForm, 'annotationOptionsForm': annotationOptionsForm, 'imageOptionsForm': imageOptionsForm, 'uploadedImages': uploadedImages, }, context_instance=RequestContext(request) )
def test_upload_form_url(self): image_test_url = 'https://upload.wikimedia.org/wikipedia/commons/e/e0/JPEG_example_JPG_RIP_050.jpg' form_data = {'url_file':image_test_url} form = ImageUploadForm(form_data) self.assertTrue(form.is_valid())
def test_upload_form_wrong_format(self): with open("media/images/test/wrong.txt", "rb") as test_file: form = ImageUploadForm(files={'upload_file':File(test_file)}) self.assertFalse(form.is_valid())
def test_upload_form_empty(self): form = ImageUploadForm() self.assertFalse(form.is_valid())
def test_upload_form_file(self): with open("media/images/test/Slowpoke.jpeg", "rb") as image_test_file: form = ImageUploadForm(files={'upload_file':File(image_test_file)}) self.assertTrue(form.is_valid())