Code optimization
authorThierry Florac <thierry.florac@onf.fr>
Fri, 25 May 2018 16:56:57 +0200
changeset 559 5ddb2d0bef4b
parent 558 d9c6b1d7fefa
child 560 aa0c77dfa529
child 653 238b75a21396
Code optimization
src/pyams_content/skin/zmi/viewlet/toplinks/__init__.py
--- a/src/pyams_content/skin/zmi/viewlet/toplinks/__init__.py	Fri May 25 15:48:59 2018 +0200
+++ b/src/pyams_content/skin/zmi/viewlet/toplinks/__init__.py	Fri May 25 16:56:57 2018 +0200
@@ -30,10 +30,9 @@
 from pyams_skin.viewlet.toplinks import TopLinksViewlet, TopLinksMenu
 from pyams_template.template import template_config
 from pyams_utils.list import unique
-from pyams_utils.registry import get_local_registry, get_utility
+from pyams_utils.registry import get_utility, get_all_utilities_registered_for
 from pyams_utils.url import absolute_url
 from pyams_viewlet.viewlet import viewlet_config
-from pyramid.threadlocal import get_current_registry
 
 from pyams_content import _
 
@@ -48,12 +47,14 @@
 
     def update(self):
         super(SharedSitesMenu, self).update()
-        registry = get_local_registry()
-        for site in sorted(registry.getAllUtilitiesRegisteredFor(ISharedSite),
-                           key=lambda x: II18n(x).query_attribute('title', request=self.request) or ''):
-            menu = TopLinksMenu(self.context, self.request, self.__parent__, self)
-            menu.label = II18n(site).query_attribute('title', request=self.request) or site.__name__
-            menu.url = absolute_url(site, self.request, 'admin#dashboard.html')
+        context = self.context
+        request = self.request
+        parent = self.__parent__
+        for site in sorted(get_all_utilities_registered_for(ISharedSite),
+                           key=lambda x: II18n(x).query_attribute('title', request=request) or ''):
+            menu = TopLinksMenu(context, request, parent, self)
+            menu.label = II18n(site).query_attribute('title', request=request) or site.__name__
+            menu.url = absolute_url(site, request, 'admin#dashboard.html')
             self.viewlets.append(menu)
 
 
@@ -67,14 +68,16 @@
 
     def update(self):
         super(SharedContentsMenu, self).update()
-        registry = get_local_registry()
-        for tool in sorted(registry.getAllUtilitiesRegisteredFor(IBaseSharedTool),
-                           key=lambda x: II18n(x).query_attribute('title', request=self.request) or ''):
+        context = self.context
+        request = self.request
+        parent = self.__parent__
+        for tool in sorted(get_all_utilities_registered_for(IBaseSharedTool),
+                           key=lambda x: II18n(x).query_attribute('title', request=request) or ''):
             if ISharedSite.providedBy(tool) or (not tool.shared_content_menu):
                 continue
-            menu = TopLinksMenu(self.context, self.request, self.__parent__, self)
-            menu.label = II18n(tool).query_attribute('title', request=self.request) or tool.__name__
-            menu.url = absolute_url(tool, self.request, 'admin#dashboard.html')
+            menu = TopLinksMenu(context, request, parent, self)
+            menu.label = II18n(tool).query_attribute('title', request=request) or tool.__name__
+            menu.url = absolute_url(tool, request, 'admin#dashboard.html')
             self.viewlets.append(menu)
 
 
@@ -88,14 +91,16 @@
 
     def update(self):
         super(SharedToolsMenu, self).update()
-        registry = get_local_registry()
-        for tool in sorted(registry.getAllUtilitiesRegisteredFor(IBaseSharedTool),
-                           key=lambda x: II18n(x).query_attribute('title', request=self.request) or ''):
+        context = self.context
+        request = self.request
+        parent = self.__parent__
+        for tool in sorted(get_all_utilities_registered_for(IBaseSharedTool),
+                           key=lambda x: II18n(x).query_attribute('title', request=request) or ''):
             if ISharedSite.providedBy(tool) or tool.shared_content_menu:
                 continue
