Add global registry utilities view
authorThierry Florac <thierry.florac@onf.fr>
Tue, 03 Mar 2015 16:53:26 +0100
changeset 3 b284b151f292
parent 2 bc1d70f9a9e2
child 4 0ff044965637
Add global registry utilities view
src/pyams_zmi/control_panel.py
src/pyams_zmi/locales/fr/LC_MESSAGES/pyams_zmi.mo
src/pyams_zmi/locales/fr/LC_MESSAGES/pyams_zmi.po
src/pyams_zmi/locales/pyams_zmi.pot
--- a/src/pyams_zmi/control_panel.py	Sat Feb 28 16:20:01 2015 +0100
+++ b/src/pyams_zmi/control_panel.py	Tue Mar 03 16:53:26 2015 +0100
@@ -14,10 +14,12 @@
 
 
 # import standard library
+from html import escape
 
 # import interfaces
 from pyams_skin.interfaces import IInnerPage, IPageHeader, IContentHelp
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces.site import IOptionalUtility
 from pyams_zmi.interfaces.menu import IControlPanelMenu, IUtilitiesMenu
 from pyams_zmi.layer import IAdminLayer
 from z3c.table.interfaces import IValues, IColumn
@@ -27,15 +29,18 @@
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_skin.container import ContainerView
 from pyams_skin.help import ContentHelp
-from pyams_skin.table import BaseTable
+from pyams_skin.table import BaseTable, TrashColumn
 from pyams_skin.viewlet.menu import MenuItem
 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyams_utils.registry import registered_utilities
 from pyams_utils.text import text_to_html
+from pyams_utils.url import absolute_url
 from pyams_viewlet.manager import viewletmanager_config
 from pyams_viewlet.viewlet import viewlet_config
 from pyams_zmi.view import AdminView
+from pyramid.view import view_config
 from z3c.table.column import GetAttrColumn
-from zope.interface import implementer
+from zope.interface import implementer, Interface
 
 from pyams_zmi import _
 
@@ -63,6 +68,23 @@
     title = _("Site utilities")
     cssClasses = {'table': 'table table-bordered table-striped table-hover table-tight datatable'}
 
+    @property
+    def data_attributes(self):
+        attrs = super(UtilitiesTable, self).data_attributes
+        attrs['table'] = {'data-ams-location': absolute_url(self.context, self.request),
+                          'data-ams-delete-target': 'delete-utility.json'}
+        return attrs
+
+
+@adapter_config(name='trash', context=(Interface, IAdminLayer, UtilitiesTable), provides=IColumn)
+class UtilitiesTrashColumn(TrashColumn):
+    """Utilities trash column"""
+
+    icon_hint = _("Delete utility")
+    permission = 'system.manage'
+
+    checker = lambda col, x: IOptionalUtility.providedBy(x)
+
 
 @adapter_config(context=(ISite, IAdminLayer, UtilitiesTable), provides=IValues)
 class UtilitiesValuesAdapter(ContextRequestViewAdapter):
@@ -93,38 +115,34 @@
     subtitle = _("Utilities")
 
 
-#
-# Registrations views and adapters
-#
-
-@viewlet_config(name='registrations.menu', layer=IAdminLayer, context=ISite, manager=IUtilitiesMenu,
-                permission='system.view', weight=1)
-class RegistrationsMenuItem(MenuItem):
-    """Registrations menu"""
-
-    label = _("Registrations")
-    url = '#registrations.html'
+@view_config(name='delete-utility.json', context=ISite, request_type=IPyAMSLayer,
+             permission='system.manage', renderer='json', xhr=True)
+def delete_utility(request):
+    """Delete utility from site manager"""
+    translate = request.localizer.translate
+    name = request.params.get('object_name')
+    if not name:
+        return {'status': 'message',
+                'messagebox': {'status': 'error',
+                               'content': translate(_("No provided object_name argument!"))}}
+    manager = request.context.getSiteManager()
+    if name not in manager:
+        return {'status': 'message',
+                'messagebox': {'status': 'error',
+                               'content': translate(_("Given utility name doesn't exist!"))}}
+    del manager[name]
+    return {'status': 'success'}
 
 
-class RegistrationsTable(BaseTable):
-    """Utilities registrations table"""
+#
+# Common registrations views and adapters
+#
 
