Пример #1
0
def up_image_async(request, model_name, object_pk):
    """Upload all images in request.FILES."""

    # Verify the model agaist our white-list
    if model_name not in ALLOWED_MODELS:
        message = _('Model not allowed.')
        return HttpResponseBadRequest(
            json.dumps({'status': 'error', 'message': message}))

    # Get the model
    m = get_model(*model_name.split('.'))

    # Then look up the object by pk
    try:
        obj = m.objects.get(pk=object_pk)
    except ObjectDoesNotExist:
        message = _('Object does not exist.')
        return HttpResponseNotFound(
            json.dumps({'status': 'error', 'message': message}))

    try:
        file_info = upload_imageattachment(request, obj)
    except FileTooLargeError as e:
        return HttpResponseBadRequest(
            json.dumps({'status': 'error', 'message': e.args[0]}))

    if isinstance(file_info, dict) and 'thumbnail_url' in file_info:
        return HttpResponse(
            json.dumps({'status': 'success', 'file': file_info}))

    message = _('Invalid or no image received.')
    return HttpResponseBadRequest(
        json.dumps({'status': 'error', 'message': message,
                    'errors': file_info}))
Пример #2
0
def up_image_async(request, model_name, object_pk):
    """Upload all images in request.FILES."""

    # Verify the model agaist our white-list
    if model_name not in ALLOWED_MODELS:
        message = _("Model not allowed.")
        return HttpResponseBadRequest(
            json.dumps({
                "status": "error",
                "message": message
            }))

    # Get the model
    m = apps.get_model(*model_name.split("."))

    # Then look up the object by pk
    try:
        obj = m.objects.get(pk=object_pk)
    except ObjectDoesNotExist:
        message = _("Object does not exist.")
        return HttpResponseNotFound(
            json.dumps({
                "status": "error",
                "message": message
            }))

    try:
        file_info = upload_imageattachment(request, obj)
    except FileTooLargeError as e:
        return HttpResponseBadRequest(
            json.dumps({
                "status": "error",
                "message": e.args[0]
            }))

    if hasattr(obj, "clear_cached_images"):
        # if the object the image is attached to has a `clear_cached_images` method,
        # like questions and answers do, call it
        obj.clear_cached_images()

    if isinstance(file_info, dict) and "thumbnail_url" in file_info:
        return HttpResponse(
            json.dumps({
                "status": "success",
                "file": file_info
            }))

    message = _("Invalid or no image received.")
    return HttpResponseBadRequest(
        json.dumps({
            "status": "error",
            "message": message,
            "errors": file_info
        }))
Пример #3
0
def up_image_async(request, model_name, object_pk):
    """Upload all images in request.FILES."""

    # Verify the model agaist our white-list
    if model_name not in ALLOWED_MODELS:
        message = _('Model not allowed.')
        return HttpResponseBadRequest(
            json.dumps({
                'status': 'error',
                'message': message
            }))

    # Get the model
    m = get_model(*model_name.split('.'))

    # Then look up the object by pk
    try:
        obj = m.objects.get(pk=object_pk)
    except ObjectDoesNotExist:
        message = _('Object does not exist.')
        return HttpResponseNotFound(
            json.dumps({
                'status': 'error',
                'message': message
            }))

    try:
        file_info = upload_imageattachment(request, obj)
    except FileTooLargeError as e:
        return HttpResponseBadRequest(
            json.dumps({
                'status': 'error',
                'message': e.args[0]
            }))

    if isinstance(file_info, dict) and 'thumbnail_url' in file_info:
        return HttpResponse(
            json.dumps({
                'status': 'success',
                'file': file_info
            }))

    message = _('Invalid or no image received.')
    return HttpResponseBadRequest(
        json.dumps({
            'status': 'error',
            'message': message,
            'errors': file_info
        }))