Skip to content

Commit

Permalink
Support djangocms-picture 2.0.0 and higher in create_picture_plugin h…
Browse files Browse the repository at this point in the history
…elper. djangocms-picture 2.0.0 switched to a filer reference to store the image so the helper must create a filer image instance
  • Loading branch information
leture committed Nov 27, 2023
1 parent 2e463bf commit 6ea4519
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions djangocms_text_ckeditor/picture_save.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

Check failure on line 1 in djangocms_text_ckeditor/picture_save.py

View workflow job for this annotation

GitHub Actions / flake8

'os' imported but unused

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'os' is not used.

from django.conf import settings

Check failure on line 3 in djangocms_text_ckeditor/picture_save.py

View workflow job for this annotation

GitHub Actions / flake8

'django.conf.settings' imported but unused
from django.core.files.base import ContentFile

Check warning on line 4 in djangocms_text_ckeditor/picture_save.py

View check run for this annotation

Codecov / codecov/patch

djangocms_text_ckeditor/picture_save.py#L4

Added line #L4 was not covered by tests

from cms.models.pluginmodel import CMSPlugin

Expand All @@ -17,13 +18,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

Check warning on line 28 in djangocms_text_ckeditor/picture_save.py

View check run for this annotation

Codecov / codecov/patch

djangocms_text_ckeditor/picture_save.py#L21-L28

Added lines #L21 - L28 were not covered by tests

pic.save()
return pic

0 comments on commit 6ea4519

Please sign in to comment.