Restore icon state correctly when alert display is switched
authorThierry Florac <tflorac@ulthar.net>
Mon, 06 Jul 2020 16:23:13 +0200
changeset 1401 5b89ee261e0d
parent 1400 328c4ccd9275
child 1402 4304b80a00b3
Restore icon state correctly when alert display is switched
src/pyams_content/features/alert/zmi/container.py
--- a/src/pyams_content/features/alert/zmi/container.py	Fri Jul 03 19:23:45 2020 +0200
+++ b/src/pyams_content/features/alert/zmi/container.py	Mon Jul 06 16:23:13 2020 +0200
@@ -10,43 +10,41 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
-
-
-# import standard library
 import json
 
-# import interfaces
-from pyams_content.features.alert.interfaces import IAlertTarget, IAlertContainer
+from pyramid.decorator import reify
+from pyramid.view import view_config
+from z3c.table.interfaces import IColumn, IValues
+
+from pyams_content.features.alert.interfaces import IAlertContainer, IAlertTarget
 from pyams_content.interfaces import MANAGE_SITE_ROOT_PERMISSION
-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.zmi import pyams_content
 from pyams_i18n.column import I18nAttrColumn
 from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.container import switch_element_visibility
+from pyams_skin.interfaces import IPageHeader
+from pyams_skin.layer import IPyAMSLayer
 from pyams_skin.page import DefaultPageHeaderAdapter
-from pyams_skin.table import BaseTable, SorterColumn, I18nColumn, TrashColumn, VisibilitySwitcherColumn
+from pyams_skin.table import BaseTable, I18nColumn, SorterColumn, TrashColumn, \
+    VisibilitySwitcherColumn
 from pyams_skin.viewlet.menu import MenuItem
-from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
 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.interfaces.menu import ISiteManagementMenu
+from pyams_zmi.layer import IAdminLayer
 from pyams_zmi.view import ContainerAdminView
-from pyramid.decorator import reify
-from pyramid.exceptions import NotFound
-from pyramid.view import view_config
+
+
+__docformat__ = 'restructuredtext'
 
 from pyams_content import _
 
 
-@viewlet_config(name='alerts.menu', context=IAlertTarget, layer=IPyAMSLayer, manager=ISiteManagementMenu,
-                permission=MANAGE_SITE_ROOT_PERMISSION, weight=30)
+@viewlet_config(name='alerts.menu', context=IAlertTarget, layer=IPyAMSLayer,
+                manager=ISiteManagementMenu, permission=MANAGE_SITE_ROOT_PERMISSION, weight=30)
 class AlertsMenu(MenuItem):
     """Alerts menu"""
 
@@ -105,7 +103,8 @@
         return IAlertContainer(self.context).values()
 
 
-@adapter_config(name='sorter', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+@adapter_config(name='sorter', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable),
+                provides=IColumn)
 class AlertContainerSorterColumn(SorterColumn):
     """Alert container sorter column"""
 
@@ -119,24 +118,22 @@
     return {'status': 'success'}
 
 
-@adapter_config(name='show-hide', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+@adapter_config(name='show-hide', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable),
+                provides=IColumn)
 class AlertContainerShowHideColumn(VisibilitySwitcherColumn):
     """Alert container visibility switcher column"""
 
 
-@view_config(name='switch-alert-visibility.json', context=IAlertContainer, request_type=IPyAMSLayer,
-             permission=MANAGE_SITE_ROOT_PERMISSION, renderer='json', xhr=True)
+@view_config(name='switch-alert-visibility.json', context=IAlertContainer,
+             request_type=IPyAMSLayer, permission=MANAGE_SITE_ROOT_PERMISSION,
+             renderer='json', xhr=True)
 def switch_alert_visibility(request):
     """Switch 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}
+    return switch_element_visibility(request, IAlertContainer)
 
 
-@adapter_config(name='name', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+@adapter_config(name='name', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable),
+                provides=IColumn)
 class AlertContainerNameColumn(I18nColumn, I18nAttrColumn):
     """Alert container message column"""
 
@@ -151,14 +148,16 @@
         return get_text_start(value, 50, 10)
 
 
-@adapter_config(name='trash', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable), provides=IColumn)
+@adapter_config(name='trash', context=(IAlertTarget, IPyAMSLayer, AlertContainerTable),
+                provides=IColumn)
 class AlertContainerTrashColumn(TrashColumn):
     """Alert container trash column"""
 
     permission = MANAGE_SITE_ROOT_PERMISSION
 
 
-@pagelet_config(name='alerts.html', context=IAlertTarget, layer=IPyAMSLayer, permission=MANAGE_SITE_ROOT_PERMISSION)
+@pagelet_config(name='alerts.html', context=IAlertTarget, layer=IPyAMSLayer,
+                permission=MANAGE_SITE_ROOT_PERMISSION)
 class AlertContainerView(ContainerAdminView):
     """Alerts container view"""