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

Support djangocms-picture 2.0.0 and higher in create_picture_plugin #660

Merged
merged 7 commits into from
Nov 28, 2023
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,
),
)
Loading