-    id = 'registrations_table'
-    title = _("Utilities registrations")
-
-    data_attributes = {}
+class IRegistrationsTable(Interface):
+    """Registrations view marker interface"""
 
 
-@adapter_config(context=(ISite, IAdminLayer, RegistrationsTable), provides=IValues)
-class RegistrationsValuesAdapter(ContextRequestViewAdapter):
-    """Utilities values adapter"""
-
-    @property
-    def values(self):
-        return list(self.context.getSiteManager().registeredUtilities())
-
-
-@adapter_config(name='component', context=(ISite, IAdminLayer, RegistrationsTable), provides=IColumn)
+@adapter_config(name='component', context=(ISite, IAdminLayer, IRegistrationsTable), provides=IColumn)
 class RegistrationsComponentColumn(GetAttrColumn):
     """Registrations component column"""
 
@@ -136,10 +154,17 @@
         return self.request.localizer.translate(self._header)
 
     def getValue(self, obj):
-        return obj.component.__name__
+        component = obj.component
+        if component is not None:
+            name = getattr(component, '__name__', None)
+            if not name:
+                name = str(component.__class__)
+        else:
+            name = str(obj.factory)
+        return escape(name)
 
 
-@adapter_config(name='interface', context=(ISite, IAdminLayer, RegistrationsTable), provides=IColumn)
+@adapter_config(name='interface', context=(ISite, IAdminLayer, IRegistrationsTable), provides=IColumn)
 class RegistrationsInterfaceColumn(GetAttrColumn):
     """Registrations interface column"""
 
@@ -154,7 +179,7 @@
         return text_to_html(str(obj.provided))
 
 
-@adapter_config(name='name', context=(ISite, IAdminLayer, RegistrationsTable), provides=IColumn)
+@adapter_config(name='name', context=(ISite, IAdminLayer, IRegistrationsTable), provides=IColumn)
 class RegistrationsNameColumn(GetAttrColumn):
     """Registrations name column"""
 
@@ -169,29 +194,61 @@
         return obj.name or _('< no name >')
 
 
-@pagelet_config(name='registrations.html', context=ISite, layer=IPyAMSLayer, permission='system.view')
+#
+# Local registrations views
+#
+
+@viewlet_config(name='local-registrations.menu', layer=IAdminLayer, context=ISite, manager=IUtilitiesMenu,
+                permission='system.view', weight=1)
+class LocalRegistrationsMenuItem(MenuItem):
+    """Local registrations menu"""
+
+    label = _("Local registrations")
+    url = '#local-registrations.html'
+
+
+@implementer(IRegistrationsTable)
+class LocalRegistrationsTable(BaseTable):
+    """Local utilities registrations table"""
+
+    id = 'local_registrations_table'
+    title = _("Local utilities registrations")
+
+    data_attributes = {}
+
+
+@adapter_config(context=(ISite, IAdminLayer, LocalRegistrationsTable), provides=IValues)
+class LocalRegistrationsValuesAdapter(ContextRequestViewAdapter):
+    """Local utilities values adapter"""
+
+    @property
+    def values(self):
+        return list(self.context.getSiteManager().registeredUtilities())
+
+
+@pagelet_config(name='local-registrations.html', context=ISite, layer=IPyAMSLayer, permission='system.view')
 @implementer(IInnerPage)
-class RegistrationsView(AdminView, ContainerView):
+class LocalRegistrationsView(AdminView, ContainerView):
     """Registrations view"""
 
