Skip to content

Commit

Permalink
Merge pull request #98 from timmyomahony/develop
Browse files Browse the repository at this point in the history
Image upload PR and bugfix PRs
  • Loading branch information
timmyomahony authored Dec 9, 2020
2 parents d516ebe + 2d89f3e commit ea21272
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ The following options are available via your settings to tweak how the image upl

- `PAGEDOWN_IMAGE_UPLOAD_PATH` can be used to change the path within your media root (default is `pagedown-uploads`)
- `PAGEDOWN_IMAGE_UPLOAD_EXTENSIONS` can be used to limit the extensions allowed for upload (default is `jpg`, `jpeg`, `png`, `webp`)
- `PAGEDOWN_IMAGE_UPLOAD_UNIQUE` can be used to ensure all uploads are stored in a uniquely named subfolder, e.g. `f748e009-c3cb-40f3-abf2-d103ab0ad259/my-file.png` (default is `False`)

Check out the `pagedown_init.js` script to [see how the upload is being performed on the client side](https://github.com/timmyomahony/django-pagedown/blob/develop/pagedown/static/pagedown_init.js).

Expand Down
2 changes: 1 addition & 1 deletion pagedown/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = ('2', '1', '3')
VERSION = ('2', '2', '0')

default_app_config = 'pagedown.apps.PagedownConfig'
5 changes: 4 additions & 1 deletion pagedown/static/pagedown_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ DjangoPagedown = (function() {

var createEditor = function(element) {
var input = element.getElementsByClassName("wmd-input")[0];
if (input === undefined) {
return
}
var id = input.id.substr(9);
if (!editors.hasOwnProperty(id)) {
var editor = new Markdown.Editor(converter, id, {});
Expand Down Expand Up @@ -54,7 +57,7 @@ DjangoPagedown = (function() {
var data = new FormData();
var xhr = new XMLHttpRequest();
data.append("image", file.files[0]);
xhr.open("POST", "/pagedown/image-upload/", true);
xhr.open("POST", file.dataset.action, true);
xhr.addEventListener(
"load",
function() {
Expand Down
8 changes: 7 additions & 1 deletion pagedown/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from django.conf import settings
Expand All @@ -11,6 +12,8 @@

IMAGE_UPLOAD_PATH = getattr(
settings, 'PAGEDOWN_IMAGE_UPLOAD_PATH', 'pagedown-uploads')
IMAGE_UPLOAD_UNIQUE = getattr(
settings, 'PAGEDOWN_IMAGE_UPLOAD_UNIQUE', False)
IMAGE_UPLOAD_ENABLED = getattr(
settings, 'PAGEDOWN_IMAGE_UPLOAD_ENABLED', False)

Expand All @@ -27,7 +30,10 @@ def image_upload_view(request):
form = ImageUploadForm(request.POST, request.FILES)
if form.is_valid():
image = request.FILES['image']
path = os.path.join(IMAGE_UPLOAD_PATH, image.name)
path_args = [IMAGE_UPLOAD_PATH, image.name]
if IMAGE_UPLOAD_UNIQUE:
path_args.insert(1, str(uuid.uuid4()))
path = os.path.join(*path_args)
path = default_storage.save(path, image)
url = default_storage.url(path)
return JsonResponse({'success': True, 'url': url})
Expand Down

0 comments on commit ea21272

Please sign in to comment.