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 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) )