Back to index

plone3  3.1.7
storage.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import adapts
00003 from zope.component import getUtility
00004 
00005 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
00006 from zope.publisher.interfaces.browser import IBrowserPublisher
00007 
00008 from zope.app.container.interfaces import INameChooser
00009 from zope.app.container.contained import NameChooser
00010 
00011 from zope.app.container.traversal import ItemTraverser
00012 
00013 from Acquisition import aq_base
00014 from OFS.SimpleItem import SimpleItem
00015 
00016 from plone.i18n.normalizer.interfaces import IIDNormalizer
00017 
00018 from plone.portlets.interfaces import IPortletAssignmentMapping
00019 from plone.portlets import constants
00020 from plone.portlets.storage import PortletAssignmentMapping as BaseMapping
00021 
00022 from plone.app.portlets.interfaces import IUserPortletAssignmentMapping
00023 
00024 ATTEMPTS = 10000
00025 
00026 category_to_name = {
00027     constants.CONTEXT_CATEGORY      : 'contextportlets',
00028     constants.USER_CATEGORY         : 'dashboard',
00029     constants.GROUP_CATEGORY        : 'groupportlets',
00030     constants.CONTENT_TYPE_CATEGORY : 'contenttypeportlets',
00031 }
00032 
00033 class PortletAssignmentMapping(BaseMapping, SimpleItem):
00034     """A Zope 2 version of the default assignment mapping storage.
00035     """
00036     
00037     @property
00038     def id(self):
00039         manager = self.__manager__
00040         category = self.__category__
00041         key = self.__name__
00042         
00043         prefix = category_to_name.get(category, category)
00044         suffix = manager
00045         
00046         if category != constants.CONTEXT_CATEGORY and key:
00047             suffix = "%s+%s" % (manager, key)
00048         
00049         return "++%s++%s" % (prefix, suffix)
00050     
00051     def __setitem__(self, key, assignment):
00052         BaseMapping.__setitem__(self, key, aq_base(assignment))
00053 
00054 class UserPortletAssignmentMapping(PortletAssignmentMapping):
00055     """An assignment mapping for user/dashboard portlets
00056     """
00057 
00058     implements(IUserPortletAssignmentMapping)
00059         
00060 class PortletAssignmentMappingTraverser(ItemTraverser):
00061     """A traverser for portlet assignment mappings, that is acqusition-aware
00062     """
00063     implements(IBrowserPublisher)
00064     adapts(IPortletAssignmentMapping, IDefaultBrowserLayer)
00065     
00066     def publishTraverse(self, request, name):
00067         ob = ItemTraverser.publishTraverse(self, request, name)
00068         return ob.__of__(self.context)
00069 
00070 class PortletsNameChooser(NameChooser):
00071     """A name chooser for portlets
00072     """
00073     
00074     implements(INameChooser)
00075     
00076     def __init__(self, context):
00077         self.context = context
00078 
00079     def chooseName(self, name, object):
00080         """Choose a name based on a the portlet title
00081         
00082         >>> from plone.app.portlets.storage import PortletAssignmentMapping
00083         >>> mapping = PortletAssignmentMapping()
00084         
00085         >>> from zope.app.container.interfaces import INameChooser
00086         >>> chooser = INameChooser(mapping)
00087         
00088         >>> from plone.app.portlets.portlets import base
00089         >>> class DummyAssignment(base.Assignment):
00090         ...     title = u""
00091         
00092         >>> dummy = DummyAssignment()
00093         >>> dummy.title = u"A test title"
00094         
00095         >>> chooser.chooseName(None, dummy)
00096         'a-test-title'
00097         
00098         >>> chooser.chooseName(None, dummy)
00099         'a-test-title'
00100         
00101         >>> mapping[u'a-test-title'] = dummy
00102         >>> chooser.chooseName(None, dummy)
00103         'a-test-title-1'
00104         
00105         >>> dummy.title = 'RSS: http://plone.org'
00106         >>> chooser.chooseName(None, dummy)
00107         'RSS-http-plone.org'
00108         
00109         >>> dummy.title = None
00110         >>> chooser.chooseName(None, dummy)
00111         'dummyassignment'
00112         
00113         >>> mapping[u'dummyassignment'] = dummy
00114         >>> delattr(dummy, 'title')
00115         >>> chooser.chooseName(None, dummy)
00116         'dummyassignment-1'
00117         
00118         
00119         """
00120         container = self.context
00121 
00122         if not name:
00123             name = getattr(object, 'title', None)
00124 
00125         if not name:
00126             name = object.__class__.__name__
00127             
00128         name = getUtility(IIDNormalizer).normalize(name)
00129 
00130         i = 0
00131         new_name = name
00132         while new_name in container and i <= ATTEMPTS:
00133             i += 1
00134             new_name = "%s-%d" % (name, i)
00135             
00136         self.checkName(new_name, object)
00137         return new_name