-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Home
agus makmun edited this page Jan 7, 2017
·
11 revisions
If you want to save the images uploaded to your storage, DracEditor also provide to handle it by customing uploader. and this script below is how to create it:
1. settings.py
# Global draceditor settings
# Input: string boolean, `true/false`
DRACEDITOR_ENABLE_CONFIGS = {
'imgur': 'true', # to enable/disable imgur uploader/custom uploader.
'mention': 'true', # to enable/disable mention
'jquery': 'true', # to include/revoke jquery (require for admin default django)
}
# Upload to locale storage
import time
DRACEDITOR_UPLOAD_PATH = 'images/uploads/{}'.format(time.strftime("%Y/%m/%d/"))
DRACEDITOR_UPLOAD_URL = '/api/uploader/' # change to local uploader
# Media Path
MEDIA_URL = '/media/'
MEDIA_ROOT = '/path/to/yourenv/yourproject/media'
2. views.py
import os
import json
import uuid
from django.conf import settings
from django.http import HttpResponse
from django.utils.translation import ugettext_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
@login_required
def markdown_uploader(request):
"""
Makdown image upload for locale storage
and represent as json to markdown editor.
"""
if request.method == 'POST' and request.is_ajax():
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.')
})
return HttpResponse(
data, content_type="application/json", status=405)
img_uuid = "{0}-{1}".format(uuid.uuid4().hex[:10], image.name)
tmp_file = os.path.join(settings.DRACEDITOR_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!'))
3. urls.py
from yourapp.views import markdown_uploader
urlpatterns = [
....
url(
r'^api/uploader/$',
markdown_uploader, name='markdown_uploader_page'
),
]