Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 05f9b27

Browse files
authored
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
1 parent 2e463bf commit 05f9b27

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525
ubuntu-20.04,
2626
]
2727
exclude:
28+
- python-version: 3.7
29+
requirements-file: dj32_cms41.txt
2830
- python-version: 3.7
2931
requirements-file: dj40_cms311.txt
3032
- python-version: 3.7
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
import os
2-
3-
from django.conf import settings
1+
from django.core.files.base import ContentFile
42

53
from cms.models.pluginmodel import CMSPlugin
64

75

86
def create_picture_plugin(filename, file, parent_plugin, **kwargs):
9-
try:
10-
from djangocms_picture.models import Picture
11-
except ImportError:
12-
from cms.plugins.picture.models import Picture
7+
from djangocms_picture.models import Picture
138

149
pic = Picture()
1510
pic.placeholder = parent_plugin.placeholder
1611
pic.parent = parent_plugin
1712
pic.position = CMSPlugin.objects.filter(parent=parent_plugin).count()
1813
pic.language = parent_plugin.language
1914
pic.plugin_type = 'PicturePlugin'
20-
path = pic.get_media_path(filename)
21-
full_path = os.path.join(settings.MEDIA_ROOT, path)
22-
if not os.path.exists(os.path.dirname(full_path)):
23-
os.makedirs(os.path.dirname(full_path))
24-
pic.image = path
25-
f = open(full_path, 'wb')
26-
f.write(file.read())
27-
f.close()
15+
16+
# Set the FilerImageField value.
17+
from filer.settings import FILER_IMAGE_MODEL
18+
from filer.utils.loader import load_model
19+
image_class = load_model(FILER_IMAGE_MODEL)
20+
image_obj = image_class(file=ContentFile(file.read(), name=filename))
21+
image_obj.save()
22+
pic.picture = image_obj
23+
2824
pic.save()
2925
return pic

tests/test_plugin.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unittest
55
from urllib.parse import unquote
66

7+
from django.conf import settings
78
from django.contrib import admin
89
from django.contrib.auth import get_permission_codename
910
from django.contrib.auth.models import Permission
@@ -40,6 +41,9 @@
4041
HAS_DJANGOCMS_TRANSLATIONS = False
4142

4243

44+
HAS_DJANGOCMS_PICTURE = "djangocms_picture" in settings.INSTALLED_APPS
45+
46+
4347
class PluginActionsTestCase(TestFixture, BaseTestCase):
4448

4549
def get_custom_admin_url(self, plugin_class, name):
@@ -1079,3 +1083,32 @@ def test_textfield_with_untranslatable_children(self):
10791083

10801084
result = TextPlugin.set_translation_import_content(result, plugin)
10811085
self.assertDictEqual(result, {child1.pk: ''})
1086+
1087+
1088+
@unittest.skipUnless(
1089+
HAS_DJANGOCMS_PICTURE,
1090+
'Optional dependency djangocms-picture for tests is not installed.',
1091+
)
1092+
class DjangoCMSPictureIntegrationTestCase(TestFixture, BaseTestCase):
1093+
def setUp(self):
1094+
super().setUp()
1095+
self.page = self.create_page('test page', template='page.html', language='en')
1096+
self.placeholder = self.get_placeholders(self.page, 'en').get(slot='content')
1097+
1098+
def test_extract_images(self):
1099+
text_plugin = add_plugin(
1100+
self.placeholder,
1101+
'TextPlugin',
1102+
'en',
1103+
body='<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg==">',
1104+
)
1105+
1106+
from djangocms_picture.models import Picture
1107+
picture_plugin = Picture.objects.order_by('-id')[0]
1108+
self.assertEqual(picture_plugin.parent.id, text_plugin.id)
1109+
self.assertHTMLEqual(
1110+
text_plugin.body,
1111+
'<cms-plugin alt="Image - unnamed file " title="Image - unnamed file" id="{}"></cms-plugin>'.format(
1112+
picture_plugin.id,
1113+
),
1114+
)

0 commit comments

Comments
 (0)