-            menu = TopLinksMenu(self.context, self.request, self.__parent__, self)
-            menu.label = II18n(tool).query_attribute('title', request=self.request) or tool.__name__
-            menu.url = absolute_url(tool, self.request, 'admin#dashboard.html')
+            menu = TopLinksMenu(context, request, parent, self)
+            menu.label = II18n(tool).query_attribute('title', request=request) or tool.__name__
+            menu.url = absolute_url(tool, request, 'admin#dashboard.html')
             self.viewlets.append(menu)
 
 
@@ -110,15 +115,18 @@
     def update(self):
         super(UserRolesMenu, self).update()
         catalog = get_utility(ICatalog)
-        params = And(Or(Any(catalog['role:contributor'], {self.request.principal.id}),
-                        Any(catalog['role:manager'], {self.request.principal.id}),
-                        Any(catalog['role:pilot'], {self.request.principal.id})),
+        context = self.context
+        request = self.request
+        parent = self.__parent__
+        params = And(Or(Any(catalog['role:contributor'], {request.principal.id}),
+                        Any(catalog['role:manager'], {request.principal.id}),
+                        Any(catalog['role:pilot'], {request.principal.id})),
                      NotEq(catalog['content_type'], None))
         for tool in sorted(unique(CatalogResultSet(CatalogQuery(catalog).query(params))),
-                           key=lambda x: II18n(x).query_attribute('title', request=self.request) or ''):
-            menu = TopLinksMenu(self.context, self.request, self.__parent__, self)
-            menu.label = II18n(tool).query_attribute('title', request=self.request) or tool.__name__
-            menu.url = absolute_url(tool, self.request, 'admin#dashboard.html')
+                           key=lambda x: II18n(x).query_attribute('title', request=request) or ''):
+            menu = TopLinksMenu(context, request, parent, self)
+            menu.label = II18n(tool).query_attribute('title', request=request) or tool.__name__
+            menu.url = absolute_url(tool, request, 'admin#dashboard.html')
             self.viewlets.append(menu)
 
 
@@ -134,17 +142,23 @@
     def update(self):
         super(UserAddingsMenu, self).update()
         catalog = get_utility(ICatalog)
-        registry = get_current_registry()
-        params = And(Any(catalog['role:contributor'], {self.request.principal.id}),
+        context = self.context
+        request = self.request
+        parent = self.__parent__
+        registry = request.registry
+        params = And(Any(catalog['role:contributor'], {request.principal.id}),
                      NotEq(catalog['content_type'], None))
         for tool in sorted(unique(CatalogResultSet(CatalogQuery(catalog).query(params))),
-                           key=lambda x: II18n(x).query_attribute('title', request=self.request) or ''):
-            menu = TopLinksMenu(self.context, self.request, self.__parent__, self)
-            adapter = registry.queryMultiAdapter((tool, self.request), IUserAddingsMenuLabel)
+                           key=lambda x: II18n(x).query_attribute('title', request=request) or ''):
+            content_class = tool.shared_content_factory.content_class
+            if content_class is None:
+                continue
+            menu = TopLinksMenu(context, request, parent, self)
+            adapter = registry.queryMultiAdapter((tool, request), IUserAddingsMenuLabel)
             if adapter is None:
-                menu.label = self.request.localizer.translate(tool.shared_content_factory.content_class.content_name)
+                menu.label = request.localizer.translate(content_class.content_name)
             else:
                 menu.label = adapter.label
-            menu.url = absolute_url(tool, self.request, 'add-shared-content.html')
+            menu.url = absolute_url(tool, request, 'add-shared-content.html')
             menu.data = {'data-toggle': 'modal'}
             self.viewlets.append(menu)