Sort pictograms in manager settings form
authorThierry Florac <thierry.florac@onf.fr>
Thu, 11 Oct 2018 17:16:15 +0200
changeset 1014 6cdae82405f7
parent 1011 f82a78ce9963 (current diff)
parent 1013 d69762f1ca4d (diff)
child 1015 ac47f95d7bd6
child 1078 e608aecc2e60
Sort pictograms in manager settings form
src/pyams_content/reference/pictograms/zmi/manager.py
--- a/src/pyams_content/reference/pictograms/zmi/manager.py	Wed Oct 10 17:29:44 2018 +0200
+++ b/src/pyams_content/reference/pictograms/zmi/manager.py	Thu Oct 11 17:16:15 2018 +0200
@@ -12,33 +12,31 @@
 
 __docformat__ = 'restructuredtext'
 
+import json
+import locale
 
-# import standard library
-import json
+from z3c.form import button, field
+from z3c.form.util import changedField
+from zope.interface import Interface, implementer
 
-# import interfaces
 from pyams_content.interfaces import MANAGE_TOOL_PERMISSION
-from pyams_content.reference.pictograms.interfaces import IPictogramManagerTarget, IPictogramManager, IPictogramTable
-from pyams_form.interfaces.form import IWidgetForm, IWidgetsSuffixViewletsManager, IUncheckedEditFormButtons
+from pyams_content.reference.pictograms.interfaces import IPictogramManager, IPictogramManagerTarget, IPictogramTable
+from pyams_form.form import ajax_config
+from pyams_form.interfaces.form import IUncheckedEditFormButtons, IWidgetForm, IWidgetsSuffixViewletsManager
+from pyams_i18n.interfaces import II18n
+from pyams_pagelet.pagelet import pagelet_config
 from pyams_skin.interfaces import IInnerPage
 from pyams_skin.layer import IPyAMSLayer
+from pyams_skin.viewlet.menu import MenuItem
+from pyams_template.template import template_config
 from pyams_utils.interfaces.data import IObjectData
+from pyams_utils.registry import query_utility
+from pyams_utils.traversing import get_parent
+from pyams_viewlet.viewlet import Viewlet, viewlet_config
+from pyams_zmi.form import AdminEditForm
 from pyams_zmi.interfaces.menu import IPropertiesMenu
 from pyams_zmi.layer import IAdminLayer
 
-# import packages
-from pyams_form.form import ajax_config
-from pyams_pagelet.pagelet import pagelet_config
-from pyams_skin.viewlet.menu import MenuItem
-from pyams_template.template import template_config
-from pyams_utils.registry import query_utility
-from pyams_utils.traversing import get_parent
-from pyams_viewlet.viewlet import viewlet_config, Viewlet
-from pyams_zmi.form import AdminEditForm
-from z3c.form import field, button
-from z3c.form.util import changedField
-from zope.interface import implementer, Interface
-
 from pyams_content import _
 
 
@@ -85,24 +83,35 @@
 class PictogramManagerSelectionView(Viewlet):
     """Pictogram manager selection viewlet"""
 
+    pictogram_table = None
+    pictogram_manager = None
+
+    def update(self):
+        super(PictogramManagerSelectionView, self).update()
+        self.pictogram_table = query_utility(IPictogramTable)
+        target = get_parent(self.context, IPictogramManagerTarget)
+        if target is not None:
+            self.pictogram_manager = IPictogramManager(target)
+
+    @property
+    def sorted_pictograms(self):
+        yield from sorted(self.pictogram_table.values(),
+                          key=lambda x: locale.strxfrm(II18n(x).query_attribute('title',
+                                                                                request=self.request) or '').lower())
+
     @property
     def available_pictograms(self):
-        table = query_utility(IPictogramTable)
-        target = get_parent(self.context, IPictogramManagerTarget)
-        if target is not None:
-            manager = IPictogramManager(target)
-            for pictogram in table.values():
-                if (not manager.selected_pictograms) or (pictogram.__name__ not in manager.selected_pictograms):
+        manager = self.pictogram_manager
+        if manager is not None:
+            for pictogram in self.sorted_pictograms:
+                if pictogram.__name__ not in manager.selected_pictograms:
                     yield pictogram
 
     @property
     def selected_pictograms(self):
-        table = query_utility(IPictogramTable)
-        target = get_parent(self.context, IPictogramManagerTarget)
-        if target is not None:
-            manager = IPictogramManager(target)
-            if manager.selected_pictograms:
-                for name in manager.selected_pictograms:
-                    pictogram = table.get(name)
-                    if pictogram is not None:
-                        yield pictogram
+        manager = self.pictogram_manager
+        if manager is not None:
+            for name in (manager.selected_pictograms or ()):
+                pictogram = self.pictogram_table.get(name)
+                if pictogram is not None:
+                    yield pictogram