Use 'associations' to handle links and external files
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Sep 2017 14:55:53 +0200
changeset 20 ea4bfc8f6278
parent 19 548b72a430c7
child 21 ec814fadbd27
Use 'associations' to handle links and external files
src/pyams_content_es/component/extfile.py
src/pyams_content_es/component/gallery.py
src/pyams_content_es/component/paragraph.py
--- a/src/pyams_content_es/component/extfile.py	Mon Sep 11 14:55:31 2017 +0200
+++ b/src/pyams_content_es/component/extfile.py	Mon Sep 11 14:55:53 2017 +0200
@@ -17,35 +17,41 @@
 import base64
 
 # import interfaces
-from pyams_content.component.extfile.interfaces import IExtFileContainerTarget, IExtFileContainer
+from pyams_content.component.association.interfaces import IAssociationContainer
+from pyams_content.component.extfile.interfaces import IBaseExtFile
+from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IParagraphContainer
 from pyams_content_es.interfaces import IDocumentIndexInfo
 
 # import packages
 from pyams_utils.adapter import adapter_config
 
 
-@adapter_config(name='extfile', context=IExtFileContainerTarget, provides=IDocumentIndexInfo)
-def ExtFileContainerTargetIndexInfo(content):
+@adapter_config(name='extfile', context=IParagraphContainerTarget, provides=IDocumentIndexInfo)
+def ParagraphContainerTargetExtFileIndexInfo(context):
     """External files index info"""
     extfiles = []
     attachments = []
-    for extfile in IExtFileContainer(content).values():
-        extfiles.append({'title': extfile.title,
-                         'description': extfile.description})
-        for lang, data in extfile.data.items():
-            content_type = data.content_type
-            if isinstance(content_type, bytes):
-                content_type = content_type.decode()
-            if content_type.startswith('image/') or \
-               content_type.startswith('audio/') or \
-               content_type.startswith('video/'):
+    for paragraph in IParagraphContainer(context).values():
+        associations = IAssociationContainer(paragraph, {})
+        for extfile in associations.values():
+            if not IBaseExtFile.providedBy(extfile):
                 continue
