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

Last change on this file since 3599 was 3599, checked in by vmaksymiv, 7 years ago

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