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