1 | import cgi |
---|
2 | from DateTime import DateTime |
---|
3 | from Acquisition import aq_inner |
---|
4 | |
---|
5 | from zope.component import queryAdapter |
---|
6 | from zope.component import queryMultiAdapter |
---|
7 | from zope.component import getMultiAdapter |
---|
8 | from plone.app.layout.viewlets.common import ViewletBase |
---|
9 | |
---|
10 | from Products.CMFPlone.utils import safe_unicode, getSiteEncoding |
---|
11 | |
---|
12 | from quintagroup.seoptimizer.util import SortedDict |
---|
13 | from quintagroup.seoptimizer.interfaces import IMetaKeywords |
---|
14 | from quintagroup.seoptimizer.interfaces import IMappingMetaTags |
---|
15 | from quintagroup.seoptimizer.browser.seo_configlet import ISEOConfigletSchema |
---|
16 | |
---|
17 | from Products.CMFPlone.PloneTool import FLOOR_DATE, CEILING_DATE |
---|
18 | |
---|
19 | |
---|
20 | def escape(value): |
---|
21 | """Extended escape""" |
---|
22 | value = cgi.escape(value, True) |
---|
23 | return value.replace("'", "'") |
---|
24 | |
---|
25 | |
---|
26 | class SEOTagsViewlet(ViewletBase): |
---|
27 | """ Simple viewlet for custom title rendering. |
---|
28 | """ |
---|
29 | |
---|
30 | def render(self): |
---|
31 | TEMPLATE = '<meta name="%s" content="%s"/>' |
---|
32 | enc = getSiteEncoding(self.context) |
---|
33 | sfuncd = lambda x, enc=enc: safe_unicode(x, enc) |
---|
34 | return u'\n'.join([TEMPLATE % tuple(map(sfuncd, (k, v))) \ |
---|
35 | for k, v in self.listMetaTags().items()]) |
---|
36 | |
---|
37 | def listMetaTags(self): |
---|
38 | """Calculate list metatags""" |
---|
39 | |
---|
40 | result = SortedDict() |
---|
41 | pps = queryMultiAdapter((self.context, self.request), |
---|
42 | name="plone_portal_state") |
---|
43 | seo_global = queryAdapter(pps.portal(), ISEOConfigletSchema) |
---|
44 | seo_context = queryMultiAdapter((self.context, self.request), |
---|
45 | name='seo_context') |
---|
46 | |
---|
47 | use_all = seo_global.exposeDCMetaTags |
---|
48 | adapter = IMappingMetaTags(self.context, None) |
---|
49 | mapping_metadata = adapter and adapter.getMappingMetaTags() \ |
---|
50 | or SortedDict() |
---|
51 | |
---|
52 | if not use_all: |
---|
53 | metadata_names = 'DC.description' in mapping_metadata and \ |
---|
54 | {'DC.description': mapping_metadata['DC.description']} \ |
---|
55 | or SortedDict() |
---|
56 | if 'description' in mapping_metadata: |
---|
57 | metadata_names['description'] = mapping_metadata['description'] |
---|
58 | else: |
---|
59 | metadata_names = mapping_metadata |
---|
60 | |
---|
61 | for key, accessor in metadata_names.items(): |
---|
62 | if accessor == 'meta_keywords': |
---|
63 | # Render all the existing keywords for the current content type |
---|
64 | adapter = IMetaKeywords(self.context, None) |
---|
65 | if adapter is not None: |
---|
66 | keywords = adapter.getMetaKeywords() |
---|
67 | if keywords: |
---|
68 | result['keywords'] = keywords |
---|
69 | continue |
---|
70 | |
---|
71 | if accessor in seo_context._seotags: |
---|
72 | value = seo_context._seotags.get(accessor, None) |
---|
73 | else: |
---|
74 | method = getattr(seo_context, accessor, None) |
---|
75 | if method is None: |
---|
76 | method = getattr(aq_inner(self.context).aq_explicit, |
---|
77 | accessor, None) |
---|
78 | |
---|
79 | if not callable(method): |
---|
80 | continue |
---|
81 | |
---|
82 | # Catch AttributeErrors raised by some AT applications |
---|
83 | try: |
---|
84 | value = method() |
---|
85 | except AttributeError: |
---|
86 | value = None |
---|
87 | |
---|
88 | if not value: |
---|
89 | # No data |
---|
90 | continue |
---|
91 | if accessor == 'Publisher' and value == 'No publisher': |
---|
92 | # No publisher is hardcoded (TODO: still?) |
---|
93 | continue |
---|
94 | if isinstance(value, (list, tuple)): |
---|
95 | # convert a list to a string |
---|
96 | value = ', '.join(value) |
---|
97 | |
---|
98 | # Special cases |
---|
99 | if accessor == 'Description' and \ |
---|
100 | not 'description' in metadata_names: |
---|
101 | result['description'] = escape(value) |
---|
102 | elif accessor == 'Subject' and \ |
---|
103 | not 'keywords' in metadata_names: |
---|
104 | result['keywords'] = escape(value) |
---|
105 | |
---|
106 | if accessor not in ('Description', 'Subject'): |
---|
107 | result[key] = escape(value) |
---|
108 | |
---|
109 | if use_all: |
---|
110 | created = self.context.CreationDate() |
---|
111 | |
---|
112 | try: |
---|
113 | effective = self.context.EffectiveDate() |
---|
114 | if effective == 'None': |
---|
115 | effective = None |
---|
116 | if effective: |
---|
117 | effective = DateTime(effective) |
---|
118 | except AttributeError: |
---|
119 | effective = None |
---|
120 | |
---|
121 | try: |
---|
122 | expires = self.context.ExpirationDate() |
---|
123 | if expires == 'None': |
---|
124 | expires = None |
---|
125 | if expires: |
---|
126 | expires = DateTime(expires) |
---|
127 | except AttributeError: |
---|
128 | expires = None |
---|
129 | |
---|
130 | # Filter out DWIMish artifacts on effective / expiration dates |
---|
131 | if effective is not None and \ |
---|
132 | effective > FLOOR_DATE and \ |
---|
133 | effective != created: |
---|
134 | eff_str = effective.Date() |
---|
135 | else: |
---|
136 | eff_str = '' |
---|
137 | |
---|
138 | if expires is not None and expires < CEILING_DATE: |
---|
139 | exp_str = expires.Date() |
---|
140 | else: |
---|
141 | exp_str = '' |
---|
142 | |
---|
143 | if exp_str or exp_str: |
---|
144 | result['DC.date.valid_range'] = '%s - %s' % (eff_str, exp_str) |
---|
145 | |
---|
146 | # add custom meta tags (added from qseo tab by user) |
---|
147 | # for given context and default from configlet |
---|
148 | custom_meta_tags = seo_context and \ |
---|
149 | seo_context['seo_customMetaTags'] or [] |
---|
150 | for tag in custom_meta_tags: |
---|
151 | if tag['meta_content']: |
---|
152 | result[tag['meta_name']] = escape(tag['meta_content']) |
---|
153 | |
---|
154 | return result |
---|
155 | |
---|
156 | |
---|
157 | class TitleCommentViewlet(ViewletBase): |
---|
158 | """ Simple viewlet for custom title rendering. |
---|
159 | """ |
---|
160 | |
---|
161 | def update(self): |
---|
162 | self.portal_state = getMultiAdapter((self.context, self.request), |
---|
163 | name=u'plone_portal_state') |
---|
164 | self.context_state = getMultiAdapter((self.context, self.request), |
---|
165 | name=u'plone_context_state') |
---|
166 | self.seo_context = getMultiAdapter((self.context, self.request), |
---|
167 | name=u'seo_context') |
---|
168 | |
---|
169 | self.override_title = self.seo_context['has_seo_title'] |
---|
170 | self.has_comments = self.seo_context['has_html_comment'] |
---|
171 | |
---|
172 | def std_title(self): |
---|
173 | page_title = safe_unicode(self.context_state.object_title()) |
---|
174 | portal_title = safe_unicode(self.portal_state.portal_title()) |
---|
175 | if page_title == portal_title: |
---|
176 | return u"<title>%s</title>" % (escape(portal_title)) |
---|
177 | else: |
---|
178 | return u"<title>%s — %s</title>" % ( |
---|
179 | escape(safe_unicode(page_title)), |
---|
180 | escape(safe_unicode(portal_title))) |
---|
181 | |
---|
182 | def render(self): |
---|
183 | if self.override_title: |
---|
184 | qseo_title = u"<title>%s</title>" % escape(safe_unicode( |
---|
185 | self.seo_context["seo_title"])) |
---|
186 | else: |
---|
187 | qseo_title = self.std_title() |
---|
188 | |
---|
189 | comments = "" |
---|
190 | if self.has_comments: |
---|
191 | comments = u"\n<!--%s-->" % escape(safe_unicode( |
---|
192 | self.seo_context["seo_html_comment"])) |
---|
193 | |
---|
194 | return qseo_title + comments |
---|
195 | |
---|
196 | |
---|
197 | class CustomScriptViewlet(ViewletBase): |
---|
198 | """ Simple viewlet for custom script rendering. |
---|
199 | """ |
---|
200 | def getCustomScript(self): |
---|
201 | pps = queryMultiAdapter((self.context, self.request), |
---|
202 | name="plone_portal_state") |
---|
203 | gseo = queryAdapter(pps.portal(), ISEOConfigletSchema) |
---|
204 | if gseo: |
---|
205 | return gseo.custom_script |
---|
206 | return '' |
---|
207 | |
---|
208 | def render(self): |
---|
209 | return safe_unicode("""%s""" % self.getCustomScript()) |
---|
210 | |
---|
211 | |
---|
212 | class CanonicalUrlViewlet(ViewletBase): |
---|
213 | """ Simple viewlet for canonical url link rendering. |
---|
214 | """ |
---|
215 | def render(self): |
---|
216 | seoc = getMultiAdapter((self.context, self.request), |
---|
217 | name=u'seo_context') |
---|
218 | if seoc['seo_canonical']: |
---|
219 | return """<link rel="canonical" href="%s" />""" \ |
---|
220 | % seoc['seo_canonical'] |
---|
221 | return "" |
---|