src/pyams_content/features/alert/zmi/container.py
changeset 506 174894a2293d
child 527 5dd1aa8bedd9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/features/alert/zmi/container.py	Tue Apr 03 14:53:57 2018 +0200
@@ -0,0 +1,242 @@
+#
+# 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.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+import json
+
+# import interfaces
+from pyams_content.features.alert.interfaces import IAlertTarget, IAlertContainer
+from pyams_content.interfaces import MANAGE_SITE_ROOT_PERMISSION
+from pyams_i18n.interfaces import II18n
+from pyams_skin.interfaces import IPageHeader
+from pyams_skin.layer import IPyAMSLayer
+from pyams_zmi.interfaces.menu import ISiteManagementMenu
+from pyams_zmi.layer import IAdminLayer
+from z3c.table.interfaces import IValues, IColumn
+
+# import packages
+from pyams_content.skin import pyams_content
+from pyams_i18n.column import I18nAttrColumn
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.page import DefaultPageHeaderAdapter
+from pyams_skin.table import BaseTable, SorterColumn, JsActionColumn, I18nColumn, TrashColumn
+from pyams_skin.viewlet.menu import MenuItem
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyams_utils.fanstatic import get_resource_path
+from pyams_utils.text import get_text_start
+from pyams_utils.url import absolute_url
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_zmi.view import ContainerAdminView
+from pyramid.decorator import reify
+from pyramid.exceptions import NotFound
+from pyramid.view import view_config
+from z3c.table.column import GetAttrColumn
+
+from pyams_content import _
+
+
+@viewlet_config(name='alerts.menu', context=IAlertTarget, layer=IPyAMSLayer, manager=ISiteManagementMenu,
+                permission=MANAGE_SITE_ROOT_PERMISSION, weight=4)
+class AlertsMenu(MenuItem):
+    """Alerts menu"""
+
+    label = _("Alerts")
+    icon_class = 'fa-exclamation-triangle'
+    url = '#alerts.html'
+
+
+class AlertContainerTable(BaseTable):
+    """Alerts container table"""
+
+    id = 'alerts_table'
+    hide_header = True
+    sortOn = None
+
+    cssClasses = {'table': 'table table-bordered table-striped table-hover table-tight table-dnd'}
+
+    @property
+    def data_attributes(self):
+        attributes = super(AlertContainerTable, self).data_attributes
+        attributes.setdefault('table', {}).update({
+            'id': self.id,
+            'data-ams-plugins': 'pyams_content',
+            'data-ams-plugin-pyams_content-src': get_resource_path(pyams_content),
+            'data-ams-location': absolute_url(IAlertContainer(self.context), self.request),
+            'data-ams-tablednd-drag-handle': 'td.sorter',
+            'data-ams-tablednd-drop-target': 'set-alerts-order.json'
+        })
+        attributes.setdefault('tr', {}).update({
+            'id': lambda x, col: 'alert_{0}'.format(x.__name__),
+            'data-ams-delete-target': 'delete-alert.json'
+        })
+        return attributes
+
+    @reify
+    def values(self):
+        return list(super(AlertContainerTable, self).values)
+
+    def render(self):
+        if not self.values:
+            translate = self.request.localizer.translate
+            return translate(_("No currently defined alert."))
+        return super(AlertContainerTable, self).render()
+
+
+@adapter_config(context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IValues)
+class AlertContainerValues(ContextRequestViewAdapter):
+    """Alerts container values"""
+
+    @property
+    def values(self):
+        return IAlertContainer(self.context).values()
+
+
+@adapter_config(name='sorter', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+class AlertContainerSorterColumn(SorterColumn):
+    """Alert container sorter column"""
+
+
+@view_config(name='set-alerts-order.json', context=IAlertContainer, request_type=IPyAMSLayer,
+             permission=MANAGE_SITE_ROOT_PERMISSION, renderer='json', xhr=True)
+def set_alerts_order(request):
+    """Update alerts order"""
+    order = list(map(str, json.loads(request.params.get('names'))))
+    request.context.updateOrder(order)
+    return {'status': 'success'}
+
+
+@adapter_config(name='show-hide', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+class AlertContainerShowHideColumn(JsActionColumn):
+    """Alert container visibility switcher column"""
+
+    cssClasses = {'th': 'action',
+                  'td': 'action switcher'}
+
+    icon_class = 'fa fa-fw fa-eye'
+    icon_hint = _("Switch alert visibility")
+
+    url = 'PyAMS_content.alerts.switchVisibility'
+
+    weight = 5
+
+    def get_icon_class(self, item):
+        if item.visible:
+            return self.icon_class
+        else:
+            return 'fa fa-fw fa-eye-slash text-danger'
+
+
+@view_config(name='set-alert-visibility.json', context=IAlertContainer, request_type=IPyAMSLayer,
+             permission=MANAGE_SITE_ROOT_PERMISSION, renderer='json', xhr=True)
+def set_alert_visibility(request):
+    """Set alert visibility"""
+    container = IAlertContainer(request.context)
+    alert = container.get(str(request.params.get('object_name')))
+    if alert is None:
+        raise NotFound()
+    alert.visible = not alert.visible
+    return {'visible': alert.visible}
+
+
+@adapter_config(name='pictogram', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+class AlertContainerPictogramColumn(GetAttrColumn):
+    """Alert container pictogram image column"""
+
+    header = ''
+    weight = 10
+
+    cssClasses = {'td': 'text-center width-50'}
+    dt_sortable = 'false'
+
+    def getValue(self, obj):
+        pictogram = obj.pictogram
+        if pictogram is not None:
+            image = II18n(pictogram).query_attribute('image', request=self.request)
+            if image:
+                return '<img src="{0}" />'.format(absolute_url(image, self.request, '++thumb++32x32'))
+        return '--'
+
+
+@adapter_config(name='header', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+class AlertContainerHeaderColumn(I18nColumn, I18nAttrColumn):
+    """Alert container header column"""
+
+    _header = _("Header")
+    attrName = 'header'
+    weight = 20
+
+    def getValue(self, obj):
+        return super(AlertContainerHeaderColumn, self).getValue(obj) or '--'
+
+
+@adapter_config(name='name', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+class AlertContainerNameColumn(I18nColumn, I18nAttrColumn):
+    """Alert container message column"""
+
+    _header = _("Message")
+    attrName = 'message'
+    weight = 30
+
+    def getValue(self, obj):
+        value = super(AlertContainerNameColumn, self).getValue(obj)
+        if not value:
+            return '--'
+        return get_text_start(value, 50, 10)
+
+
+@adapter_config(name='trash', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+class AlertContainerTrashColumn(TrashColumn):
+    """Alert container trash column"""
+
+
+@view_config(name='delete-alert.json', context=IAlertTarget, request_type=IPyAMSLayer,
+             permission=MANAGE_SITE_ROOT_PERMISSION, renderer='json', xhr=True)
+def delete_alert(request):
+    """Delete alert"""
+    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 alert name doesn't exist!"))
+            }
+        }
+    del request.context[name]
+    return {'status': 'success'}
+
+
+@pagelet_config(name='alerts.html', context=IAlertTarget, layer=IPyAMSLayer, permission=MANAGE_SITE_ROOT_PERMISSION)
+class AlertContainerView(ContainerAdminView):
+    """Alerts container view"""
+
+    title = _("Alert list")
+    table_class = AlertContainerTable
+
+
+@adapter_config(context=(IAlertTarget, IAdminLayer, AlertContainerView), provides=IPageHeader)
+class AlertContainerViewHeaderAdapter(DefaultPageHeaderAdapter):
+    """Alerts container view header adapter"""
+
+    icon_class = 'fa fa-fw fa-exclamation-triangle'