-    table_class = RegistrationsTable
+    table_class = LocalRegistrationsTable
 
     def __init__(self, context, request):
-        super(RegistrationsView, self).__init__(context, request)
+        super(LocalRegistrationsView, self).__init__(context, request)
 
 
-@adapter_config(context=(ISite, IAdminLayer, RegistrationsView), provides=IPageHeader)
-class RegistrationsHeaderAdapter(ContextRequestViewAdapter):
-    """Registrations header adapter"""
+@adapter_config(context=(ISite, IAdminLayer, LocalRegistrationsView), provides=IPageHeader)
+class LocalRegistrationsHeaderAdapter(ContextRequestViewAdapter):
+    """Local registrations header adapter"""
 
     icon_class = 'fa fa-fw fa-codepen'
     title = _("Control panel")
     subtitle = _("Local utilities registry")
 
 
-@adapter_config(context=(ISite, IAdminLayer, RegistrationsView), provides=IContentHelp)
-class RegistrationsHelpAdapter(ContentHelp):
-    """Registrations help adapter"""
+@adapter_config(context=(ISite, IAdminLayer, LocalRegistrationsView), provides=IContentHelp)
+class LocalRegistrationsHelpAdapter(ContentHelp):
+    """Local registrations help adapter"""
 
     header = _("Local registry utilities")
     message = _("""A local registry is a registry defining utilities stored into
@@ -200,3 +257,68 @@
 You can manage these utilities and modify their properties and site's behaviour without
 modifying the application.""")
     message_format = 'rest'
+
+
+#
+# Global registrations views
+#
+
+@viewlet_config(name='global-registrations.menu', layer=IAdminLayer, context=ISite, manager=IUtilitiesMenu,
+                permission='system.view', weight=2)
+class GlobalRegistrationsMenuItem(MenuItem):
+    """Global registrations menu"""
+
+    label = _("Global registrations")
+    url = '#global-registrations.html'
+
+
+@implementer(IRegistrationsTable)
+class GlobalRegistrationsTable(BaseTable):
+    """Utilities global registrations table"""
+
+    id = 'global_registrations_table'
+    title = _("Global utilities registrations")
+
+    data_attributes = {}
+
+
+@adapter_config(context=(ISite, IAdminLayer, GlobalRegistrationsTable), provides=IValues)
+class GlobalRegistrationsValuesAdapter(ContextRequestViewAdapter):
+    """Global utilities values adapter"""
+
+    @property
+    def values(self):
+        return list(registered_utilities())
+
+
+@pagelet_config(name='global-registrations.html', context=ISite, layer=IPyAMSLayer, permission='system.view')
+@implementer(IInnerPage)
+class GlobalRegistrationsView(AdminView, ContainerView):
+    """Global registrations view"""
+
+    table_class = GlobalRegistrationsTable
+
+    def __init__(self, context, request):
+        super(GlobalRegistrationsView, self).__init__(context, request)
+
+
+@adapter_config(context=(ISite, IAdminLayer, GlobalRegistrationsView), provides=IPageHeader)
+class GlobalRegistrationsHeaderAdapter(ContextRequestViewAdapter):
+    """Global registrations header adapter"""
+
+    icon_class = 'fa fa-fw fa-codepen'
+    title = _("Control panel")
+    subtitle = _("Global utilities registry")
+
+
+@adapter_config(context=(ISite, IAdminLayer, GlobalRegistrationsView), provides=IContentHelp)
+class GlobalRegistrationsHelpAdapter(ContentHelp):
+    """Global registrations help adapter"""
+
+    header = _("Global registry utilities")
+    message = _("""The global registry groups local utilities as well as utilities registered outside
+site's Object Database (ZODB).
+
+These utilities are declared statically (generally using include or ZCML directives) and can't be setup
+without modifying site configuration.""")
+    message_format = 'rest'
Binary file src/pyams_zmi/locales/fr/LC_MESSAGES/pyams_zmi.mo has changed
--- a/src/pyams_zmi/locales/fr/LC_MESSAGES/pyams_zmi.po	Sat Feb 28 16:20:01 2015 +0100
+++ b/src/pyams_zmi/locales/fr/LC_MESSAGES/pyams_zmi.po	Tue Mar 03 16:53:26 2015 +0100
@@ -5,7 +5,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE 1.0\n"
-"POT-Creation-Date: 2015-02-04 11:00+0100\n"
+"POT-Creation-Date: 2015-03-03 16:47+0100\n"
 "PO-Revision-Date: 2015-02-04 09:50+0100\n"
 "Last-Translator: Thierry Florac <tflorac@ulthar.net>\n"
 "Language-Team: French <traduc@traduc.org>\n"
