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

Add local image upload into flat pages content (ref #3552) #4024

Merged
merged 2 commits into from
Apr 2, 2024
Merged
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
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ CHANGELOG
2.103.2+dev (XXXX-XX-XX)
------------------------

**WARNING!**

This version breaks the flat pages API. Upgrade first to Geotrek-rando 3.19.0 (simultaneously released on April 2024) for a smooth transition.

**Features**

- Add new menu headers and flat pages with tree hierarchies
- Add styling and embedding tools for FlatPages content (refs #3921, #3922, #4019)
- Add local image upload into content of flat pages (ref #3552)

**Bug fixes**

Expand Down
26 changes: 26 additions & 0 deletions geotrek/flatpages/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from tempfile import TemporaryDirectory

from django.test import TestCase
from django.urls import reverse
from geotrek.common.utils.testdata import get_dummy_uploaded_image
from mapentity.tests import SuperUserFactory


class TinyMCEUploadViewTestCase(TestCase):

def test_tinymce_upload(self):
user = SuperUserFactory()
self.client.force_login(user)
file = get_dummy_uploaded_image("tinymce_uploaded_image.png")

with TemporaryDirectory() as tmp_dir:
with self.settings(MEDIA_ROOT=tmp_dir):
url = reverse("flatpages:tinymce_upload")
response = self.client.post(url, data={"file": file})

self.assertEqual(response.status_code, 200)
data = response.json()
self.assertIn("location", data)
location = data["location"]
self.assertTrue(location.startswith("http://") or location.startswith("https://"))
self.assertIn("tinymce_uploaded_image", location)
8 changes: 8 additions & 0 deletions geotrek/flatpages/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from geotrek.flatpages.views import tinymce_upload

app_name = 'flatpages'

urlpatterns = [
path('tinymce/upload/', tinymce_upload, name="tinymce_upload"),
]
17 changes: 17 additions & 0 deletions geotrek/flatpages/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from uuid import uuid4

from django.contrib.auth.decorators import login_required
from django.core.files.storage import default_storage
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods


@require_http_methods(["POST"])
@csrf_exempt
@login_required
def tinymce_upload(request):
file = request.FILES.get('file')
filename = f"flatpages/content/upload/{uuid4()}/{str(file)}"
default_storage.save(filename, file)
return JsonResponse({"location": request.build_absolute_uri(default_storage.url(filename))})
6 changes: 3 additions & 3 deletions geotrek/flatpages/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"menubar": False,
'image_title': False,
'image_caption': True,
'automatic_uploads': False,
'automatic_uploads': True,
'convert_urls': False,
'file_picker_types': None,
'images_upload_url': None,
'file_picker_types': "image media",
'images_upload_url': "/flatpages/tinymce/upload/",
"toolbar": 'undo redo | styleselect | blockquote | bold italic forecolor |'
'alignleft aligncenter alignright alignjustify | bullist numlist | link image media |'
'button-link suggestions | removeformat visualblocks code | wordcount | help',
Expand Down
2 changes: 2 additions & 0 deletions geotrek/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
urlpatterns.append(path('', include('geotrek.feedback.urls')))
if 'geotrek.sensitivity' in settings.INSTALLED_APPS:
urlpatterns.append(path('', include('geotrek.sensitivity.urls')))
if 'geotrek.flatpages' in settings.INSTALLED_APPS:
urlpatterns.append(path('flatpages/', include('geotrek.flatpages.urls')))
if 'geotrek.api' in settings.INSTALLED_APPS:
urlpatterns.append(path('', include('geotrek.api.v2.urls')))
if 'geotrek.flatpages' in settings.INSTALLED_APPS and 'geotrek.trekking' in settings.INSTALLED_APPS and 'geotrek.tourism' in settings.INSTALLED_APPS:
Expand Down
Loading