Added Opengraph metas for shared contents
authorThierry Florac <thierry.florac@onf.fr>
Thu, 05 Jul 2018 15:12:11 +0200
changeset 804 16feb37fe22a
parent 803 1fd2ceec9fbf
child 805 c477732d8d62
Added Opengraph metas for shared contents
src/pyams_content/shared/common/skin/opengraph.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/common/skin/opengraph.py	Thu Jul 05 15:12:11 2018 +0200
@@ -0,0 +1,93 @@
+#
+# Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.illustration import IIllustration, IIllustrationTarget
+from pyams_content.component.theme.interfaces import ITagsInfo
+from pyams_content.shared.common.interfaces import IWfSharedContent
+from pyams_i18n.interfaces import II18n, INegotiator, II18nManager
+from pyams_file.interfaces import IThumbnails
+from pyams_skin.interfaces.configuration import IConfiguration
+from pyams_skin.interfaces.metas import IHTMLContentMetas
+from pyams_skin.layer import IPyAMSUserLayer
+from pyams_workflow.interfaces import IWorkflowPublicationInfo
+from zope.dublincore.interfaces import IZopeDublinCore
+
+# import packages
+from pyams_skin.metas import PropertyMeta
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyams_utils.registry import get_utility
+from pyams_utils.traversing import get_parent
+from pyams_utils.url import canonical_url, absolute_url
+from zope.interface import Interface
+
+
+@adapter_config(name='opengraph', context=(IWfSharedContent, IPyAMSUserLayer, Interface), provides=IHTMLContentMetas)
+class OpengraphSharedContentMetasAdapter(ContextRequestViewAdapter):
+    """Opengraph shared content metas adapter"""
+
+    order = 15
+
+    def get_metas(self):
+        context = self.context
+        i18n = II18n(context)
+        request = self.request
+        negotiator = get_utility(INegotiator)
+        lang = negotiator.server_language
+        # main properties
+        yield PropertyMeta('og:title', i18n.query_attribute('title', lang=lang, request=request))
+        yield PropertyMeta('og:description', i18n.query_attribute('description', lang=lang, request=request) or '')
+        yield PropertyMeta('og:type', 'article')
+        # workflow informations
+        yield PropertyMeta('article:modified_time', IZopeDublinCore(context).modified.isoformat())
+        pub_info = IWorkflowPublicationInfo(context)
+        if pub_info.first_publication_date:
+            yield PropertyMeta('article:published_time', pub_info.first_publication_date.isoformat())
+        if pub_info.publication_expiration_date:
+            yield PropertyMeta('article:expiration_time', pub_info.publication_expiration_date.isoformat())
+        # tags
+        tags = ITagsInfo(context)
+        for tag in tags.tags or ():
+            yield PropertyMeta('article:tag', tag.label)
+        # URL and site name
+        yield PropertyMeta('og:url', canonical_url(context, request))
+        configuration = IConfiguration(request.root)
+        yield PropertyMeta('og:site_name', configuration.title)
+        # illustration properties
+        registry = request.registry
+        illustration = None
+        target = context
+        while target is not None:
+            illustration = registry.queryAdapter(target, IIllustration, name='link')
+            if (illustration is None) or (not illustration.has_data()):
+                illustration = registry.queryAdapter(target, IIllustration)
+            if (illustration is not None) and illustration.has_data():
+                break
+            target = get_parent(target, IIllustrationTarget, allow_context=False)
+        if illustration is not None:
+            data = II18n(illustration).query_attribute('data', lang=lang, request=request)
+            thumbnail = IThumbnails(data).get_thumbnail('800x600')
+            yield PropertyMeta('og:image:url', absolute_url(thumbnail, self.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])
+            yield PropertyMeta('og:image:alt', II18n(illustration).query_attribute('alt_title', lang=lang, request=request))
+        # locales properties
+        yield PropertyMeta('of:locale', lang)
+        for lang in II18nManager(context).languages or ():
+            yield PropertyMeta('of:locale:alternate', lang)