@@ -20,48 +20,53 @@
 msgid "PyAMS management skin"
 msgstr "Skin d'administration PyAMS"
 
-#: src/pyams_zmi/control_panel.py:54 src/pyams_zmi/control_panel.py:94
+#: src/pyams_zmi/control_panel.py:59 src/pyams_zmi/control_panel.py:115
 msgid "Utilities"
 msgstr "Utilitaires"
 
-#: src/pyams_zmi/control_panel.py:63
+#: src/pyams_zmi/control_panel.py:68
 msgid "Site utilities"
 msgstr "Utilitaires du site"
 
-#: src/pyams_zmi/control_panel.py:93 src/pyams_zmi/control_panel.py:193
-#: src/pyams_zmi/viewlet/menu/__init__.py:47
+#: src/pyams_zmi/control_panel.py:83
+msgid "Delete utility"
+msgstr "Supprimer cet utilitaire"
+
+#: src/pyams_zmi/control_panel.py:114 src/pyams_zmi/control_panel.py:245
+#: src/pyams_zmi/control_panel.py:310
+#: src/pyams_zmi/viewlet/menu/__init__.py:49
 msgid "Control panel"
 msgstr "Panneau de configuration"
 
-#: src/pyams_zmi/control_panel.py:105
-msgid "Registrations"
-msgstr "Inscriptions dans le registre"
-
-#: src/pyams_zmi/control_panel.py:113
-msgid "Utilities registrations"
-msgstr "Utilitaires inscrits dans le registre local"
-
-#: src/pyams_zmi/control_panel.py:133
+#: src/pyams_zmi/control_panel.py:149
 msgid "Component"
 msgstr "Composant"
 
-#: src/pyams_zmi/control_panel.py:149
+#: src/pyams_zmi/control_panel.py:171
 msgid "Registered interface"
 msgstr "Interface enregistrée"
 
-#: src/pyams_zmi/control_panel.py:165
+#: src/pyams_zmi/control_panel.py:186
 msgid "Name"
 msgstr "Nom"
 
-#: src/pyams_zmi/control_panel.py:194
+#: src/pyams_zmi/control_panel.py:206
+msgid "Local registrations"
+msgstr "Registre local"
+
+#: src/pyams_zmi/control_panel.py:215
+msgid "Local utilities registrations"
+msgstr "Registre local des composants"
+
+#: src/pyams_zmi/control_panel.py:246
 msgid "Local utilities registry"
 msgstr "Registre local des composants"
 
-#: src/pyams_zmi/control_panel.py:202
+#: src/pyams_zmi/control_panel.py:253
 msgid "Local registry utilities"
 msgstr "Le registre local des composants"
 
-#: src/pyams_zmi/control_panel.py:203
+#: src/pyams_zmi/control_panel.py:254
 msgid ""
 "A local registry is a registry defining utilities stored into\n"
 "site's Object Database (ZODB).\n"
@@ -70,32 +75,73 @@
 "behaviour without\n"
 "modifying the application."
 msgstr ""
-"Un registre local est une liste de composants stockés dans la base "
-"de données du site (ZODB).\n"
+"Un registre local est une liste de composants stockés dans la base de "
+"données du site (ZODB).\n"
 "\n"
