source: products/quintagroup.seoptimizer/trunk/quintagroup/seoptimizer/browser/views.py @ 2247

Last change on this file since 2247 was 2247, checked in by crchemist, 14 years ago

Fix http://plone.org/products/plone-seo/issues/24

  • Property svn:eol-style set to native
File size: 13.5 KB
Line 
1from time import time
2from DateTime import DateTime
3from Acquisition import aq_inner
4from zope.component import queryAdapter
5from zope.component import queryMultiAdapter
6from zope.schema.interfaces import InvalidValue
7
8from plone.memoize import view, ram
9
10from Products.Five.browser import BrowserView
11from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
12from Products.CMFPlone.utils import getSiteEncoding
13
14from quintagroup.canonicalpath.interfaces import ICanonicalLink
15from quintagroup.canonicalpath.adapters import PROPERTY_LINK as CANONICAL_PROPERTY
16
17from quintagroup.seoptimizer.browser.seo_configlet import ISEOConfigletSchema
18from quintagroup.seoptimizer import SeoptimizerMessageFactory as _
19
20SEPERATOR = '|'
21SEO_PREFIX = 'seo_'
22PROP_PREFIX = 'qSEO_'
23SUFFIX = '_override'
24PROP_CUSTOM_PREFIX = 'qSEO_custom_'
25
26# Ram cache function, which depends on plone instance and time
27def plone_instance_time(method, self, *args, **kwargs):
28    return (self.pps.portal(), time() // (60 * 60))
29
30class SEOContext( BrowserView ):
31    """ This class contains methods that allows to edit html header meta tags.
32    """
33
34    def __init__(self, *args, **kwargs):
35        super(SEOContext, self).__init__(*args, **kwargs)
36        self.pps = queryMultiAdapter((self.context, self.request), name="plone_portal_state")
37        self.pcs = queryMultiAdapter((self.context, self.request), name="plone_context_state")
38        self.gseo = queryAdapter(self.pps.portal(), ISEOConfigletSchema)
39        self._seotags = self._getSEOTags()
40
41    def __getitem__(self, key):
42        return self._seotags.get(key, '')
43
44    @view.memoize
45    def _getSEOTags(self):
46        seotags = {
47            "seo_title": self.getSEOProperty( 'qSEO_title', default=self.pcs.object_title() ),
48            "seo_robots": self.getSEOProperty( 'qSEO_robots', default='ALL'),
49            "seo_description": self.getSEOProperty( 'qSEO_description', accessor='Description' ),
50            "seo_distribution": self.getSEOProperty( 'qSEO_distribution', default="Global"),
51            "seo_customMetaTags": self.seo_customMetaTags(),
52            # "seo_localCustomMetaTags": self.seo_localCustomMetaTags(),
53            # "seo_globalCustomMetaTags": self.seo_globalCustomMetaTags(),
54            "seo_html_comment": self.getSEOProperty( 'qSEO_html_comment', default='' ),
55            "meta_keywords": self.getSEOProperty('qSEO_keywords', 'Subject', ()),
56            "seo_keywords": self.getSEOProperty('qSEO_keywords', default=()),
57            "seo_canonical": self.getCanonical(),
58            # Add test properties
59            "has_seo_title": self.context.hasProperty('qSEO_title'),
60            "has_seo_robots": self.context.hasProperty('qSEO_robots'),
61            "has_seo_description": self.context.hasProperty( 'qSEO_description'),
62            "has_seo_distribution": self.context.hasProperty( 'qSEO_distribution'),
63            "has_html_comment": self.context.hasProperty('qSEO_html_comment'),
64            "has_seo_keywords": self.context.hasProperty('qSEO_keywords'),
65            "has_seo_canonical": self.context.hasProperty(CANONICAL_PROPERTY),
66            }
67        #seotags["seo_nonEmptylocalMetaTags"] = bool(seotags["seo_localCustomMetaTags"])
68        return seotags
69
70    def getSEOProperty( self, property_name, accessor='', default=None ):
71        """ Get value from seo property by property name.
72        """
73        context = aq_inner(self.context)
74
75        if context.hasProperty(property_name):
76            return context.getProperty(property_name, default)
77
78        if accessor:
79            method = getattr(context, accessor, default)
80            if not callable(method):
81                return default
82
83            # Catch AttributeErrors raised by some AT applications
84            try:
85                value = method()
86            except AttributeError:
87                value = default
88
89            return value
90        return default
91
92    def seo_customMetaTags( self ):
93        """Returned seo custom metatags from default_custom_metatags property in seo_properties
94           (global seo custom metatags) with update from seo custom metatags properties
95           in context (local seo custom metatags).
96        """
97        glob, loc = self.seo_globalCustomMetaTags(), self.seo_localCustomMetaTags()
98        gnames = set(map(lambda x: x['meta_name'], glob))
99        lnames = set(map(lambda x: x['meta_name'], loc))
100        # Get untouch global, override global in custom and new custom meta tags
101        untouchglob = [t for t in glob if t['meta_name'] in list(gnames - lnames)]
102        return untouchglob + loc
103
104    def seo_globalWithoutLocalCustomMetaTags( self ):
105        """Returned seo custom metatags from default_custom_metatags property in seo_properties
106           (global seo custom metatags) without seo custom metatags from properties
107           in context (local seo custom metatags).
108        """
109        glob, loc = self.seo_globalCustomMetaTags(), self.seo_localCustomMetaTags()
110        gnames = set(map(lambda x: x['meta_name'], glob))
111        lnames = set(map(lambda x: x['meta_name'], loc))
112        return [t for t in glob if t['meta_name'] in list(gnames - lnames)]
113
114    def seo_localCustomMetaTags( self ):
115        """ Returned seo custom metatags from properties in context (local seo custom metatags).
116        """
117        result = []
118        property_prefix = 'qSEO_custom_'
119        context = aq_inner(self.context)
120        for property, value in context.propertyItems():
121            if property.startswith(property_prefix) and property[len(property_prefix):]:
122                result.append({'meta_name'    : property[len(property_prefix):],
123                               'meta_content' : value})
124        return result
125
126    @ram.cache(plone_instance_time)
127    def seo_globalCustomMetaTags( self ):
128        """ Returned seo custom metatags from default_custom_metatags property in seo_properties.
129        """
130        result = []
131        if self.gseo:
132            for tag in self.gseo.default_custom_metatags:
133                name_value = tag.split(SEPERATOR)
134                if name_value[0]:
135                    result.append({'meta_name'    : name_value[0],
136                                   'meta_content' : len(name_value) == 2 and name_value[1] or ''})
137        return result
138
139    def getCanonical(self):
140        canonical = queryAdapter(self.context, ICanonicalLink)
141        return canonical and canonical.canonical_link or ""
142
143
144class SEOContextPropertiesView( BrowserView ):
145    """ This class contains methods that allows to manage seo properties.
146    """
147    template = ViewPageTemplateFile('templates/seo_context_properties.pt')
148
149    def __init__(self, *args, **kwargs):
150        super(SEOContextPropertiesView, self).__init__(*args, **kwargs)
151        self.pps = queryMultiAdapter((self.context, self.request),
152                                     name="plone_portal_state")
153        self.gseo = queryAdapter(self.pps.portal(), ISEOConfigletSchema)
154
155
156    def test( self, condition, first, second ):
157        """
158        """
159        return condition and first or second
160
161    def validateSEOProperty(self, property, value):
162        """ Validate a seo property.
163        """
164        return ''
165
166    def setProperty(self, property, value, type='string'):
167        """ Add a new property.
168
169            Sets a new property with the given id, value and type or changes it.
170        """
171        context = aq_inner(self.context)
172        state = self.validateSEOProperty(property, value)
173        if not state:
174            if context.hasProperty(property):
175                context.manage_changeProperties({property: value})
176            else:
177                context.manage_addProperty(property, value, type)
178        return state
179
180    def manageSEOProps(self, **kw):
181        """ Manage seo properties.
182        """
183        context = aq_inner(self.context)
184        state = ''
185        delete_list, seo_overrides_keys, seo_keys = [], [], []
186        seo_items = dict([(k[len(SEO_PREFIX):],v) for k,v in kw.items() if k.startswith(SEO_PREFIX)])
187        for key in seo_items.keys():
188            if key.endswith(SUFFIX):
189                seo_overrides_keys.append(key[:-len(SUFFIX)])
190            else:
191                seo_keys.append(key)
192        for seo_key in seo_keys:
193            if seo_key == 'custommetatags':
194                self.manageSEOCustomMetaTagsProperties(**kw)
195            else:
196                if seo_key in seo_overrides_keys and seo_items.get(seo_key+SUFFIX):
197                    seo_value = seo_items[seo_key]
198                    if seo_key == 'canonical':
199                        try:
200                            ICanonicalLink(self.context).canonical_link = seo_value
201                        except InvalidValue, e:
202                            state = "'%s' - wrong canonical url" % str(e)
203                    else:
204                        t_value = 'string'
205                        if type(seo_value)==type([]) or type(seo_value)==type(()): t_value = 'lines'
206                        state = self.setProperty(PROP_PREFIX+seo_key, seo_value, type=t_value)
207                    if state:
208                        return state
209                elif seo_key == 'canonical':
210                    del ICanonicalLink(self.context).canonical_link
211                elif context.hasProperty(PROP_PREFIX+seo_key):
212                    delete_list.append(PROP_PREFIX+seo_key)
213        if delete_list:
214            context.manage_delProperties(delete_list)
215        return state
216
217    def setSEOCustomMetaTags(self, custommetatags):
218        """ Set seo custom metatags properties.
219        """
220        context = aq_inner(self.context)
221        for tag in custommetatags:
222            self.setProperty('%s%s' % (PROP_CUSTOM_PREFIX, tag['meta_name']), tag['meta_content'])
223
224    def delAllSEOCustomMetaTagsProperties(self):
225        """ Delete all seo custom metatags properties.
226        """
227        context = aq_inner(self.context)
228        delete_list = []
229        for property, value in context.propertyItems():
230            if property.startswith(PROP_CUSTOM_PREFIX)  and not property == PROP_CUSTOM_PREFIX:
231                delete_list.append(property)
232        if delete_list:
233            context.manage_delProperties(delete_list)
234
235    def updateSEOCustomMetaTagsProperties(self, custommetatags):
236        """ Update seo custom metatags properties.
237        """
238        globalCustomMetaTags = []
239        if self.gseo:
240            custom_meta_tags = self.gseo.default_custom_metatags
241            for tag in custom_meta_tags:
242                name_value = tag.split(SEPERATOR)
243                if name_value[0]:
244                    globalCustomMetaTags.append(
245                        {'meta_name' : name_value[0],
246                         'meta_content' : len(name_value) == 1 and '' or name_value[1]})
247        for tag in custommetatags:
248            meta_name, meta_content = tag['meta_name'], tag['meta_content']
249            if meta_name:
250                if not [gmt for gmt in globalCustomMetaTags \
251                        if (gmt['meta_name']==meta_name and gmt['meta_content']==meta_content)]:
252                    self.setProperty('%s%s' % (PROP_CUSTOM_PREFIX, meta_name), meta_content)
253
254    def manageSEOCustomMetaTagsProperties(self, **kw):
255        """ Update seo custom metatags properties, if enabled checkbox override or delete properties.
256
257            Change object properties by passing either a mapping object
258            of name:value pairs {'foo':6} or passing name=value parameters.
259        """
260        context = aq_inner(self.context)
261        self.delAllSEOCustomMetaTagsProperties()
262        if kw.get('seo_custommetatags_override'):
263            custommetatags = kw.get('seo_custommetatags', {})
264            self.updateSEOCustomMetaTagsProperties(custommetatags)
265
266    def getPropertyStopWords(self):
267        """ Get property 'stop_words' from SEO Properties tool.
268        """
269        enc = getSiteEncoding(self.context)
270        # self.gseo.stop_words return list of unicode objects,
271        # and may contains stop words in different languages.
272        # So we must return encoded strings.
273        sw = map(lambda x:unicode.encode(x, enc), self.gseo.stop_words)
274        return str(sw)
275
276    def getPropertyFields(self):
277        """ Get property 'fields' from SEO Properties tool.
278        """
279        # self.gseo.fields return list of unicode objects,
280        # so *str* use as encoding function from unicode to latin-1 string.
281        fields_id = map(str, self.gseo.fields)
282        return str(fields_id)
283
284    def __call__( self ):
285        """ Perform the update seo properties and redirect if necessary,
286            or render the page Call method.
287        """
288        context = aq_inner(self.context)
289        request = self.request
290        form = self.request.form
291        submitted = form.get('form.submitted', False)
292        if submitted:
293            state = self.manageSEOProps(**form)
294            if not state:
295                state = _('seoproperties_saved', default=u'Content SEO properties have been saved.')
296                context.plone_utils.addPortalMessage(state)
297                kwargs = {'modification_date' : DateTime()} 
298                context.plone_utils.contentEdit(context, **kwargs)
299                return request.response.redirect(self.context.absolute_url())
300            context.plone_utils.addPortalMessage(state, 'error')
301        return self.template()
302
303
304class VisibilityCheckerView( BrowserView ):
305    """ This class contains methods that visibility checker.
306    """
307
308    def checkVisibilitySEOAction(self):
309        """ Checks visibility 'SEO Properties' action for content
310        """
311        context = aq_inner(self.context)
312        plone = queryMultiAdapter((self, self.request),name="plone_portal_state").portal()
313        adapter = ISEOConfigletSchema(plone)
314        return bool(self.context.portal_type in adapter.types_seo_enabled)
315
Note: See TracBrowser for help on using the repository browser.