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