-"Chaque composant peut être inscrit une ou plusieurs fois pour fournir une "
-"ou plusieurs interfaces. Un composant présent dans la liste des utilitaires "
+"Chaque composant peut être inscrit une ou plusieurs fois pour fournir une ou "
+"plusieurs interfaces. Un composant présent dans la liste des utilitaires "
 "mais non inscrit dans le registre est le plus souvent inutilisé !\n"
 "\n"
 "Vous pouvez gérer ces composants et modifier leurs propriétés pour changer "
 "le comportement du site sans modifier l'application."
 
-#: src/pyams_zmi/control_panel.py:173
+#: src/pyams_zmi/control_panel.py:271
+msgid "Global registrations"
+msgstr "Registre global"
+
+#: src/pyams_zmi/control_panel.py:280
+msgid "Global utilities registrations"
+msgstr "Registre global des composants"
+
+#: src/pyams_zmi/control_panel.py:311
+msgid "Global utilities registry"
+msgstr "Registre global des composants"
+
+#: src/pyams_zmi/control_panel.py:318
+msgid "Global registry utilities"
+msgstr "Le registre global des composants"
+
+#: src/pyams_zmi/control_panel.py:319
+msgid ""
+"The global registry groups local utilities as well as utilities registered "
+"outside\n"
+"site's Object Database (ZODB).\n"
+"\n"
+"These utilities are declared statically (generally using include or ZCML "
+"directives) and can't be setup\n"
+"without modifying site configuration."
+msgstr ""
+"Un registre global regroupe les utilitaires locaux ainsi que ceux définis "
+"en dehors de la ZODB.\n"
+"\n"
+"Ces composants sont déclarés de façon statique (en général via des inclusions de "
+"paquets ou des directives ZCML) et ne peuvent être modifiés sans modifier la "
+"configuration du site."
+
+#: src/pyams_zmi/control_panel.py:194
 msgid "< no name >"
 msgstr "< pas de nom >"
 
-#: src/pyams_zmi/site.py:49
+#: src/pyams_zmi/control_panel.py:127
+msgid "No provided object_name argument!"
+msgstr "L'argument 'object_name' n'a pas été fourni !"
+
+#: src/pyams_zmi/control_panel.py:132
+msgid "Given utility name doesn't exist!"
+msgstr "L'utilitaire indiqué n'existe pas !"
+
+#: src/pyams_zmi/site.py:50
 msgid "Properties"
 msgstr "Propriétés"
 
-#: src/pyams_zmi/site.py:59
+#: src/pyams_zmi/site.py:60
 msgid "Update main site properties"
 msgstr "Mise à jour des propriétés de base du site"
 
-#: src/pyams_zmi/site.py:82 src/pyams_zmi/viewlet/menu/__init__.py:38
+#: src/pyams_zmi/site.py:99 src/pyams_zmi/viewlet/menu/__init__.py:39
 msgid "Site management"
 msgstr "Gérer le site"
 
-#: src/pyams_zmi/site.py:83
+#: src/pyams_zmi/site.py:100
 msgid "Site properties"
 msgstr "Propriétés"
