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 Products.CMFCore.utils import getToolByName |
---|
8 | |
---|
9 | from quintagroup.canonicalpath.interfaces import ICanonicalPath |
---|
10 | from quintagroup.canonicalpath.interfaces import ICanonicalLink |
---|
11 | |
---|
12 | PROPERTY_PATH = "canonical_path" |
---|
13 | PROPERTY_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 | |
---|
20 | class DefaultCanonicalAdapter(object): |
---|
21 | """Generic canonical adapter. |
---|
22 | """ |
---|
23 | adapts(ITraversable) |
---|
24 | |
---|
25 | prop = None |
---|
26 | |
---|
27 | def __init__(self, context): |
---|
28 | self.context = context |
---|
29 | self.purl = getToolByName(self.context,'portal_url') |
---|
30 | |
---|
31 | def _validate(self, value): |
---|
32 | value.strip() |
---|
33 | if not _is_canonical(value): |
---|
34 | raise InvalidValue(value) |
---|
35 | return value |
---|
36 | |
---|
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 |
---|
44 | """ |
---|
45 | if self.context.hasProperty(self.prop): |
---|
46 | return self.context.getProperty(self.prop) |
---|
47 | |
---|
48 | return self.getDefault() |
---|
49 | |
---|
50 | def setProp(self, value): |
---|
51 | """ First validate value, than add/updater self.prop |
---|
52 | """ |
---|
53 | value = self._validate(value) |
---|
54 | |
---|
55 | if self.context.hasProperty(self.prop): |
---|
56 | self.context._updateProperty(self.prop, value) |
---|
57 | else: |
---|
58 | self.context._setProperty(self.prop, value, type="string") |
---|
59 | |
---|
60 | def delProp(self): |
---|
61 | """ Delete self.prop customization |
---|
62 | """ |
---|
63 | if self.context.hasProperty(self.prop): |
---|
64 | self.context.manage_delProperties(ids=[self.prop,]) |
---|
65 | |
---|
66 | |
---|
67 | class DefaultCanonicalPathAdapter(DefaultCanonicalAdapter): |
---|
68 | """Adapts base content to canonical path. |
---|
69 | """ |
---|
70 | implements(ICanonicalLink) |
---|
71 | |
---|
72 | prop = PROPERTY_PATH |
---|
73 | |
---|
74 | def getDefault(self): |
---|
75 | return '/'+'/'.join(self.purl.getRelativeContentPath(self.context)) |
---|
76 | |
---|
77 | canonical_path = property(getProp, setProp, delProp) |
---|
78 | |
---|
79 | |
---|
80 | class DefaultCanonicalLinkAdapter(DefaultCanonicalAdapter): |
---|
81 | """Adapts base content to canonical link. |
---|
82 | """ |
---|
83 | implements(ICanonicalLink) |
---|
84 | |
---|
85 | prop = PROPERTY_LINK |
---|
86 | |
---|
87 | def getDefault(self): |
---|
88 | return self.context.absolute_url() |
---|
89 | |
---|
90 | canonical_link = property(getProp, setProp, delProp) |
---|