From 05f9b276df29937e17df74c033aa6aafb78a8b36 Mon Sep 17 00:00:00 2001 From: Steffen Jasper Date: Tue, 28 Nov 2023 15:53:29 +0100 Subject: [PATCH] Support djangocms-picture 2.0.0 and higher in create_picture_plugin (#660) * Support djangocms-picture 2.0.0 and higher in create_picture_plugin helper. djangocms-picture 2.0.0 switched to a filer reference to store the image so the helper must create a filer image instance * Removed no longer used imports * Added a test for helper extract_images which subsequently also tests the helper create_picture_plugin * Use assertHTMLEqual to be stable against different order of tag attributes * Exclude Django 3.2 / django-cms 4.1 under Python 3.7 from tests execution because django-cms 4.1 is needs Python>=3.8 * The current minium version for django-cms of 3.6 allows to remove the conditional import of the picture plugin * Look in INSTALLED_APPS instead of import to check if djangocms_picture is active --- .github/workflows/test.yml | 2 ++ djangocms_text_ckeditor/picture_save.py | 26 +++++++++---------- tests/test_plugin.py | 33 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c66c2a350..bb93c288d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,6 +25,8 @@ jobs: ubuntu-20.04, ] exclude: + - python-version: 3.7 + requirements-file: dj32_cms41.txt - python-version: 3.7 requirements-file: dj40_cms311.txt - python-version: 3.7 diff --git a/djangocms_text_ckeditor/picture_save.py b/djangocms_text_ckeditor/picture_save.py index f085d73cc..51ea97803 100644 --- a/djangocms_text_ckeditor/picture_save.py +++ b/djangocms_text_ckeditor/picture_save.py @@ -1,15 +1,10 @@ -import os - -from django.conf import settings +from django.core.files.base import ContentFile from cms.models.pluginmodel import CMSPlugin def create_picture_plugin(filename, file, parent_plugin, **kwargs): - try: - from djangocms_picture.models import Picture - except ImportError: - from cms.plugins.picture.models import Picture + from djangocms_picture.models import Picture pic = Picture() pic.placeholder = parent_plugin.placeholder @@ -17,13 +12,14 @@ def create_picture_plugin(filename, file, parent_plugin, **kwargs): pic.position = CMSPlugin.objects.filter(parent=parent_plugin).count() pic.language = parent_plugin.language pic.plugin_type = 'PicturePlugin' - path = pic.get_media_path(filename) - full_path = os.path.join(settings.MEDIA_ROOT, path) - if not os.path.exists(os.path.dirname(full_path)): - os.makedirs(os.path.dirname(full_path)) - pic.image = path - f = open(full_path, 'wb') - f.write(file.read()) - f.close() + + # Set the FilerImageField value. + from filer.settings import FILER_IMAGE_MODEL + from filer.utils.loader import load_model + image_class = load_model(FILER_IMAGE_MODEL) + image_obj = image_class(file=ContentFile(file.read(), name=filename)) + image_obj.save() + pic.picture = image_obj + pic.save() return pic diff --git a/tests/test_plugin.py b/tests/test_plugin.py index cc89d3583..45efb50b7 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -4,6 +4,7 @@ import unittest from urllib.parse import unquote +from django.conf import settings from django.contrib import admin from django.contrib.auth import get_permission_codename from django.contrib.auth.models import Permission @@ -40,6 +41,9 @@ HAS_DJANGOCMS_TRANSLATIONS = False +HAS_DJANGOCMS_PICTURE = "djangocms_picture" in settings.INSTALLED_APPS + + class PluginActionsTestCase(TestFixture, BaseTestCase): def get_custom_admin_url(self, plugin_class, name): @@ -1079,3 +1083,32 @@ def test_textfield_with_untranslatable_children(self): result = TextPlugin.set_translation_import_content(result, plugin) self.assertDictEqual(result, {child1.pk: ''}) + + +@unittest.skipUnless( + HAS_DJANGOCMS_PICTURE, + 'Optional dependency djangocms-picture for tests is not installed.', +) +class DjangoCMSPictureIntegrationTestCase(TestFixture, BaseTestCase): + def setUp(self): + super().setUp() + self.page = self.create_page('test page', template='page.html', language='en') + self.placeholder = self.get_placeholders(self.page, 'en').get(slot='content') + + def test_extract_images(self): + text_plugin = add_plugin( + self.placeholder, + 'TextPlugin', + 'en', + body='', + ) + + from djangocms_picture.models import Picture + picture_plugin = Picture.objects.order_by('-id')[0] + self.assertEqual(picture_plugin.parent.id, text_plugin.id) + self.assertHTMLEqual( + text_plugin.body, + ''.format( + picture_plugin.id, + ), + )