-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4024 from GeotrekCE/feat_add_tinymce_upload_image
Add local image upload into flat pages content (ref #3552)
- Loading branch information
Showing
6 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters