Changeset 1779 in products for quintagroup.seoptimizer/branches/refactoring2.3.0/quintagroup/seoptimizer/browser/viewlets.py
- Timestamp:
- Feb 18, 2010 4:46:46 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
quintagroup.seoptimizer/branches/refactoring2.3.0/quintagroup/seoptimizer/browser/viewlets.py
r1509 r1779 1 1 from cgi import escape 2 from DateTime import DateTime 3 from Acquisition import aq_inner 4 5 from zope.component import queryMultiAdapter 2 6 from zope.component import getMultiAdapter 7 from plone.app.layout.viewlets.common import ViewletBase 8 3 9 from Products.CMFPlone.utils import safe_unicode 4 from plone.app.layout.viewlets.common import ViewletBase5 10 from Products.CMFCore.utils import getToolByName 11 12 from quintagroup.seoptimizer.util import SortedDict 13 from quintagroup.seoptimizer.interfaces import IMetaKeywords 14 from quintagroup.seoptimizer.interfaces import IMappingMetaTags 15 16 from Products.CMFPlone.PloneTool import * 17 18 class SEOTagsViewlet( ViewletBase ): 19 """ Simple viewlet for custom title rendering. 20 """ 21 22 def render(self): 23 TEMPLATE = '<meta name="%s" content="%s"/>' 24 return '\n'.join([TEMPLATE % (k,v) \ 25 for k,v in self.listMetaTags().items()]) 26 27 def listMetaTags(self): 28 """Calculate list metatags""" 29 30 result = SortedDict() 31 32 site_props = getToolByName(self, 'portal_properties').site_properties 33 use_all = site_props.getProperty('exposeDCMetaTags', None) 34 35 seo_context = queryMultiAdapter((self.context, self.request), name='seo_context') 36 adapter = IMappingMetaTags(self.context, None) 37 mapping_metadata = adapter and adapter.getMappingMetaTags() or SortedDict() 38 39 if not use_all: 40 metadata_names = mapping_metadata.has_key('DC.description') \ 41 and {'DC.description': mapping_metadata['DC.description']} \ 42 or SortedDict() 43 if mapping_metadata.has_key('description'): 44 metadata_names['description'] = mapping_metadata['description'] 45 else: 46 metadata_names = mapping_metadata 47 48 for key, accessor in metadata_names.items(): 49 if accessor == 'meta_keywords': 50 # Render all the existing keywords for the current content type 51 adapter = IMetaKeywords(self.context, None) 52 if adapter is not None: 53 keywords = adapter.getMetaKeywords() 54 if keywords: 55 result['keywords'] = keywords 56 continue 57 58 method = getattr(seo_context, accessor, None) 59 if method is None: 60 method = getattr(aq_inner(self.context).aq_explicit, accessor, None) 61 62 if not callable(method): 63 continue 64 65 # Catch AttributeErrors raised by some AT applications 66 try: 67 value = method() 68 except AttributeError: 69 value = None 70 71 if not value: 72 # No data 73 continue 74 if accessor == 'Publisher' and value == 'No publisher': 75 # No publisher is hardcoded (TODO: still?) 76 continue 77 if isinstance(value, (list, tuple)): 78 # convert a list to a string 79 value = ', '.join(value) 80 81 # Special cases 82 if accessor == 'Description' and not metadata_names.has_key('description'): 83 result['description'] = value 84 elif accessor == 'Subject' and not metadata_names.has_key('keywords'): 85 result['keywords'] = value 86 87 if accessor not in ('Description', 'Subject'): 88 result[key] = value 89 90 if use_all: 91 created = self.context.CreationDate() 92 93 try: 94 effective = self.context.EffectiveDate() 95 if effective == 'None': 96 effective = None 97 if effective: 98 effective = DateTime(effective) 99 except AttributeError: 100 effective = None 101 102 try: 103 expires = self.context.ExpirationDate() 104 if expires == 'None': 105 expires = None 106 if expires: 107 expires = DateTime(expires) 108 except AttributeError: 109 expires = None 110 111 # Filter out DWIMish artifacts on effective / expiration dates 112 if effective is not None and \ 113 effective > FLOOR_DATE and \ 114 effective != created: 115 eff_str = effective.Date() 116 else: 117 eff_str = '' 118 119 if expires is not None and expires < CEILING_DATE: 120 exp_str = expires.Date() 121 else: 122 exp_str = '' 123 124 if exp_str or exp_str: 125 result['DC.date.valid_range'] = '%s - %s' % (eff_str, exp_str) 126 127 # add custom meta tags (added from qseo tab by user) 128 # for given context and default from configlet 129 custom_meta_tags = seo_context and seo_context.seo_customMetaTags() or [] 130 for tag in custom_meta_tags: 131 if tag['meta_content']: 132 result[tag['meta_name']] = tag['meta_content'] 133 134 return result 135 6 136 7 137
Note: See TracChangeset
for help on using the changeset viewer.