-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
46 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters