Added generic renderers vocabulary
authorThierry Florac <thierry.florac@onf.fr>
Thu, 24 May 2018 10:51:45 +0200
changeset 554 120ce09ade94
parent 553 781e3958f6bf
child 555 8e8a14452567
Added generic renderers vocabulary
src/pyams_content/features/renderer/__init__.py
--- a/src/pyams_content/features/renderer/__init__.py	Wed May 23 14:56:48 2018 +0200
+++ b/src/pyams_content/features/renderer/__init__.py	Thu May 24 10:51:45 2018 +0200
@@ -22,6 +22,7 @@
 from pyams_utils.adapter import adapter_config
 from pyams_utils.request import check_request
 from zope.interface import implementer
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
 
 @implementer(IRenderedContent)
@@ -34,7 +35,7 @@
     def get_renderer(self, request=None):
         if request is None:
             request = check_request()
-        return request.registry.queryMultiAdapter((self, request), self.renderer_interface, name=self.renderer)
+        return request.registry.queryMultiAdapter((self, request), self.renderer_interface, name=self.renderer or '')
 
 
 @adapter_config(context=IRenderedContent, provides=IContentRenderer)
@@ -50,3 +51,21 @@
     if renderer.settings_interface is None:
         return None
     return renderer.settings_interface(context)
+
+
+class RenderersVocabulary(SimpleVocabulary):
+    """Renderers vocabulary"""
+
+    content_interface = IRenderedContent
+    content_factory = RenderedContentMixin
+
+    def __init__(self, context=None):
+        request = check_request()
+        translate = request.localizer.translate
+        registry = request.registry
+        if not self.content_interface.providedBy(context):
+            context = self.content_factory()
+        terms = [SimpleTerm(name, title=translate(adapter.label))
+                 for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+                                             key=lambda x: x[1].weight)]
+        super(RenderersVocabulary, self).__init__(terms)