def handle_uploaded_image(request, display_obj, model_name, image_type, current_image=None): ''' process the uploaded file and generate ImageRevision ''' try: form = UploadImageForm(request.POST, request.FILES) except IOError: # sometimes uploads misbehave. connection dropped ? error_text = 'Something went wrong with the upload. ' + \ 'Please <a href="' + request.path + '">try again</a>.' return render_error(request, error_text, redirect=False, is_safe=True) if not form.is_valid(): return _display_image_upload_form(request, form, display_obj, model_name, image_type) # process form image = form.cleaned_data['image'] if current_image: from apps.oi.views import _is_reservable is_reservable = _is_reservable('image', current_image.id) if is_reservable == 0: return render_error(request, u'Cannot replace %s as it is already reserved.' % \ current_image.description()) # create OI records changeset = Changeset(indexer=request.user, state=states.OPEN, change_type=CTYPES['image']) changeset.save() revision = ImageRevision( changeset=changeset, content_type=ContentType.objects.get_for_model(display_obj), object_id=display_obj.id, type=ImageType.objects.get(name=image_type), marked=form.cleaned_data['marked']) if current_image: revision.image = current_image revision.is_replacement = True revision.save() revision.image_file.save(str(revision.id) + '.jpg', content=File(image)) revision.changeset.submit(form.cleaned_data['comments']) return HttpResponseRedirect(urlresolvers.reverse('editing'))
def handle_uploaded_image(request, display_obj, model_name, image_type, current_image=None): ''' process the uploaded file and generate ImageRevision ''' try: form = UploadImageForm(request.POST, request.FILES) except IOError: # sometimes uploads misbehave. connection dropped ? error_text = 'Something went wrong with the upload. ' + \ 'Please <a href="' + request.path + '">try again</a>.' return render_error(request, error_text, redirect=False, is_safe=True) if not form.is_valid(): return _display_image_upload_form(request, form, display_obj, model_name, image_type) # process form image = form.cleaned_data['image'] if current_image: from apps.oi.views import _is_reservable is_reservable = _is_reservable('image', current_image.id) if is_reservable == 0: return render_error(request, u'Cannot replace %s as it is already reserved.' % \ current_image.description()) # create OI records changeset = Changeset(indexer=request.user, state=states.OPEN, change_type=CTYPES['image']) changeset.save() revision = ImageRevision(changeset=changeset, content_type=ContentType.objects.get_for_model(display_obj), object_id=display_obj.id, type=ImageType.objects.get(name=image_type), marked=form.cleaned_data['marked']) if current_image: revision.image = current_image revision.is_replacement = True revision.save() revision.image_file.save(str(revision.id) + '.jpg', content=File(image)) revision.changeset.submit(form.cleaned_data['comments']) return HttpResponseRedirect(urlresolvers.reverse('editing'))