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