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

Last change on this file since 2103 was 2103, checked in by mylan, 14 years ago

#190: Extract general part for canonical_link and canonical_path adapters

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