--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_catalog/zmi/catalog.py Tue Dec 27 15:43:09 2016 +0100
@@ -0,0 +1,246 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+from pyramid.view import view_config
+
+from pyams_utils.url import absolute_url
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from hypatia.interfaces import ICatalog, IIndexStatistics, IIndexEnumeration
+from pyams_form.interfaces.form import IWidgetsSuffixViewletsManager
+from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION
+from pyams_zmi.layer import IAdminLayer
+from z3c.table.interfaces import IColumn, IValues
+
+# import packages
+from pyams_catalog.index import InterfaceSupportIndexMixin
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.table import BaseTable, I18nColumn, TrashColumn
+from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyams_utils.text import text_to_html
+from pyams_viewlet.viewlet import viewlet_config, Viewlet
+from pyams_zmi.form import AdminDialogDisplayForm
+from z3c.form import field
+from z3c.table.column import GetAttrColumn
+from zope.interface import Interface
+
+from pyams_catalog import _
+
+
+@pagelet_config(name='properties.html', context=ICatalog, layer=IPyAMSLayer, permission=VIEW_SYSTEM_PERMISSION)
+class CatalogPropertiesDisplayForm(AdminDialogDisplayForm):
+ """Catalog properties display form"""
+
+ @property
+ def title(self):
+ return self.context.__name__
+
+ dialog_class = 'modal-max'
+ legend = _("Display catalog indexes")
+
+ fields = field.Fields(Interface)
+
+
+class CatalogIndexesTable(BaseTable):
+ """Catalog indexes table"""
+
+ id = 'CatalogIndexes'
+ title = _("Catalog indexes")
+ cssClasses = {'table': 'table table-bordered table-striped table-hover table-tight datatable'}
+ sortOn = None
+
+ @property
+ def data_attributes(self):
+ attributes = super(CatalogIndexesTable, self).data_attributes
+ table_attrs = {'data-ams-location': absolute_url(self.context, self.request),
+ 'data-ams-delete-target': 'delete-index.json',
+ 'data-ams-datatable-global-filter': 'false',
+ 'data-ams-datatable-info': 'false',
+ 'data-ams-datatable-sdom': "t<'dt-row dt-bottom-row'<'text-right'p>>",
+ 'data-ams-datatable-display-length': '20',
+ 'data-ams-datatable-pagination-type': 'bootstrap_prevnext'}
+ if 'table' in attributes:
+ attributes['table'].update(table_attrs)
+ else:
+ attributes['table'] = table_attrs
+ return attributes
+
+
+@adapter_config(name='name', context=(Interface, IAdminLayer, CatalogIndexesTable), provides=IColumn)
+class CatalogIndexNameColumn(I18nColumn, GetAttrColumn):
+ """Catalog index name column"""
+
+ _header = _("Name")
+ weight = 1
+
+ attrName = '__name__'
+
+
+@adapter_config(name='class', context=(Interface, IAdminLayer, CatalogIndexesTable), provides=IColumn)
+class CatalogIndexClassColumn(I18nColumn, GetAttrColumn):
+ """Catalog index class column"""
+
+ _header = _("Index type")
+ weight = 2
+
+ def getValue(self, obj):
+ return text_to_html(str(obj.__class__))
+
+
+@adapter_config(name='interface', context=(Interface, IAdminLayer, CatalogIndexesTable), provides=IColumn)
+class CatalogIndexInterfaceColumn(I18nColumn, GetAttrColumn):
+ """Catalog index interface column"""
+
+ _header = _("Interface")
+ weight = 3
+
+ def getValue(self, obj):
+ if isinstance(obj, InterfaceSupportIndexMixin):
+ return text_to_html(str(obj.interface))
+ else:
+ return '--'
+
+
+@adapter_config(name='discriminator', context=(Interface, IAdminLayer, CatalogIndexesTable), provides=IColumn)
+class CatalogIndexDiscriminatorColumn(I18nColumn, GetAttrColumn):
+ """Catalog index discriminator column"""
+
+ _header = _("Discriminator")
+ weight = 4
+
+ def getValue(self, obj):
+ return obj.discriminator
+
+
+@adapter_config(name='nb_docs', context=(Interface, IAdminLayer, CatalogIndexesTable), provides=IColumn)
+class CatalogIndexNbDocsColumn(I18nColumn, GetAttrColumn):
+ """Catalog index documents count column"""
+
+ _header = _("#docs")
+ weight = 5
+
+ cssClasses = {'td': 'center'}
+
+ def getValue(self, obj):
+ if IIndexEnumeration.providedBy(obj):
+ return obj.docids_count()
+ else:
+ return '--'
+
+
+@adapter_config(name='nb_indexed', context=(Interface, IAdminLayer, CatalogIndexesTable), provides=IColumn)
+class CatalogIndexNbIndexedColumn(I18nColumn, GetAttrColumn):
+ """Catalog index documents indexed column"""
+
+ _header = _("#indexed")
+ weight = 6
+
+ cssClasses = {'td': 'center'}
+
+ def getValue(self, obj):
+ if IIndexEnumeration.providedBy(obj):
+ return obj.indexed_count()
+ else:
+ return '--'
+
+
+@adapter_config(name='nb_unindexed', context=(Interface, IAdminLayer, CatalogIndexesTable), provides=IColumn)
+class CatalogIndexNbUnindexedColumn(I18nColumn, GetAttrColumn):
+ """Catalog index documents not-indexed column"""
+
+ _header = _("#unindexed")
+ weight = 7
+
+ cssClasses = {'td': 'center'}
+
+ def getValue(self, obj):
+ if IIndexEnumeration.providedBy(obj):
+ return obj.not_indexed_count()
+ else:
+ return '--'
+
+
+@adapter_config(name='nb_words', context=(Interface, IAdminLayer, CatalogIndexesTable), provides=IColumn)
+class CatalogIndexNbWordsColumn(I18nColumn, GetAttrColumn):
+ """Catalog index documents count column"""
+
+ _header = _("#values")
+ weight = 10
+
+ cssClasses = {'td': 'center'}
+
+ def getValue(self, obj):
+ if IIndexStatistics.providedBy(obj):
+ return obj.word_count()
+ else:
+ return '--'
+
+
+@adapter_config(name='trash', context=(Interface, IAdminLayer, CatalogIndexesTable), provides=IColumn)
+class CatalogIndexTrashColumn(TrashColumn):
+ """Catalog index trash column"""
+
+ icon_hint = _("Delete index")
+ permission = MANAGE_SYSTEM_PERMISSION
+
+ def has_permission(self, item):
+ return super(CatalogIndexTrashColumn, self).has_permission(self.context)
+
+
+@adapter_config(context=(ICatalog, IAdminLayer, CatalogIndexesTable), provides=IValues)
+class CatalogIndexesValuesAdapter(ContextRequestViewAdapter):
+ """Catalog indexes values adapter"""
+
+ @property
+ def values(self):
+ return self.context.values()
+
+
+@viewlet_config(name='catalog-indexes', view=CatalogPropertiesDisplayForm, layer=IAdminLayer,
+ manager=IWidgetsSuffixViewletsManager)
+@template_config(template='templates/catalog-indexes.pt')
+class CatalogIndexesViewlet(Viewlet):
+ """Catalog indexes viewlet"""
+
+ table = None
+
+ def __init__(self, context, request, view, manager):
+ super(CatalogIndexesViewlet, self).__init__(context, request, view, manager)
+ self.table = CatalogIndexesTable(context, request)
+
+ def update(self):
+ super(CatalogIndexesViewlet, self).update()
+ self.table.update()
+
+
+@view_config(name='delete-index.json', context=ICatalog, request_type=IPyAMSLayer,
+ permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
+def delete_catalog_index(request):
+ """Delete index from catalog"""
+ 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!"))}}
+ if name not in request.context:
+ return {'status': 'message',
+ 'messagebox': {'status': 'error',
+ 'content': translate(_("Given index doesn't exist!"))}}
+ del request.context[name]
+ return {'status': 'success'}