Added count markers for each association type
authortflorac@xsup98-004.onf.fr
Thu, 12 Oct 2017 22:28:32 +0200
changeset 202 7d7947f69fce
parent 201 46819098e77a
child 203 6120de3dd9ec
Added count markers for each association type
src/pyams_content/component/extfile/zmi/__init__.py
--- a/src/pyams_content/component/extfile/zmi/__init__.py	Thu Oct 12 22:27:18 2017 +0200
+++ b/src/pyams_content/component/extfile/zmi/__init__.py	Thu Oct 12 22:28:32 2017 +0200
@@ -19,7 +19,8 @@
 from pyams_content.component.association.interfaces import IAssociationContainer
 from pyams_content.component.association.zmi.interfaces import IAssociationsView
 from pyams_content.component.extfile.interfaces import IExtFileContainerTarget, IBaseExtFile, \
-    IExtFile, IExtImage, IExtVideo, IExtAudio
+    IExtFile, IExtMedia, IExtImage, IExtVideo, IExtAudio
+from pyams_content.component.paragraph.zmi.interfaces import IParagraphContainerTable, IParagraphTitleToolbar
 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
 from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
 from pyams_skin.layer import IPyAMSLayer
@@ -28,6 +29,7 @@
 # import packages
 from pyams_content.component.association.zmi import AssociationItemAJAXAddForm, AssociationItemAJAXEditForm
 from pyams_content.component.extfile import EXTERNAL_FILES_FACTORIES
+from pyams_content.component.paragraph.zmi.container import ParagraphContainerCounterBase
 from pyams_form.security import ProtectedFormObjectMixin
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_skin.viewlet.toolbar import ToolbarMenuItem, ToolbarMenuDivider
@@ -41,10 +43,6 @@
 from pyams_content import _
 
 
-#
-# External file view
-#
-
 class IExtFileFactoryChooser(Interface):
     """External file factory chooser interface"""
 
@@ -60,6 +58,26 @@
     """External file add menu divider"""
 
 
+#
+# External file view
+#
+
+@viewlet_config(name='extfiles', context=IExtFileContainerTarget, layer=IPyAMSLayer, view=IParagraphContainerTable,
+                manager=IParagraphTitleToolbar, permission=VIEW_SYSTEM_PERMISSION, weight=20)
+class ExtFilesCounter(ParagraphContainerCounterBase):
+    """External files counter"""
+
+    weight = 20
+    action_class = 'action extfiles nowrap width-40'
+    icon_class = 'fa fa-fw fa-file-text-o'
+    icon_hint = _("External files")
+
+    @property
+    def count(self):
+        return len([file for file in IAssociationContainer(self.context).values()
+                    if IExtFile.providedBy(file) and not IExtMedia.providedBy(file)])
+
+
 @viewlet_config(name='add-extfile.menu', context=IExtFileContainerTarget, view=IAssociationsView,
                 layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=60)
 class ExtFileAddMenu(ProtectedFormObjectMixin, ToolbarMenuItem):
@@ -103,6 +121,20 @@
 class ExtFileAJAXAddForm(AssociationItemAJAXAddForm, ExtFileAddForm):
     """External file add form, JSON renderer"""
 
