Module: check_mk
Branch: master
Commit: 012c87332446c9a8102111c68598ddb59402f757
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=012c87332446c9…
Author: Simon Betz <si(a)mathias-kettner.de>
Date: Mon Jun 12 11:04:29 2017 +0200
4783 User notifications can now temporarily disabled wihtin configured timerange
Change-Id: I13dc3ba4fe735e1df151030f45df5a2d70234235
---
.werks/4783 | 12 ++++++++++
modules/notify.py | 44 ++++++++++++++++++++++++++++++-----
web/htdocs/default_permissions.py | 2 +-
web/htdocs/wato.py | 8 ++++++-
web/plugins/userdb/user_attributes.py | 24 +++++++++++++++----
5 files changed, 77 insertions(+), 13 deletions(-)
diff --git a/.werks/4783 b/.werks/4783
new file mode 100644
index 0000000..5fe2dcc
--- /dev/null
+++ b/.werks/4783
@@ -0,0 +1,12 @@
+Title: User notifications can now temporarily disabled within configured timerange
+Level: 1
+Component: notifications
+Compatible: compat
+Edition: cre
+Version: 1.5.0i1
+Date: 1497258136
+Class: feature
+
+So far users could disable notifications "forever" within their user profile.
+Now they have additionaly the possibility to customize a timerange where
+notifications are disabled.
diff --git a/modules/notify.py b/modules/notify.py
index 2157a08..57380eb 100644
--- a/modules/notify.py
+++ b/modules/notify.py
@@ -117,6 +117,24 @@ $LONGSERVICEOUTPUT$
"""
#.
+# .--helper--------------------------------------------------------------.
+# | _ _ |
+# | | |__ ___| |_ __ ___ _ __ |
+# | | '_ \ / _ \ | '_ \ / _ \ '__| |
+# | | | | | __/ | |_) | __/ | |
+# | |_| |_|\___|_| .__/ \___|_| |
+# | |_| |
+# '----------------------------------------------------------------------'
+
+
+def _transform_user_disable_notifications_opts(contact):
+ if "disable_notifications" in contact and
type(contact["disable_notifications"]) == bool:
+ return {"disable" : contact["disable_notifications"]}
+ else:
+ return contact.get("disable_notifications", {})
+
+
+#.
# .--Main----------------------------------------------------------------.
# | __ __ _ |
# | | \/ | __ _(_)_ __ |
@@ -291,9 +309,16 @@ def locally_deliver_raw_context(raw_context, analyse=False):
# flexible notifications even if they are enabled.
contact = contacts.get(contactname)
- if contact.get("disable_notifications", False):
- notify_log("Notifications for %s are disabled in personal settings.
Skipping." % contactname)
- return
+ disable_notifications_opts = _transform_user_disable_notifications_opts(contact)
+ if disable_notifications_opts.get("disable", False):
+ start, end = disable_notifications_opts.get("timerange", (None,
None))
+ if start is None or end is None:
+ notify_log("Notifications for %s are disabled in personal settings.
Skipping." % contactname)
+ return
+ elif start <= time.time() <= end:
+ notify_log("Notifications for %s are disabled in personal settings
from %s to %s. Skipping." % \
+ (contactname, start, end))
+ return
# Get notification settings for the contact in question - if available.
if contact:
@@ -691,9 +716,16 @@ def rbn_rule_contacts(rule, context):
contact = contacts.get(contactname)
if contact:
- if contact.get("disable_notifications", False):
- notify_log(" - skipping contact %s: he/she has disabled
notifications" % contactname)
- continue
+ disable_notifications_opts =
_transform_user_disable_notifications_opts(contact)
+ if disable_notifications_opts.get("disable", False):
+ start, end = disable_notifications_opts.get("timerange", (None,
None))
+ if start is None or end is None:
+ notify_log(" - skipping contact %s: he/she has disabled
notifications" % contactname)
+ continue
+ elif start <= time.time() <= end:
+ notify_log(" - skipping contact %s: he/she has disabled
notifications from %s to %s." % \
+ (contactname, start, end))
+ continue
reason = rbn_match_contact_macros(rule, contactname, contact) or \
rbn_match_contact_groups(rule, contactname, contact)
diff --git a/web/htdocs/default_permissions.py b/web/htdocs/default_permissions.py
index eeec1d7..4fe4b53 100644
--- a/web/htdocs/default_permissions.py
+++ b/web/htdocs/default_permissions.py
@@ -107,7 +107,7 @@ def load_plugins(force):
config.declare_permission('general.disable_notifications',
_('Disable all personal notifications'),
- _('This permissions provides a checkbox in the personal settings of the user
that '
+ _('This permissions provides a checkbox and timerange in the personal
settings of the user that '
'allows him to completely disable all of his notifications. Use with
caution.'),
[ 'admin', ]
)
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index a7b7333..1321f77 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -10331,7 +10331,13 @@ def mode_users(phase):
locked = user.get("locked", False)
if user.get("locked", False):
html.icon(_('The login is currently locked'), 'user_locked')
- if user.get("disable_notifications", False):
+
+ if "disable_notifications" in user and
type(user["disable_notifications"]) == bool:
+ disable_notifications_opts = {"disable" :
user["disable_notifications"]}
+ else:
+ disable_notifications_opts = user.get("disable_notifications", {})
+
+ if disable_notifications_opts.get("disable", False):
html.icon(_('Notifications are disabled'), 'notif_disabled')
# Full name / Alias
diff --git a/web/plugins/userdb/user_attributes.py
b/web/plugins/userdb/user_attributes.py
index 9d8cc28..da46cd9 100644
--- a/web/plugins/userdb/user_attributes.py
+++ b/web/plugins/userdb/user_attributes.py
@@ -52,16 +52,30 @@ declare_user_attribute(
)
+def transform_disable_notification(p):
+ if type(p) is not dict:
+ return {"disable": True}
+ else:
+ return p
+
+
declare_user_attribute(
"disable_notifications",
- Checkbox(
- title = _("Disable Notifications"),
- label = _("Temporarily disable <b>all</b>
notifications!"),
+ Transform(Dictionary(
+ title=_("Disable Notifications"),
help = _("When this option is active the you will not get
<b>any</b> "
"alerts or other notifications via email, SMS or similar. "
"This overrides all other notification settings or rules, so make
"
- "sure that you know what you do."),
- ),
+ "sure that you know what you do. Moreover you can customize a
timerange "
+ "within no notifications are generated."),
+ elements = [
+ ("disable", Checkbox(title=_("Temporarily disable
<b>all</b> notifications!"), label=_("Disable"))),
+ ("timerange", Tuple(title=_("Customize timerange"),
elements = [
+ AbsoluteDate(title=_("From:"), include_time=True),
+ AbsoluteDate(title=_("To:"), include_time=True),
+ ]))
+ ],
+ ), forth = transform_disable_notification),
permission = "general.disable_notifications",
domain = "check_mk",
)