mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 21:27:49 +02:00
Use a markdown editor
This commit is contained in:
@@ -1,4 +1,72 @@
|
||||
import os
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.files.storage import default_storage
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
from martor.utils import LazyEncoder
|
||||
|
||||
|
||||
@login_required
|
||||
def markdown_uploader(request):
|
||||
"""
|
||||
Makdown image upload for locale storage
|
||||
and represent as json to markdown editor.
|
||||
"""
|
||||
if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":
|
||||
if "markdown-image-upload" in request.FILES:
|
||||
image = request.FILES["markdown-image-upload"]
|
||||
image_types = [
|
||||
"image/png",
|
||||
"image/jpg",
|
||||
"image/jpeg",
|
||||
"image/pjpeg",
|
||||
"image/gif",
|
||||
]
|
||||
if image.content_type not in image_types:
|
||||
data = json.dumps(
|
||||
{"status": 405, "error": _("Bad image format.")},
|
||||
cls=LazyEncoder,
|
||||
)
|
||||
return HttpResponse(
|
||||
data, content_type="application/json", status=405
|
||||
)
|
||||
|
||||
if image.size > settings.MAX_IMAGE_UPLOAD_SIZE:
|
||||
to_MB = settings.MAX_IMAGE_UPLOAD_SIZE / (1024 * 1024)
|
||||
data = json.dumps(
|
||||
{
|
||||
"status": 405,
|
||||
"error": _("Maximum image file is %(size)s MB.")
|
||||
% {"size": to_MB},
|
||||
},
|
||||
cls=LazyEncoder,
|
||||
)
|
||||
return HttpResponse(
|
||||
data, content_type="application/json", status=405
|
||||
)
|
||||
|
||||
img_uuid = "{0}-{1}".format(
|
||||
uuid.uuid4().hex[:10], image.name.replace(" ", "-")
|
||||
)
|
||||
tmp_file = os.path.join(settings.MARTOR_UPLOAD_PATH, img_uuid)
|
||||
def_path = default_storage.save(
|
||||
tmp_file, ContentFile(image.read())
|
||||
)
|
||||
img_url = os.path.join(settings.MEDIA_URL, def_path)
|
||||
|
||||
data = json.dumps(
|
||||
{"status": 200, "link": img_url, "name": image.name}
|
||||
)
|
||||
return HttpResponse(data, content_type="application/json")
|
||||
return HttpResponse(_("Invalid request!"))
|
||||
return HttpResponse(_("Invalid request!"))
|
||||
|
||||
|
||||
def get_site_conf():
|
||||
|
Reference in New Issue
Block a user