[487] | 1 | from string import strip |
---|
| 2 | from time import time |
---|
| 3 | |
---|
[383] | 4 | from zope.interface import implements |
---|
| 5 | from zope.component import adapts |
---|
[487] | 6 | from plone.memoize import ram |
---|
[383] | 7 | |
---|
[487] | 8 | from Products.CMFCore.utils import getToolByName |
---|
| 9 | from Products.CMFPlone.interfaces import IPloneSiteRoot |
---|
[383] | 10 | |
---|
[487] | 11 | from interfaces import IDomainsMapExtractor |
---|
[383] | 12 | |
---|
| 13 | |
---|
[487] | 14 | def cache_key(*args, **kwargs): |
---|
| 15 | return time() // (60 * 60) |
---|
[383] | 16 | |
---|
[487] | 17 | |
---|
| 18 | class DomainsMapExtractor(object): |
---|
| 19 | implements(IDomainsMapExtractor) |
---|
| 20 | adapts(IPloneSiteRoot) |
---|
| 21 | |
---|
[383] | 22 | def __init__(self, context): |
---|
| 23 | self.context = context |
---|
| 24 | |
---|
[487] | 25 | def processData(self, value): |
---|
| 26 | try: |
---|
| 27 | pd_maps = map(lambda x: map(strip, x.split('::')), value) |
---|
| 28 | except: |
---|
| 29 | pd_maps = () |
---|
| 30 | else: |
---|
| 31 | pd_maps.sort(lambda x,y: len(y[0])-len(x[0])) |
---|
| 32 | return pd_maps |
---|
[384] | 33 | |
---|
[487] | 34 | |
---|
| 35 | @ram.cache(cache_key) |
---|
| 36 | def getDomainsMap(self): |
---|
| 37 | """ Return sorted by sybpath length tupple |
---|
| 38 | of (subpath, domain name) tuple. |
---|
| 39 | """ |
---|
| 40 | portal = getToolByName(self.context, 'portal_url').getPortalObject() |
---|
| 41 | pd_maps = portal.getProperty('path_domains_map', ()) |
---|
| 42 | return self.processData(pd_maps) |
---|
| 43 | |
---|
| 44 | def setDomainsMap(self, value): |
---|
| 45 | """ Set tupple of (subpath, domain name) tuples |
---|
| 46 | """ |
---|
| 47 | if value: |
---|
| 48 | prepared_value = self.processData(value) |
---|
| 49 | portal = getToolByName(self.context, 'portal_url').getPortalObject() |
---|
| 50 | |
---|
| 51 | if not portal.getPropertyType('path_domains_map') == 'lines': |
---|
| 52 | if not portal.hasPropertyType('path_domains_map'): |
---|
| 53 | portal.manage_addProperty('path_domains_map', [], 'lines') |
---|
| 54 | else: |
---|
| 55 | raise Exception("Wrong types of 'path_domains_map' " \ |
---|
| 56 | "portal property - should be list type") |
---|
| 57 | |
---|
| 58 | update_value = ['::'.join(l) for l in prepared_value] |
---|
| 59 | portal._updateProperty('path_domains_map', prepared_value) |
---|
| 60 | |
---|
| 61 | |
---|