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

Last change on this file was 3601, checked in by vmaksymiv, 11 years ago

fix pep

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