|
1 # |
|
2 # Copyright (c) 2008-2019 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 from zope.interface import Interface |
|
16 |
|
17 from pyams_content.component.illustration import IIllustration |
|
18 from pyams_content.features.share.interfaces import ISocialShareInfo |
|
19 from pyams_content.root import ISiteRoot |
|
20 from pyams_file.interfaces import IThumbnails |
|
21 from pyams_i18n.interfaces import II18n, INegotiator |
|
22 from pyams_skin.interfaces.configuration import IConfiguration |
|
23 from pyams_skin.interfaces.metas import IHTMLContentMetas |
|
24 from pyams_skin.layer import IPyAMSUserLayer |
|
25 from pyams_skin.metas import ContentMeta, PropertyMeta, SchemaMeta |
|
26 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config |
|
27 from pyams_utils.registry import get_utility |
|
28 from pyams_utils.url import absolute_url, canonical_url |
|
29 |
|
30 |
|
31 @adapter_config(name='opengraph', context=(ISiteRoot, IPyAMSUserLayer, Interface), provides=IHTMLContentMetas) |
|
32 class SiteRootOpengraphMetasAdapter(ContextRequestViewAdapter): |
|
33 """Opengraph site root metas adapter""" |
|
34 |
|
35 order = 15 |
|
36 |
|
37 def get_metas(self): |
|
38 context = self.context |
|
39 request = self.request |
|
40 i18n = II18n(context) |
|
41 negotiator = get_utility(INegotiator) |
|
42 lang = negotiator.server_language |
|
43 |
|
44 configuration = IConfiguration(context) |
|
45 title = configuration.title |
|
46 description = configuration.description |
|
47 |
|
48 # main properties |
|
49 yield PropertyMeta('og:type', 'website') |
|
50 if title: |
|
51 yield PropertyMeta('og:title', title) |
|
52 if description: |
|
53 yield PropertyMeta('og:description', description) |
|
54 |
|
55 # URL and site name |
|
56 yield PropertyMeta('og:url', canonical_url(context, request)) |
|
57 yield PropertyMeta('og:site_name', title) |
|
58 |
|
59 # illustration properties |
|
60 registry = request.registry |
|
61 thumbnail = None |
|
62 illustration = registry.queryAdapter(context, IIllustration, name='link') |
|
63 if (illustration is None) or (not illustration.has_data()): |
|
64 illustration = registry.queryAdapter(context, IIllustration) |
|
65 if (illustration is not None) and illustration.has_data(): |
|
66 data = II18n(illustration).query_attribute('data', lang=lang, request=request) |
|
67 thumbnail = IThumbnails(data).get_thumbnail('800x600') |
|
68 yield PropertyMeta('og:image', absolute_url(thumbnail, request)) |
|
69 if request.scheme == 'https': |
|
70 yield PropertyMeta('og:image:secure_url', absolute_url(thumbnail, request)) |
|
71 else: |
|
72 yield PropertyMeta('og:image:url', absolute_url(thumbnail, request)) |
|
73 yield PropertyMeta('og:image:type', thumbnail.content_type) |
|
74 image_size = thumbnail.image_size |
|
75 yield PropertyMeta('og:image:width', image_size[0]) |
|
76 yield PropertyMeta('og:image:height', image_size[1]) |
|
77 alt = II18n(illustration).query_attribute('alt_title', lang=lang, request=request) |
|
78 if alt: |
|
79 yield PropertyMeta('og:image:alt', alt) |
|
80 |
|
81 # locales properties |
|
82 yield PropertyMeta('og:locale', lang) |
|
83 for other_lang in negotiator.offered_languages or (): |
|
84 if other_lang != lang: |
|
85 yield PropertyMeta('og:locale:alternate', other_lang) |
|
86 |
|
87 # twitter properties |
|
88 share_info = ISocialShareInfo(request.root, None) |
|
89 if (share_info is not None) and share_info.twitter_account: |
|
90 yield ContentMeta('twitter:site', share_info.twitter_account) |
|
91 yield ContentMeta('twitter:creator', share_info.twitter_creator_account or share_info.twitter_account) |
|
92 if illustration is not None: |
|
93 yield ContentMeta('twitter:card', 'summary_large_image') |
|
94 else: |
|
95 yield ContentMeta('twitter:card', 'summary') |
|
96 if title: |
|
97 yield ContentMeta('twitter:title', title) |
|
98 if description: |
|
99 yield ContentMeta('twitter:description', description) |
|
100 |
|
101 # Schema.org properties |
|
102 if title: |
|
103 yield SchemaMeta('name', title) |
|
104 if description: |
|
105 yield SchemaMeta('description', description) |
|
106 if thumbnail is not None: |
|
107 yield SchemaMeta('image', absolute_url(thumbnail, request)) |