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
RevLine 
[1921]1import re
[782]2from zope.interface import implements
3from zope.component import adapts
[1921]4from zope.schema.interfaces import InvalidValue
[782]5
[1695]6from OFS.interfaces import ITraversable
[2369]7from OFS.interfaces import IPropertyManager
8
[782]9from Products.CMFCore.utils import getToolByName
10
[1695]11from quintagroup.canonicalpath.interfaces import ICanonicalPath
[1927]12from quintagroup.canonicalpath.interfaces import ICanonicalLink
[782]13
[1927]14PROPERTY_PATH = "canonical_path"
15PROPERTY_LINK = "canonical_link"
[782]16
[3601]17# non space and no new line(should be pickier)
18_is_canonical = re.compile(r"\S*$").match
[1921]19
20
[2369]21class DefaultPropertyAdapter(object):
22    """Generic property adapter.
[782]23    """
[2369]24    adapts(IPropertyManager)
[782]25
[2103]26    prop = None
27
[782]28    def __init__(self, context):
29        self.context = context
30
[2103]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
[1921]38        """
[2103]39        if self.context.hasProperty(self.prop):
40            return self.context.getProperty(self.prop)
[1921]41
[2103]42        return self.getDefault()
[1921]43
[2103]44    def setProp(self, value):
45        """ First validate value, than add/updater self.prop
[1921]46        """
[2103]47        if self.context.hasProperty(self.prop):
48            self.context._updateProperty(self.prop, value)
[1921]49        else:
[2103]50            self.context._setProperty(self.prop, value, type="string")
[1921]51
[2103]52    def delProp(self):
53        """ Delete self.prop customization
[1933]54        """
[2103]55        if self.context.hasProperty(self.prop):
[3145]56            self.context.manage_delProperties(ids=[self.prop, ])
[1927]57
58
[2369]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
[2103]78class DefaultCanonicalPathAdapter(DefaultCanonicalAdapter):
79    """Adapts base content to canonical path.
[1927]80    """
[2105]81    implements(ICanonicalPath)
[1927]82
[2103]83    prop = PROPERTY_PATH
[1927]84
[2369]85    def __init__(self, context):
86        super(DefaultCanonicalPathAdapter, self).__init__(context)
[3145]87        self.purl = getToolByName(self.context, 'portal_url')
[2369]88
[2103]89    def getDefault(self):
[3145]90        return '/' + '/'.join(self.purl.getRelativeContentPath(self.context))
[1927]91
[2105]92    canonical_path = property(DefaultCanonicalAdapter.getProp,
93                              DefaultCanonicalAdapter.setProp,
94                              DefaultCanonicalAdapter.delProp)
[1927]95
96
[2103]97class DefaultCanonicalLinkAdapter(DefaultCanonicalAdapter):
98    """Adapts base content to canonical link.
99    """
100    implements(ICanonicalLink)
[1927]101
[2103]102    prop = PROPERTY_LINK
[1927]103
[2103]104    def getDefault(self):
105        return self.context.absolute_url()
[1933]106
[2105]107    canonical_link = property(DefaultCanonicalAdapter.getProp,
108                              DefaultCanonicalAdapter.setProp,
109                              DefaultCanonicalAdapter.delProp)
Note: See TracBrowser for help on using the repository browser.