# Path: trumbowyg/settings.py
# UPLOAD_PATH = getattr(settings, 'TRUMBOWYG_UPLOAD_PATH', 'uploads/')
# THUMBNAIL_SIZE = getattr(settings, 'TRUMBOWYG_THUMBNAIL_SIZE', None)
# TRANSLITERATE_FILENAME = getattr(settings, 'TRUMBOWYG_TRANSLITERATE_FILENAME', False)
# SEMANTIC = getattr(settings, 'TRUMBOWYG_SEMANTIC', 'false')
# 
# Path: trumbowyg/forms.py
# class ImageForm(forms.Form):
#     image = forms.ImageField()
# 
# Path: trumbowyg/utils.py
# def slugify(value):
#     if _settings.TRANSLITERATE_FILENAME:
#         try:
#             from unidecode import unidecode
#             value = unidecode(six.text_type(value))
#         except ImportError as e:
#             logger.exception(e)
# 
#     return django_slugify(six.text_type(value))


import os
import json
from django.conf import settings
from django.core.files.storage import default_storage
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from trumbowyg import settings as _settings
from trumbowyg.forms import ImageForm
from trumbowyg.utils import slugify
        from PIL import Image


@csrf_exempt
@require_POST
def upload_image(request):
    if not request.is_ajax():
        return HttpResponse(status=400)
    if not (request.user.is_active and request.user.is_staff):
        return HttpResponse(status=403)

    image_form = ImageForm(request.POST, request.FILES)
    if image_form.is_valid():
        image = image_form.cleaned_data['image']
        url = save_image(image)
        context = {'message': 'uploadSuccess', 'file': url}
    else:
        context = {'message': image_form.errors['image'][0]}

    return HttpResponse(json.dumps(context), content_type='application/json')


def save_image(image):
    """Receives image, saves it and returns its url"""

    filename = image.name

    root, ext = os.path.splitext(filename)
    filename = '{}{}'.format(slugify(root), ext)
    path = os.path.join(_settings.UPLOAD_PATH, filename)


# Path: trumbowyg/settings.py
# UPLOAD_PATH = getattr(settings, 'TRUMBOWYG_UPLOAD_PATH', 'uploads/')
# THUMBNAIL_SIZE = getattr(settings, 'TRUMBOWYG_THUMBNAIL_SIZE', None)
# TRANSLITERATE_FILENAME = getattr(settings, 'TRUMBOWYG_TRANSLITERATE_FILENAME', False)
# SEMANTIC = getattr(settings, 'TRUMBOWYG_SEMANTIC', 'false')
# 
# Path: trumbowyg/forms.py
# class ImageForm(forms.Form):
#     image = forms.ImageField()
# 
# Path: trumbowyg/utils.py
# def slugify(value):
#     if _settings.TRANSLITERATE_FILENAME:
#         try:
#             from unidecode import unidecode
#             value = unidecode(six.text_type(value))
#         except ImportError as e:
#             logger.exception(e)
# 
#     return django_slugify(six.text_type(value))


# Path: trumbowyg/views.py
import os
import json
from django.conf import settings
from django.core.files.storage import default_storage
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from trumbowyg import settings as _settings
from trumbowyg.forms import ImageForm
from trumbowyg.utils import slugify
        from PIL import Image



@csrf_exempt
@require_POST
def upload_image(request):
    if not request.is_ajax():
        return HttpResponse(status=400)
    if not (request.user.is_active and request.user.is_staff):
        return HttpResponse(status=403)

    image_form = ImageForm(request.POST, request.FILES)
    if image_form.is_valid():
        image = image_form.cleaned_data['image']
        url = save_image(image)
        context = {'message': 'uploadSuccess', 'file': url}
    else:
        context = {'message': image_form.errors['image'][0]}

    return HttpResponse(json.dumps(context), content_type='application/json')


def save_image(image):
    """Receives image, saves it and returns its url"""

    filename = image.name

    root, ext = os.path.splitext(filename)
    filename = '{}{}'.format(slugify(root), ext)
    path = os.path.join(_settings.UPLOAD_PATH, filename)


    if _settings.THUMBNAIL_SIZE: