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