Skip to content

Commit

Permalink
Add settings for meta image thumbnail (#743)
Browse files Browse the repository at this point in the history
* Improve meta image size handling

* Make BLOG_META_IMAGE_SIZE setting None by default and add test for it


---------

Co-authored-by: Adrien Delhorme <ad@kapt.mobi>
Co-authored-by: Iacopo Spalletti <i.spalletti@nephila.it>
  • Loading branch information
3 people committed Jul 21, 2023
1 parent 028f01e commit 1a8d6c9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/718.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve meta image size handling
11 changes: 11 additions & 0 deletions djangocms_blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.utils.html import strip_tags
from django.utils.translation import get_language, gettext, gettext_lazy as _
from djangocms_text_ckeditor.fields import HTMLField
from easy_thumbnails.files import get_thumbnailer
from filer.fields.image import FilerImageField
from filer.models import ThumbnailOption
from meta.models import ModelMeta
Expand Down Expand Up @@ -401,15 +402,25 @@ def get_description(self):

def get_image_full_url(self):
if self.main_image:
thumbnail_options = get_setting("META_IMAGE_SIZE")
if thumbnail_options:
thumbnail_url = get_thumbnailer(self.main_image).get_thumbnail(thumbnail_options).url
return self.build_absolute_uri(thumbnail_url)
return self.build_absolute_uri(self.main_image.url)
return ""

def get_image_width(self):
if self.main_image:
thumbnail_options = get_setting("META_IMAGE_SIZE")
if thumbnail_options:
return get_thumbnailer(self.main_image).get_thumbnail(thumbnail_options).width
return self.main_image.width

def get_image_height(self):
if self.main_image:
thumbnail_options = get_setting("META_IMAGE_SIZE")
if thumbnail_options:
return get_thumbnailer(self.main_image).get_thumbnail(thumbnail_options).height
return self.main_image.height

def get_tags(self):
Expand Down
9 changes: 9 additions & 0 deletions djangocms_blog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@
it's a dictionary with ``size``, ``crop`` and ``upscale`` keys.
"""

BLOG_META_IMAGE_SIZE = None
"""
.. _META_IMAGE_SIZE:
Easy-thumbnail alias configuration for the post meta image;
it's a dictionary with ``size``, ``crop`` and ``upscale`` keys.
Recommended values are {"size": (1200, 630), "crop": True, "upscale": False}
"""

BLOG_URLCONF = "djangocms_blog.urls"
"""
.. _URLCONF:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from django.utils.html import strip_tags
from django.utils.timezone import now
from django.utils.translation import get_language, override
from easy_thumbnails.files import get_thumbnailer
from filer.models import ThumbnailOption
from menus.menu_pool import menu_pool
from parler.tests.utils import override_parler_settings
Expand Down Expand Up @@ -1031,6 +1032,34 @@ def test_model_attributes(self):
post.save()
self.assertFalse(post.is_published)

def test_model_meta_image_setting(self):
post = self._get_post(self._post_data[0]["en"])
post.main_image = self.create_filer_image_object()
post.save()

post.set_current_language("en")
meta_en = post.as_meta()
self.assertEqual(meta_en.image, post.build_absolute_uri(post.main_image.url))
self.assertEqual(meta_en.image_width, post.main_image.width)
self.assertEqual(meta_en.image_height, post.main_image.height)

with override_settings(BLOG_META_IMAGE_SIZE={"size": (1200, 630), "crop": True, "upscale": False}):
meta_en = post.as_meta()
self.assertEqual(
meta_en.image,
post.build_absolute_uri(
get_thumbnailer(post.main_image).get_thumbnail(get_setting("META_IMAGE_SIZE")).url
),
)
self.assertEqual(
meta_en.image_width,
get_thumbnailer(post.main_image).get_thumbnail(get_setting("META_IMAGE_SIZE")).width,
)
self.assertEqual(
meta_en.image_height,
get_thumbnailer(post.main_image).get_thumbnail(get_setting("META_IMAGE_SIZE")).height,
)

def test_urls(self):
self.get_pages()
post = self._get_post(self._post_data[0]["en"])
Expand Down

0 comments on commit 1a8d6c9

Please sign in to comment.