enable image uploader
This commit is contained in:
parent
615514259d
commit
280601f3aa
|
@ -195,6 +195,10 @@ STATICFILES_FINDERS = (
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
|
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
|
||||||
MEDIA_ROOT = str(APPS_DIR('media'))
|
MEDIA_ROOT = str(APPS_DIR('media'))
|
||||||
|
import time
|
||||||
|
DRACEDITOR_UPLOAD_PATH = 'images/uploads/{}'.format(time.strftime("%Y/%m/%d/"))
|
||||||
|
DRACEDITOR_UPLOAD_URL = '/api/uploader/' # change to local uploader
|
||||||
|
MAX_IMAGE_UPLOAD_SIZE = 5242880 # 5MB
|
||||||
|
|
||||||
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
|
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
|
||||||
MEDIA_URL = '/media/'
|
MEDIA_URL = '/media/'
|
||||||
|
|
|
@ -15,6 +15,7 @@ from mhackspace.members.views import MemberListView
|
||||||
from mhackspace.subscriptions import views as subscription
|
from mhackspace.subscriptions import views as subscription
|
||||||
from mhackspace.base.feeds import LatestEntriesFeed
|
from mhackspace.base.feeds import LatestEntriesFeed
|
||||||
from mhackspace.blog.feeds import BlogFeed, BlogCategoryFeed
|
from mhackspace.blog.feeds import BlogFeed, BlogCategoryFeed
|
||||||
|
from mhackspace.base.views import markdown_uploader
|
||||||
from mhackspace.blog.views import blog, PostViewSet, CategoryViewSet
|
from mhackspace.blog.views import blog, PostViewSet, CategoryViewSet
|
||||||
from mhackspace.feeds.views import FeedViewSet, ArticleViewSet
|
from mhackspace.feeds.views import FeedViewSet, ArticleViewSet
|
||||||
|
|
||||||
|
@ -34,6 +35,10 @@ urlpatterns = [
|
||||||
url(r'^api/v1/', include(router.urls, namespace='v1')),
|
url(r'^api/v1/', include(router.urls, namespace='v1')),
|
||||||
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
||||||
url(r'^draceditor/', include('draceditor.urls')),
|
url(r'^draceditor/', include('draceditor.urls')),
|
||||||
|
url(
|
||||||
|
r'^api/uploader/$',
|
||||||
|
markdown_uploader, name='markdown_uploader_page'
|
||||||
|
),
|
||||||
url(r'^blog/$', blog, name='contact'),
|
url(r'^blog/$', blog, name='contact'),
|
||||||
url(r'^blog/rss/$', BlogFeed()),
|
url(r'^blog/rss/$', BlogFeed()),
|
||||||
url(r'^blog/(?P<slug>[0-9A-Za-z_\-]+)/$', blog, name='blog-item'),
|
url(r'^blog/(?P<slug>[0-9A-Za-z_\-]+)/$', blog, name='blog-item'),
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
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
|
||||||
|
|
||||||
|
from draceditor.utils import LazyEncoder
|
||||||
|
|
||||||
|
|
||||||
|
@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.')
|
||||||
|
}, 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) 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.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!'))
|
Loading…
Reference in New Issue