src/pyams_portal/template.py
changeset 279 c025abc00397
parent 275 36ff0ccd4253
child 289 fca4100c1733
--- a/src/pyams_portal/template.py	Mon Nov 23 17:20:28 2020 +0100
+++ b/src/pyams_portal/template.py	Tue Nov 24 10:43:30 2020 +0100
@@ -10,33 +10,35 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
-
 from persistent import Persistent
 from persistent.list import PersistentList
 from persistent.mapping import PersistentMapping
 from pyramid.events import subscriber
 from pyramid.threadlocal import get_current_registry
-from zope.componentvocabulary.vocabulary import UtilityVocabulary
 from zope.container.contained import Contained
 from zope.container.folder import Folder
 from zope.interface import implementer
-from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectRemovedEvent
+from zope.intid.interfaces import IIntIdAddedEvent, IIntIdRemovedEvent
 from zope.location import locate
 from zope.schema.fieldproperty import FieldProperty
 from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 from zope.traversing.interfaces import ITraversable
 
-from pyams_portal.interfaces import IPortalPortletsConfiguration, IPortalTemplate, IPortalTemplateConfiguration, \
-    IPortalTemplateContainer, IPortalTemplateContainerConfiguration, IPortlet, IPortletConfiguration, \
-    PORTLETS_CONFIGURATION_KEY, TEMPLATE_CONFIGURATION_KEY, TEMPLATE_CONTAINER_CONFIGURATION_KEY
+from pyams_portal.interfaces import IPortalPortletsConfiguration, IPortalTemplate, \
+    IPortalTemplateConfiguration, IPortalTemplateContainer, IPortalTemplateContainerConfiguration, \
+    IPortlet, IPortletConfiguration, PORTLETS_CONFIGURATION_KEY, TEMPLATE_CONFIGURATION_KEY, \
+    TEMPLATE_CONTAINER_CONFIGURATION_KEY
 from pyams_portal.slot import SlotConfiguration
 from pyams_utils.adapter import ContextAdapter, adapter_config, get_annotation_adapter
 from pyams_utils.factory import factory_config
-from pyams_utils.registry import get_local_registry, get_utility
+from pyams_utils.interfaces.intids import IUniqueID
+from pyams_utils.registry import get_local_registry, get_utilities_for, get_utility
 from pyams_utils.request import check_request
 from pyams_utils.vocabulary import vocabulary_config
 
+
+__docformat__ = 'restructuredtext'
+
 from pyams_portal import _
 
 
@@ -65,7 +67,8 @@
 @adapter_config(context=IPortalTemplateContainer, provides=IPortalTemplateContainerConfiguration)
 def portal_template_container_configuration_adapter(context):
     """Portal template container configuration factory"""
-    return get_annotation_adapter(context, TEMPLATE_CONTAINER_CONFIGURATION_KEY, IPortalTemplateContainerConfiguration)
+    return get_annotation_adapter(context, TEMPLATE_CONTAINER_CONFIGURATION_KEY,
+                                  IPortalTemplateContainerConfiguration)
 
 
 #
@@ -81,28 +84,35 @@
     content_name = _("Portal template")
 
 
-@subscriber(IObjectAddedEvent, context_selector=IPortalTemplate)
+@subscriber(IIntIdAddedEvent, context_selector=IPortalTemplate)
 def handle_added_template(event):
     """Register shared template"""
+    template = event.object
     registry = get_local_registry()
-    if (registry is not None) and IPortalTemplateContainer.providedBy(event.newParent):
-        registry.registerUtility(event.object, IPortalTemplate, name=event.object.name)
+    if (registry is not None) and IPortalTemplateContainer.providedBy(template.__parent__):
+        registry.registerUtility(template, IPortalTemplate,
+                                 name=IUniqueID(template).oid)
 
 
-@subscriber(IObjectRemovedEvent, context_selector=IPortalTemplate)
+@subscriber(IIntIdRemovedEvent, context_selector=IPortalTemplate)
 def handle_removed_template(event):
     """Unregister removed template"""
+    template = event.object
     registry = get_local_registry()
-    if (registry is not None) and IPortalTemplateContainer.providedBy(event.oldParent):
-        registry.unregisterUtility(event.object, IPortalTemplate, name=event.object.name)
+    if (registry is not None) and IPortalTemplateContainer.providedBy(template.__parent__):
+        registry.unregisterUtility(template, IPortalTemplate,
+                                   name=IUniqueID(template).oid)
 
 
 @vocabulary_config(name='PyAMS portal templates')
-class PortalTemplatesVocabulary(UtilityVocabulary):
+class PortalTemplatesVocabulary(SimpleVocabulary):
     """Portal templates vocabulary"""
 
-    interface = IPortalTemplate
-    nameOnly = True
+    def __init__(self, context):
+        terms = sorted([SimpleTerm(name, title=util.name)
+                        for (name, util) in get_utilities_for(IPortalTemplate)],
+                       key=lambda x: x.title)
+        super(PortalTemplatesVocabulary, self).__init__(terms)
 
 
 #