Updated Pictograms sorted by local title dev-dc
authorDamien Correia
Thu, 11 Oct 2018 16:25:04 +0200
branchdev-dc
changeset 1013 d69762f1ca4d
parent 1012 d77417cf9e67
child 1014 6cdae82405f7
child 1078 e608aecc2e60
Updated Pictograms sorted by local title
src/pyams_content/reference/pictograms/zmi/manager.py
--- a/src/pyams_content/reference/pictograms/zmi/manager.py	Thu Oct 11 16:18:06 2018 +0200
+++ b/src/pyams_content/reference/pictograms/zmi/manager.py	Thu Oct 11 16:25:04 2018 +0200
@@ -15,11 +15,13 @@
 
 # import standard library
 import json
+import locale
 
 # 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_i18n.interfaces import II18n
 from pyams_skin.interfaces import IInnerPage
 from pyams_skin.layer import IPyAMSLayer
 from pyams_utils.interfaces.data import IObjectData
@@ -85,24 +87,30 @@
 class PictogramManagerSelectionView(Viewlet):
     """Pictogram manager selection viewlet"""
 
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.table = query_utility(IPictogramTable)
+        self.target = get_parent(self.context, IPictogramManagerTarget)
+        if self.target is not None:
+            self.picto_manager = IPictogramManager(self.target)
+
+    def sort_pictogram(self, pictograms):
+        return sorted(pictograms,
+                     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):
+        if self.picto_manager:
+            for pictogram in self.sort_pictogram(self.table.values()):
+                if pictogram.__name__ not in self.picto_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
+        if self.picto_manager.selected_pictograms:
+            for name in self.picto_manager.selected_pictograms:
+                pictogram = self.table.get(name)
+                if pictogram is not None:
+                    yield pictogram