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

Last change on this file since 2103 was 2103, checked in by mylan, 11 years ago

#190: Extract general part for canonical_link and canonical_path adapters

  • Property svn:eol-style set to native
File size: 2.4 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(ICanonicalLink)
71
72    prop = PROPERTY_PATH
73
74    def getDefault(self):
75        return '/'+'/'.join(self.purl.getRelativeContentPath(self.context))
76
77    canonical_path = property(getProp, setProp, delProp)
78
79
80class DefaultCanonicalLinkAdapter(DefaultCanonicalAdapter):
81    """Adapts base content to canonical link.
82    """
83    implements(ICanonicalLink)
84
85    prop = PROPERTY_LINK
86
87    def getDefault(self):
88        return self.context.absolute_url()
89
90    canonical_link = property(getProp, setProp, delProp)
Note: See TracBrowser for help on using the repository browser.