コード例 #1
0
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:
        revision_lock = _get_revision_lock(current_image)
        if not revision_lock:
            return render_error(
                request, '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()
    if current_image:
        revision_lock.changeset = changeset
        revision_lock.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.previous_revision = current_image.revisions.get(
            next_revision=None,
            changeset__state=states.APPROVED,
            committed=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'))
コード例 #2
0
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:
        revision_lock = _get_revision_lock(current_image)
        if not revision_lock:
            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()
    if current_image:
        revision_lock.changeset = changeset
        revision_lock.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.previous_revision = current_image.revisions.get(
                                     next_revision=None,
                                     changeset__state=states.APPROVED,
                                     committed=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'))
コード例 #3
0
def upload_image(request, model_name, id, image_type, image=None):
    from apps.oi.views import DISPLAY_CLASSES, REVISION_CLASSES
    """
    Handles uploading of supporting images
    """

    display_obj = get_object_or_404(DISPLAY_CLASSES[model_name], id=id,
                                    deleted=False)

    kwargs = None
    # replacement
    if image:
        if image.object != display_obj:
            return render_error(request,
              'Image and object id do not match.',
              redirect=False, is_safe=True)
        kwargs = {'upload_type': 'replacement', 'current_image': image,
          'current_image_tag': get_generic_image_tag(image, 'current image')}
    # check if there is an image if image_type.unique is set
    else:
        img_type = get_object_or_404(ImageType, name=image_type)
        if img_type.unique:
            if Image.objects.filter(content_type=\
              ContentType.objects.get_for_model(display_obj),
              object_id=display_obj.id, type=img_type, deleted=False).count():
                return render_error(request,
                  ('%s has an image. Further images cannot be added, '
                  'only replaced.') % (esc(display_obj)),
                  redirect=False, is_safe=True)

    # check if there is a pending object deletion
    filter_dict = {model_name : display_obj, 'deleted' : True,
                   'changeset__state__in' : states.ACTIVE}
    revision = REVISION_CLASSES[model_name].objects.filter(**filter_dict)
    if revision:
        revision = revision.get()
        return render_error(request,
          ('%s is <a href="%s">pending deletion</a>. Images '
          'cannot be added or modified.') % (esc(display_obj),
          urlresolvers.reverse('compare', kwargs={'id': revision.changeset.id})),
          redirect=False, is_safe=True)

    # current request is an upload
    if request.method == 'POST':
        if image:
            return handle_uploaded_image(request, display_obj, model_name,
                                         image_type, current_image=image)
        else:
            return handle_uploaded_image(request, display_obj, model_name,
                                         image_type)
    # request is a GET for the form
    else:
        form = UploadImageForm()
        # display the form
        return _display_image_upload_form(request, form, display_obj,
                                          model_name, image_type, kwargs=kwargs)