Added checks against missing illustrations in OpenGraph adapters
authorThierry Florac <tflorac@ulthar.net>
Wed, 26 Jun 2019 10:00:20 +0200
changeset 447 249a63116475
parent 446 48f08a49656d
child 448 5756946121f6
Added checks against missing illustrations in OpenGraph adapters
src/pyams_default_theme/root/opengraph.py
src/pyams_default_theme/shared/common/opengraph.py
--- a/src/pyams_default_theme/root/opengraph.py	Fri Jun 21 16:16:18 2019 +0200
+++ b/src/pyams_default_theme/root/opengraph.py	Wed Jun 26 10:00:20 2019 +0200
@@ -64,19 +64,20 @@
             illustration = registry.queryAdapter(context, IIllustration)
         if (illustration is not None) and illustration.has_data():
             data = II18n(illustration).query_attribute('data', lang=lang, request=request)
-            thumbnail = IThumbnails(data).get_thumbnail('800x600')
-            yield PropertyMeta('og:image', absolute_url(thumbnail, request))
-            if request.scheme == 'https':
-                yield PropertyMeta('og:image:secure_url', absolute_url(thumbnail, request))
-            else:
-                yield PropertyMeta('og:image:url', absolute_url(thumbnail, request))
-            yield PropertyMeta('og:image:type', thumbnail.content_type)
-            image_size = thumbnail.image_size
-            yield PropertyMeta('og:image:width', image_size[0])
-            yield PropertyMeta('og:image:height', image_size[1])
-            alt = II18n(illustration).query_attribute('alt_title', lang=lang, request=request)
-            if alt:
-                yield PropertyMeta('og:image:alt', alt)
+            if data:
+                thumbnail = IThumbnails(data).get_thumbnail('800x600')
+                yield PropertyMeta('og:image', absolute_url(thumbnail, request))
+                if request.scheme == 'https':
+                    yield PropertyMeta('og:image:secure_url', absolute_url(thumbnail, request))
+                else:
+                    yield PropertyMeta('og:image:url', absolute_url(thumbnail, request))
+                yield PropertyMeta('og:image:type', thumbnail.content_type)
+                image_size = thumbnail.image_size
+                yield PropertyMeta('og:image:width', image_size[0])
+                yield PropertyMeta('og:image:height', image_size[1])
+                alt = II18n(illustration).query_attribute('alt_title', lang=lang, request=request)
+                if alt:
+                    yield PropertyMeta('og:image:alt', alt)
 
         # locales properties
         yield PropertyMeta('og:locale', lang)
--- a/src/pyams_default_theme/shared/common/opengraph.py	Fri Jun 21 16:16:18 2019 +0200
+++ b/src/pyams_default_theme/shared/common/opengraph.py	Wed Jun 26 10:00:20 2019 +0200
@@ -97,19 +97,20 @@
             target = get_parent(target, IIllustrationTarget, allow_context=False)
         if (target is not None) and (illustration is not None):
             data = II18n(illustration).query_attribute('data', lang=lang, request=request)
-            thumbnail = IThumbnails(data).get_thumbnail('800x600')
-            yield PropertyMeta('og:image', absolute_url(thumbnail, request))
-            if request.scheme == 'https':
-                yield PropertyMeta('og:image:secure_url', absolute_url(thumbnail, request))
-            else:
-                yield PropertyMeta('og:image:url', absolute_url(thumbnail, request))
-            yield PropertyMeta('og:image:type', thumbnail.content_type)
-            image_size = thumbnail.image_size
-            yield PropertyMeta('og:image:width', image_size[0])
-            yield PropertyMeta('og:image:height', image_size[1])
-            alt = II18n(illustration).query_attribute('alt_title', lang=lang, request=request)
-            if alt:
-                yield PropertyMeta('og:image:alt', alt)
+            if data:
+                thumbnail = IThumbnails(data).get_thumbnail('800x600')
+                yield PropertyMeta('og:image', absolute_url(thumbnail, request))
+                if request.scheme == 'https':
+                    yield PropertyMeta('og:image:secure_url', absolute_url(thumbnail, request))
+                else:
+                    yield PropertyMeta('og:image:url', absolute_url(thumbnail, request))
+                yield PropertyMeta('og:image:type', thumbnail.content_type)
+                image_size = thumbnail.image_size
+                yield PropertyMeta('og:image:width', image_size[0])
+                yield PropertyMeta('og:image:height', image_size[1])
+                alt = II18n(illustration).query_attribute('alt_title', lang=lang, request=request)
+                if alt:
+                    yield PropertyMeta('og:image:alt', alt)
 
         # locales properties
         yield PropertyMeta('og:locale', lang)