Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [FEAT] Add in TinyMCE Areas possibility for the user to upload a local file #277

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ CHANGELOG
8.5.6+dev (XXXX-XX-XX)
----------------------------

* Support django 4.2 and python 3.11
* Drop django 3.1 support
* Ease quickstart for developers
**Feature**

- Allow the user to upload file from TinyMCE editor to insert local images directly into the text area

**Maintenance**

- Support django 4.2 and python 3.11
- Drop django 3.1 support

**Documentation**

- Ease quickstart for developers


8.5.6 (2023-09-04)
Expand Down
8 changes: 7 additions & 1 deletion mapentity/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@
'img[longdesc|usemap|src|border|alt=|title|hspace|vspace|width|height|align],'
'p,em/i,strong/b,div[align],br,ul,li,ol,span[style],'
'iframe[src|frameborder=0|alt|title|width|height|align|name]'),
'setup': 'tinyMceInit'
'setup': 'tinyMceInit',
'image_title': True,
'image_caption': True,
'automatic_uploads': True,
'convert_urls': False,
'file_picker_types': 'image media',
'images_upload_url': '/tinymce/upload/',
}
TINYMCE_DEFAULT_CONFIG.update(getattr(settings, 'TINYMCE_DEFAULT_CONFIG', {}))
setattr(settings, 'TINYMCE_DEFAULT_CONFIG', TINYMCE_DEFAULT_CONFIG)
Expand Down
3 changes: 2 additions & 1 deletion mapentity/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from .registry import registry
from .settings import app_settings
from .views import (map_screenshot, history_delete,
from .views import (map_screenshot, history_delete, tinymce_upload,
ServeAttachment, JSSettings, Convert)

if app_settings['ACTION_HISTORY_ENABLED']:
Expand All @@ -23,6 +23,7 @@
path('convert/', Convert.as_view(), name='convert'),
path('history/delete/', history_delete, name='history_delete'),
path('api/auth/', include('rest_framework.urls')),
path('tinymce/upload/', tinymce_upload, name='tinymce_upload'),
# See default value in app_settings.JS_SETTINGS.
# Will be overriden, most probably.
path('api/settings.json', JSSettings.as_view(), name='js_settings'),
Expand Down
1 change: 1 addition & 0 deletions mapentity/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
JSSettings,
map_screenshot,
history_delete,
tinymce_upload,
)
from .generic import (
Convert,
Expand Down
17 changes: 16 additions & 1 deletion mapentity/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from datetime import datetime
from io import BytesIO
from urllib.parse import quote
from uuid import uuid4

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.gis.db.models import GeometryField
from django.core.exceptions import PermissionDenied
from django.http import (HttpResponse, HttpResponseBadRequest, Http404)
from django.http import (HttpResponse, HttpResponseBadRequest, Http404, JsonResponse)
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views import static, View
Expand Down Expand Up @@ -187,3 +188,17 @@
history = [h for h in history if h['path'] != path]
request.session['history'] = history
return HttpResponse()


@require_http_methods(["POST"])
@csrf_exempt
@login_required
def tinymce_upload(request):
file = request.FILES.get('file')
filename = f"tinymce/{uuid4()}/{str(file)}"
path = f"{settings.MEDIA_URL}{filename}"
location = f"{settings.MEDIA_ROOT}/{filename}"
os.makedirs(os.path.dirname(location), exist_ok=True)

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
with open(location, "wb+") as destination:

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.
destination.write(file.read())
return JsonResponse({"location": request.build_absolute_uri(path)})
Loading