+    def get_ajax_output(self, changes):
+        output = super(ExtFileAJAXAddForm, self).get_ajax_output(changes)
+        if output:
+            counter = ExtFilesCounter(self.context, self.request, self, None)
+            counter.update()
+            output.setdefault('events', []).append({
+                'event': 'PyAMS_content.changed_item',
+                'options': {'handler': 'PyAMS_content.paragraphs.updateMarkers',
+                            'object_name': self.context.__name__,
+                            'marker_type': 'extfiles',
+                            'marker_tag': counter.render()}
+            })
+        return output
+
 
 @pagelet_config(name='properties.html', context=IExtFile, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 class ExtFilePropertiesEditForm(AdminDialogEditForm):
@@ -140,6 +172,22 @@
 # Images views
 #
 
+@viewlet_config(name='extimages', context=IExtFileContainerTarget, layer=IPyAMSLayer, view=IParagraphContainerTable,
+                manager=IParagraphTitleToolbar, permission=VIEW_SYSTEM_PERMISSION, weight=21)
+class ExtImagesCounter(ParagraphContainerCounterBase):
+    """External image files counter"""
+
+    weight = 21
+    action_class = 'action extimages nowrap width-40'
+    icon_class = 'fa fa-fw fa-file-image-o'
+    icon_hint = _("Images")
+
+    @property
+    def count(self):
+        return len([file for file in IAssociationContainer(self.context).values()
+                    if IExtImage.providedBy(file)])
+
+
 @viewlet_config(name='add-extimage.menu', context=IExtFileContainerTarget, view=IAssociationsView,
                 layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=61)
 class ExtImageAddMenu(ProtectedFormObjectMixin, ToolbarMenuItem):
@@ -175,6 +223,20 @@
 class ExtImageAJAXAddForm(AssociationItemAJAXAddForm, ExtImageAddForm):
     """External image add form, JSON renderer"""
 
+    def get_ajax_output(self, changes):
+        output = super(ExtImageAJAXAddForm, self).get_ajax_output(changes)
+        if output:
+            counter = ExtImagesCounter(self.context, self.request, self, None)
+            counter.update()
+            output.setdefault('events', []).append({
+                'event': 'PyAMS_content.changed_item',
+                'options': {'handler': 'PyAMS_content.paragraphs.updateMarkers',
+                            'object_name': self.context.__name__,
+                            'marker_type': 'extimages',
+                            'marker_tag': counter.render()}
+            })
+        return output
+
 
 @pagelet_config(name='properties.html', context=IExtImage, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 class ExtImagePropertiesEditForm(ExtFilePropertiesEditForm):
@@ -205,6 +267,22 @@
 # Videos views
 #
 
+@viewlet_config(name='extvideos', context=IExtFileContainerTarget, layer=IPyAMSLayer, view=IParagraphContainerTable,
+                manager=IParagraphTitleToolbar, permission=VIEW_SYSTEM_PERMISSION, weight=22)
+class ExtVideosCounter(ParagraphContainerCounterBase):
+    """External video files counter"""
+
+    weight = 22
+    action_class = 'action extvideos nowrap width-40'
+    icon_class = 'fa fa-fw fa-file-video-o'
+    icon_hint = _("Videos")
+
+    @property
+    def count(self):
+        return len([file for file in IAssociationContainer(self.context).values()
+                    if IExtVideo.providedBy(file)])
+
+
 @viewlet_config(name='add-extvideo.menu', context=IExtFileContainerTarget, view=IAssociationsView,
                 layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=62)
 class ExtVideoAddMenu(ProtectedFormObjectMixin, ToolbarMenuItem):
@@ -239,6 +317,20 @@
 class ExtVideoAJAXAddForm(AssociationItemAJAXAddForm, ExtVideoAddForm):
     """External video add form, JSON renderer"""
 
+    def get_ajax_output(self, changes):
+        output = super(ExtVideoAJAXAddForm, self).get_ajax_output(changes)
+        if output:
+            counter = ExtVideosCounter(self.context, self.request, self, None)
+            counter.update()
+            output.setdefault('events', []).append({
+                'event': 'PyAMS_content.changed_item',
+                'options': {'handler': 'PyAMS_content.paragraphs.updateMarkers',
+                            'object_name': self.context.__name__,
+                            'marker_type': 'extvideos',
+                            'marker_tag': counter.render()}
+            })
+        return output
+
 
 @pagelet_config(name='properties.html', context=IExtVideo, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 class ExtVideoPropertiesEditForm(ExtFilePropertiesEditForm):
@@ -268,6 +360,22 @@
 # Audio file views
 #
 
+@viewlet_config(name='extaudios', context=IExtFileContainerTarget, layer=IPyAMSLayer, view=IParagraphContainerTable,
+                manager=IParagraphTitleToolbar, permission=VIEW_SYSTEM_PERMISSION, weight=23)
+class ExtAudiosCounter(ParagraphContainerCounterBase):
+    """External audio files counter"""
+
+    weight = 23
+    action_class = 'action extaudios nowrap width-40'
+    icon_class = 'fa fa-fw fa-file-audio-o'
+    icon_hint = _("Audios files")
+
+    @property
+    def count(self):
+        return len([file for file in IAssociationContainer(self.context).values()
+                    if IExtAudio.providedBy(file)])
+
+
 @viewlet_config(name='add-extaudio.menu', context=IExtFileContainerTarget, view=IAssociationsView,
                 layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=63)
 class ExtAudioAddMenu(ProtectedFormObjectMixin, ToolbarMenuItem):
@@ -302,6 +410,20 @@
 class ExtAudioAJAXAddForm(AssociationItemAJAXAddForm, ExtAudioAddForm):
     """External audio file add form, JSON renderer"""
 
+    def get_ajax_output(self, changes):
+        output = super(ExtAudioAJAXAddForm, self).get_ajax_output(changes)
+        if output:
+            counter = ExtAudiosCounter(self.context, self.request, self, None)
+            counter.update()
+            output.setdefault('events', []).append({
+                'event': 'PyAMS_content.changed_item',
+                'options': {'handler': 'PyAMS_content.paragraphs.updateMarkers',
+                            'object_name': self.context.__name__,
+                            'marker_type': 'extaudios',
+                            'marker_tag': counter.render()}
+            })
+        return output
+
 
 @pagelet_config(name='properties.html', context=IExtAudio, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
 class ExtAudioPropertiesEditForm(ExtFilePropertiesEditForm):
@@ -310,7 +432,7 @@
     legend = _("Update audio file properties")
     icon_css_class = 'fa fa-fw fa-file-audio-o'
 
-    fields = field.Fields(IExtVideo).select('title', 'description', 'author', 'language', 'data', 'filename')
+    fields = field.Fields(IExtAudio).select('title', 'description', 'author', 'language', 'data', 'filename')
 
 
 @view_config(name='properties.json', context=IExtAudio, request_type=IPyAMSLayer,