Skip to content

Commit

Permalink
Support djangocms-picture 2.0.0 and higher in create_picture_plugin (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
leture authored Nov 28, 2023
1 parent 2e463bf commit 05f9b27
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 11 additions & 15 deletions djangocms_text_ckeditor/picture_save.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
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
pic.parent = parent_plugin
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
33 changes: 33 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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='<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==">',
)

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,
'<cms-plugin alt="Image - unnamed file " title="Image - unnamed file" id="{}"></cms-plugin>'.format(
picture_plugin.id,
),
)

0 comments on commit 05f9b27

Please sign in to comment.