source: products/quintagroup.canonicalpath/trunk/quintagroup/canonicalpath/adapters.py @ 2105

Last change on this file since 2105 was 2105, checked in by mylan, 14 years ago

#190: Fix bug in default ICanonicalPath adapter registration, property definition

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1import re
2from zope.interface import implements
3from zope.component import adapts
4from zope.schema.interfaces import InvalidValue
5
6from OFS.interfaces import ITraversable
7from Products.CMFCore.utils import getToolByName
8
9from quintagroup.canonicalpath.interfaces import ICanonicalPath
10from quintagroup.canonicalpath.interfaces import ICanonicalLink
11
12PROPERTY_PATH = "canonical_path"
13PROPERTY_LINK = "canonical_link"
14
15_is_canonical = re.compile(
16    r"\S*$"               # non space and no new line(should be pickier)
17    ).match
18
19
20class DefaultCanonicalAdapter(object):
21    """Generic canonical adapter.
22    """
23    adapts(ITraversable)
24
25    prop = None
26
27    def __init__(self, context):
28        self.context = context
29        self.purl = getToolByName(self.context,'portal_url')
30
31    def _validate(self, value):
32        value.strip()
33        if not _is_canonical(value):
34            raise InvalidValue(value)
35        return value
36
37    def getDefault(self):
38        """Return default value for the self.prop"""
39        raise NotImplementedError()
40
41    def getProp(self):
42        """ First of all return value from the self.prop,
43        if self.prop not exist - return default value
44        """
45        if self.context.hasProperty(self.prop):
46            return self.context.getProperty(self.prop)
47
48        return self.getDefault()
49
50    def setProp(self, value):
51        """ First validate value, than add/updater self.prop
52        """
53        value = self._validate(value)
54
55        if self.context.hasProperty(self.prop):
56            self.context._updateProperty(self.prop, value)
57        else:
58            self.context._setProperty(self.prop, value, type="string")
59
60    def delProp(self):
61        """ Delete self.prop customization
62        """
63        if self.context.hasProperty(self.prop):
64            self.context.manage_delProperties(ids=[self.prop,])
65
66
67class DefaultCanonicalPathAdapter(DefaultCanonicalAdapter):
68    """Adapts base content to canonical path.
69    """
70    implements(ICanonicalPath)
71
72    prop = PROPERTY_PATH
73
74    def getDefault(self):
75        return '/'+'/'.join(self.purl.getRelativeContentPath(self.context))
76
77    canonical_path = property(DefaultCanonicalAdapter.getProp,
78                              DefaultCanonicalAdapter.setProp,
79                              DefaultCanonicalAdapter.delProp)
80
81
82class DefaultCanonicalLinkAdapter(DefaultCanonicalAdapter):
83    """Adapts base content to canonical link.
84    """
85    implements(ICanonicalLink)
86
87    prop = PROPERTY_LINK
88
89    def getDefault(self):
90        return self.context.absolute_url()
91
92    canonical_link = property(DefaultCanonicalAdapter.getProp,
93                              DefaultCanonicalAdapter.setProp,
94                              DefaultCanonicalAdapter.delProp)
Note: See TracBrowser for help on using the repository browser.