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