--- a/src/pyams_zmi/locales/pyams_zmi.pot	Sat Feb 28 16:20:01 2015 +0100
+++ b/src/pyams_zmi/locales/pyams_zmi.pot	Tue Mar 03 16:53:26 2015 +0100
@@ -6,7 +6,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE 1.0\n"
-"POT-Creation-Date: 2015-02-04 11:00+0100\n"
+"POT-Creation-Date: 2015-03-03 16:47+0100\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -20,48 +20,53 @@
 msgid "PyAMS management skin"
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:54 ./src/pyams_zmi/control_panel.py:94
+#: ./src/pyams_zmi/control_panel.py:59 ./src/pyams_zmi/control_panel.py:115
 msgid "Utilities"
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:63
+#: ./src/pyams_zmi/control_panel.py:68
 msgid "Site utilities"
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:93 ./src/pyams_zmi/control_panel.py:193
-#: ./src/pyams_zmi/viewlet/menu/__init__.py:47
+#: ./src/pyams_zmi/control_panel.py:83
+msgid "Delete utility"
+msgstr ""
+
+#: ./src/pyams_zmi/control_panel.py:114 ./src/pyams_zmi/control_panel.py:245
+#: ./src/pyams_zmi/control_panel.py:310
+#: ./src/pyams_zmi/viewlet/menu/__init__.py:49
 msgid "Control panel"
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:105
-msgid "Registrations"
-msgstr ""
-
-#: ./src/pyams_zmi/control_panel.py:113
-msgid "Utilities registrations"
-msgstr ""
-
-#: ./src/pyams_zmi/control_panel.py:133
+#: ./src/pyams_zmi/control_panel.py:149
 msgid "Component"
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:149
+#: ./src/pyams_zmi/control_panel.py:171
 msgid "Registered interface"
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:165
+#: ./src/pyams_zmi/control_panel.py:186
 msgid "Name"
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:194
+#: ./src/pyams_zmi/control_panel.py:206
+msgid "Local registrations"
+msgstr ""
+
+#: ./src/pyams_zmi/control_panel.py:215
+msgid "Local utilities registrations"
+msgstr ""
+
+#: ./src/pyams_zmi/control_panel.py:246
 msgid "Local utilities registry"
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:202
+#: ./src/pyams_zmi/control_panel.py:253
 msgid "Local registry utilities"
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:203
+#: ./src/pyams_zmi/control_panel.py:254
 msgid ""
 "A local registry is a registry defining utilities stored into\n"
 "site's Object Database (ZODB).\n"
@@ -70,22 +75,55 @@
 "modifying the application."
 msgstr ""
 
-#: ./src/pyams_zmi/control_panel.py:173
+#: ./src/pyams_zmi/control_panel.py:271
+msgid "Global registrations"
+msgstr ""
+
+#: ./src/pyams_zmi/control_panel.py:280
+msgid "Global utilities registrations"
+msgstr ""
+
+#: ./src/pyams_zmi/control_panel.py:311
+msgid "Global utilities registry"
+msgstr ""
+
+#: ./src/pyams_zmi/control_panel.py:318
+msgid "Global registry utilities"
+msgstr ""
+
+#: ./src/pyams_zmi/control_panel.py:319
+msgid ""
+"The global registry groups local utilities as well as utilities registered outside\n"
+"site's Object Database (ZODB).\n"
+"\n"
+"These utilities are declared statically (generally using include or ZCML directives) and can't be setup\n"
+"without modifying site configuration."
+msgstr ""
+
+#: ./src/pyams_zmi/control_panel.py:194
 msgid "< no name >"
 msgstr ""
 
-#: ./src/pyams_zmi/site.py:49
+#: ./src/pyams_zmi/control_panel.py:127
+msgid "No provided object_name argument!"
+msgstr ""
+
+#: ./src/pyams_zmi/control_panel.py:132
+msgid "Given utility name doesn't exist!"
+msgstr ""
+
+#: ./src/pyams_zmi/site.py:50
 msgid "Properties"
 msgstr ""
 
-#: ./src/pyams_zmi/site.py:59
+#: ./src/pyams_zmi/site.py:60
 msgid "Update main site properties"
 msgstr ""
 
-#: ./src/pyams_zmi/site.py:82 ./src/pyams_zmi/viewlet/menu/__init__.py:38
+#: ./src/pyams_zmi/site.py:99 ./src/pyams_zmi/viewlet/menu/__init__.py:39
 msgid "Site management"
 msgstr ""
 
-#: ./src/pyams_zmi/site.py:83
+#: ./src/pyams_zmi/site.py:100
 msgid "Site properties"
 msgstr ""