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

WIP Adding logic fallback for image/picture/jumbotron #351

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions cmsplugin_cascade/bootstrap4/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ def get_image_tags(instance):
"""
if hasattr(instance, 'image') and hasattr(instance.image, 'exif'):
aspect_ratio = compute_aspect_ratio(instance.image)
elif 'image' in instance.glossary and 'width' in instance.glossary['image']:
elif 'image' in instance.glossary and 'width' in instance.glossary['image']:
aspect_ratio = compute_aspect_ratio_with_glossary(instance.glossary)
# fallback logic
elif 'image_properties' in instance.glossary and 'width' in instance.glossary['image_properties']:
aspect_ratio = compute_aspect_ratio_with_glossary(instance.glossary)
else:
# if accessing the image file fails or fake image fails, abort here
Expand All @@ -182,6 +185,7 @@ def get_image_tags(instance):
upscale = 'upscale' in resize_options
if 'subject_location' in resize_options and hasattr(instance.image, 'subject_location'):
subject_location = instance.image.subject_location
# fallback logic
else:
subject_location = None
tags = {'sizes': [], 'srcsets': {}, 'is_responsive': is_responsive, 'extra_styles': {}}
Expand All @@ -193,7 +197,12 @@ def get_image_tags(instance):
else:
image_width = parse_responsive_length(instance.glossary['image_width_fixed'])
if not image_width[0]:
image_width = (instance.image.width, image_width[1])
if hasattr(instance,'image' ) and hasattr(instance.image,'width' ) :
image_width = (instance.image.width, image_width[1])
# logic fallback
else:
image_width = (instance.glossary['image_properties']['width'],image_width[1] )

try:
image_height = parse_responsive_length(instance.glossary['image_height'])
except KeyError:
Expand Down
3 changes: 3 additions & 0 deletions cmsplugin_cascade/bootstrap4/picture.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,11 @@ def get_picture_elements(instance):

if hasattr(instance, 'image') and hasattr(instance.image, 'exif'):
aspect_ratio = compute_aspect_ratio(instance.image)
# fallback logic picture
elif 'image' in instance.glossary and 'width' in instance.glossary['image']:
aspect_ratio = compute_aspect_ratio_with_glossary(instance.glossary)
elif 'image_properties' in instance.glossary and 'width' in instance.glossary['image_properties']:
aspect_ratio = compute_aspect_ratio_with_glossary(instance.glossary)
else:
# if accessing the image file fails or fake image fails, abort here
logger.warning("Unable to compute aspect ratio of image '{}'".format(instance.image))
Expand Down
6 changes: 3 additions & 3 deletions cmsplugin_cascade/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ class ImageFormMixin(EntangledModelFormMixin):
help_text=_("Textual description of the image added to the 'alt' tag of the <img> element."),
)

_image_properties = EntangledField()
image_properties = EntangledField()

class Meta:
entangled_fields = {'glossary': ['image_file', 'image_title', 'alt_tag', '_image_properties']}
entangled_fields = {'glossary': ['image_file', 'image_title', 'alt_tag', 'image_properties']}

def clean(self):
cleaned_data = super().clean()
image_file = cleaned_data.get('image_file')
if not image_file:
raise ValidationError(_("No image has been selected."))
# _image_properties are just a cached representation, maybe useless
cleaned_data['_image_properties'] = {
cleaned_data['image_properties'] = {
'width': image_file._width,
'height': image_file._height,
'exif_orientation': image_file.exif.get('Orientation', 1),
Expand Down
2 changes: 1 addition & 1 deletion cmsplugin_cascade/migrations/0027_version_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def migrate_image(glossary):
})
if 'width' in image and 'height' in image and 'exif_orientation' in image:
glossary.update({
'_image_properties': {'width': image['width'], 'height': image['height'],
'image_properties': {'width': image['width'], 'height': image['height'],
'exif_orientation': image['exif_orientation']},
})
return True
Expand Down
195 changes: 195 additions & 0 deletions cmsplugin_cascade/static/cascade/fallback_light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading