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

Last change on this file since 3145 was 3145, checked in by vmaksymiv, 9 years ago

pep8 fixes

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