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 | _is_canonical = re.compile( |
---|
18 | r"\S*$" # non space and no new line(should be pickier) |
---|
19 | ).match |
---|
20 | |
---|
21 | |
---|
22 | class DefaultPropertyAdapter(object): |
---|
23 | """Generic property adapter. |
---|
24 | """ |
---|
25 | adapts(IPropertyManager) |
---|
26 | |
---|
27 | prop = None |
---|
28 | |
---|
29 | def __init__(self, context): |
---|
30 | self.context = context |
---|
31 | |
---|
32 | def getDefault(self): |
---|
33 | """Return default value for the self.prop""" |
---|
34 | raise NotImplementedError() |
---|
35 | |
---|
36 | def getProp(self): |
---|
37 | """ First of all return value from the self.prop, |
---|
38 | if self.prop not exist - return default value |
---|
39 | """ |
---|
40 | if self.context.hasProperty(self.prop): |
---|
41 | return self.context.getProperty(self.prop) |
---|
42 | |
---|
43 | return self.getDefault() |
---|
44 | |
---|
45 | def setProp(self, value): |
---|
46 | """ First validate value, than add/updater self.prop |
---|
47 | """ |
---|
48 | if self.context.hasProperty(self.prop): |
---|
49 | self.context._updateProperty(self.prop, value) |
---|
50 | else: |
---|
51 | self.context._setProperty(self.prop, value, type="string") |
---|
52 | |
---|
53 | def delProp(self): |
---|
54 | """ Delete self.prop customization |
---|
55 | """ |
---|
56 | if self.context.hasProperty(self.prop): |
---|
57 | self.context.manage_delProperties(ids=[self.prop, ]) |
---|
58 | |
---|
59 | |
---|
60 | class DefaultCanonicalAdapter(DefaultPropertyAdapter): |
---|
61 | """Generic canonical adapter. |
---|
62 | Add validation support to functionality of DefaultPropertyAdapter. |
---|
63 | """ |
---|
64 | adapts(ITraversable) |
---|
65 | |
---|
66 | def _validate(self, value): |
---|
67 | value.strip() |
---|
68 | if not _is_canonical(value): |
---|
69 | raise InvalidValue(value) |
---|
70 | return value |
---|
71 | |
---|
72 | def setProp(self, value): |
---|
73 | """ First validate value, than add/updater self.prop |
---|
74 | """ |
---|
75 | value = self._validate(value) |
---|
76 | super(DefaultCanonicalAdapter, self).setProp(value) |
---|
77 | |
---|
78 | |
---|
79 | class DefaultCanonicalPathAdapter(DefaultCanonicalAdapter): |
---|
80 | """Adapts base content to canonical path. |
---|
81 | """ |
---|
82 | implements(ICanonicalPath) |
---|
83 | |
---|
84 | prop = PROPERTY_PATH |
---|
85 | |
---|
86 | def __init__(self, context): |
---|
87 | super(DefaultCanonicalPathAdapter, self).__init__(context) |
---|
88 | self.purl = getToolByName(self.context, 'portal_url') |
---|
89 | |
---|
90 | def getDefault(self): |
---|
91 | return '/' + '/'.join(self.purl.getRelativeContentPath(self.context)) |
---|
92 | |
---|
93 | canonical_path = property(DefaultCanonicalAdapter.getProp, |
---|
94 | DefaultCanonicalAdapter.setProp, |
---|
95 | DefaultCanonicalAdapter.delProp) |
---|
96 | |
---|
97 | |
---|
98 | class DefaultCanonicalLinkAdapter(DefaultCanonicalAdapter): |
---|
99 | """Adapts base content to canonical link. |
---|
100 | """ |
---|
101 | implements(ICanonicalLink) |
---|
102 | |
---|
103 | prop = PROPERTY_LINK |
---|
104 | |
---|
105 | def getDefault(self): |
---|
106 | return self.context.absolute_url() |
---|
107 | |
---|
108 | canonical_link = property(DefaultCanonicalAdapter.getProp, |
---|
109 | DefaultCanonicalAdapter.setProp, |
---|
110 | DefaultCanonicalAdapter.delProp) |
---|