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

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

#177: Added delete property functionality for ICanonicalPath, ICanonicalLink

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