-            attachments.append({
-                'content_type': content_type,
-                'name': data.filename,
-                'language': lang,
-                'content': base64.encodebytes(data.data).decode().replace('\n', '')
-            })
+            extfiles.append({'title': extfile.title,
+                             'description': extfile.description})
+            for lang, data in extfile.data.items():
+                content_type = data.content_type
+                if isinstance(content_type, bytes):
+                    content_type = content_type.decode()
+                if content_type.startswith('image/') or \
+                   content_type.startswith('audio/') or \
+                   content_type.startswith('video/'):
+                    continue
+                attachments.append({
+                    'content_type': content_type,
+                    'name': data.filename,
+                    'language': lang,
+                    'content': base64.encodebytes(data.data).decode().replace('\n', '')
+                })
     result = {'extfile': extfiles}
     if attachments:
         result.update({
--- a/src/pyams_content_es/component/gallery.py	Mon Sep 11 14:55:31 2017 +0200
+++ b/src/pyams_content_es/component/gallery.py	Mon Sep 11 14:55:53 2017 +0200
@@ -16,8 +16,7 @@
 # import standard library
 
 # import interfaces
-from pyams_content.component.gallery.interfaces import IGalleryContainerTarget, IGalleryContainer, IGallery, \
-    IGalleryFileInfo
+from pyams_content.component.gallery.interfaces import IGallery, IGalleryTarget
 from pyams_content_es.interfaces import IDocumentIndexInfo
 
 # import packages
@@ -37,34 +36,32 @@
                                               info=description)
             info[lang] = new_info
     for image in gallery.values():
-        image_info = IGalleryFileInfo(image, None)
-        if image_info is not None:
-            for lang, title in (image_info.title or {}).items():
-                if title:
-                    new_info = '{old}\n{info}'.format(old=info.get(lang, ''),
-                                                      info=title)
-                    info[lang] = new_info
-            for lang, description in (image_info.description or {}).items():
-                if description:
-                    new_info = '{old}\n{info}'.format(old=info.get(lang, ''),
-                                                      info=description)
-                    info[lang] = new_info
-            for lang, comments in (image_info.author_comments or {}).items():
-                if comments:
-                    new_info = '{old}\n{info}'.format(old=info.get(lang, ''),
-                                                      info=comments)
-                    info[lang] = new_info
+        for lang, title in (image.title or {}).items():
+            if title:
+                new_info = '{old}\n{info}'.format(old=info.get(lang, ''),
+                                                  info=title)
+                info[lang] = new_info
+        for lang, description in (image.description or {}).items():
+            if description:
+                new_info = '{old}\n{info}'.format(old=info.get(lang, ''),
+                                                  info=description)
+                info[lang] = new_info
+        for lang, comments in (image.author_comments or {}).items():
+            if comments:
+                new_info = '{old}\n{info}'.format(old=info.get(lang, ''),
+                                                  info=comments)
+                info[lang] = new_info
     return info
 
 
-@adapter_config(name='gallery', context=IGalleryContainerTarget, provides=IDocumentIndexInfo)
-def GalleryContainerTargetIndexInfo(content):
+@adapter_config(name='gallery', context=IGalleryTarget, provides=IDocumentIndexInfo)
+def GalleryTargetIndexInfo(content):
     """Gallery container index info"""
     body = {}
-    for gallery in IGalleryContainer(content).values():
-        info = IDocumentIndexInfo(gallery, None)
-        if info is not None:
-            for lang, info_body in info.items():
-                body[lang] = '{old}\n{body}'.format(old=body.get(lang, ''),
-                                                    body=info_body)
+    gallery = IGallery(content)
+    info = IDocumentIndexInfo(gallery, None)
+    if info is not None:
+        for lang, info_body in info.items():
+            body[lang] = '{old}\n{body}'.format(old=body.get(lang, ''),
+                                                body=info_body)
     return {'gallery': body}
--- a/src/pyams_content_es/component/paragraph.py	Mon Sep 11 14:55:31 2017 +0200
+++ b/src/pyams_content_es/component/paragraph.py	Mon Sep 11 14:55:53 2017 +0200
@@ -16,8 +16,10 @@
 # import standard library
 
 # import interfaces
-from pyams_content.component.paragraph.interfaces import IParagraphContainer, IParagraphContainerTarget, IHTMLParagraph, \
-    IIllustrationParagraph
+from pyams_content.component.illustration.interfaces import IIllustrationParagraph
+from pyams_content.component.paragraph.interfaces import IParagraphContainer, IParagraphContainerTarget
+from pyams_content.component.paragraph.interfaces.header import IHeaderParagraph
+from pyams_content.component.paragraph.interfaces.html import IHTMLParagraph
 from pyams_content_es.interfaces import IDocumentIndexInfo
 
 # import packages
@@ -25,6 +27,16 @@
 from pyams_utils.html import html_to_text
 
 
+@adapter_config(context=IHeaderParagraph, provides=IDocumentIndexInfo)
+def HeaderParagraphIndexInfo(paragraph):
+    """Header paragraph index info"""
+    info = {}
+    for lang, header in paragraph.header.items():
+        if header:
+            info.setdefault(lang, header)
+    return info
+
+
 @adapter_config(context=IHTMLParagraph, provides=IDocumentIndexInfo)
 def HTMLParagraphIndexInfo(paragraph):
     """HTML paragraph index info"""
@@ -47,7 +59,7 @@
     for lang, title in paragraph.title.items():
         if title:
             info.setdefault(lang, title)
-    for lang, legend in paragraph.legend.items():
+    for lang, legend in paragraph.alt_title.items():
         if legend:
             new_legend = '{old}\n{legend}'.format(old=info.get(lang, ''),
                                                   legend=legend)