Added method on paragraphs container to select paragraphs
authorThierry Florac <thierry.florac@onf.fr>
Mon, 03 Sep 2018 13:40:59 +0200
changeset 913 5481b19fd47c
parent 912 f0716419669f
child 914 76f637363cfd
Added method on paragraphs container to select paragraphs
src/pyams_content/component/paragraph/container.py
src/pyams_content/component/paragraph/interfaces/__init__.py
--- a/src/pyams_content/component/paragraph/container.py	Mon Sep 03 12:21:04 2018 +0200
+++ b/src/pyams_content/component/paragraph/container.py	Mon Sep 03 13:40:59 2018 +0200
@@ -42,6 +42,18 @@
         self[key] = value
         self.last_id += 1
 
+    def get_visible_paragraphs(self, anchors_only=False, factories=None):
+        for paragraph in self.values():
+            if not paragraph.visible:
+                continue
+            if anchors_only and not paragraph.anchor:
+                continue
+            if factories:
+                has_factory = tuple(filter(lambda x: x.content_type == paragraph.__class__))
+                if not has_factory:
+                    continue
+            yield paragraph
+
 
 @adapter_config(context=IParagraphContainerTarget, provides=IParagraphContainer)
 def paragraph_container_factory(target):
--- a/src/pyams_content/component/paragraph/interfaces/__init__.py	Mon Sep 03 12:21:04 2018 +0200
+++ b/src/pyams_content/component/paragraph/interfaces/__init__.py	Mon Sep 03 13:40:59 2018 +0200
@@ -66,11 +66,17 @@
     def append(self, value):
         """Add given value to container"""
 
+    def get_visible_paragraphs(self, anchors_only=False, factories=None):
+        """Get visible paragraphs matching given arguments"""
+
 
 class IParagraphContainerTarget(IAttributeAnnotatable):
     """Paragraphs container marker interface"""
 
 
+PARAGRAPH_FACTORIES_VOCABULARY = 'PyAMS paragraph factories'
+
+
 class IParagraphFactory(Interface):
     """Paragraph factory utility interface"""
 
@@ -93,7 +99,7 @@
     auto_created_paragraphs = List(title=_("Default paragraphs"),
                                    description=_("List of paragraphs automatically added to a new content"),
                                    required=False,
-                                   value_type=Choice(vocabulary='PyAMS paragraph factories'))
+                                   value_type=Choice(vocabulary=PARAGRAPH_FACTORIES_VOCABULARY))
 
 
 class IParagraphRenderer(IContentProvider):