From 2f99f4b97250c29317ab56f259dc747ef7d78e6e Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Thu, 9 Oct 2014 11:22:55 +0200 Subject: [PATCH] Add email notifications --- chat/indico_chat/controllers/management.py | 5 ++ chat/indico_chat/notifications.py | 87 +++++++++++++++++++ chat/indico_chat/plugin.py | 6 +- chat/indico_chat/templates/emails/added.txt | 8 ++ .../indico_chat/templates/emails/attached.txt | 8 ++ chat/indico_chat/templates/emails/base.txt | 15 ++++ chat/indico_chat/templates/emails/created.txt | 8 ++ chat/indico_chat/templates/emails/deleted.txt | 11 +++ .../indico_chat/templates/emails/modified.txt | 8 ++ 9 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 chat/indico_chat/notifications.py create mode 100644 chat/indico_chat/templates/emails/added.txt create mode 100644 chat/indico_chat/templates/emails/attached.txt create mode 100644 chat/indico_chat/templates/emails/base.txt create mode 100644 chat/indico_chat/templates/emails/created.txt create mode 100644 chat/indico_chat/templates/emails/deleted.txt create mode 100644 chat/indico_chat/templates/emails/modified.txt diff --git a/chat/indico_chat/controllers/management.py b/chat/indico_chat/controllers/management.py index 9eabb69..638fc2c 100644 --- a/chat/indico_chat/controllers/management.py +++ b/chat/indico_chat/controllers/management.py @@ -30,6 +30,7 @@ from indico.web.forms.base import FormDefaults from indico_chat.controllers.base import RHChatManageEventBase, RHEventChatroomMixin from indico_chat.forms import AddChatroomForm, EditChatroomForm from indico_chat.models.chatrooms import ChatroomEventAssociation, Chatroom +from indico_chat.notifications import notify_created, notify_attached, notify_modified, notify_deleted from indico_chat.views import WPChatEventMgmt from indico_chat.xmpp import create_room, update_room, get_room_config, room_exists @@ -66,6 +67,7 @@ class RHChatManageEventModify(RHEventChatroomMixin, RHChatManageEventBase): self.chatroom.modified_dt = now_utc() if attrs_changed(self.chatroom, 'name', 'description', 'password'): update_room(self.chatroom) + notify_modified(self.chatroom, self.event, session.user) flash(_('Chatroom updated'), 'success') return redirect(url_for_plugin('.manage_rooms', self.event)) return WPChatEventMgmt.render_template('manage_event_edit.html', self._conf, form=form, @@ -113,6 +115,7 @@ class RHChatManageEventCreate(RHChatManageEventBase): db.session.add_all((chatroom, event_chatroom)) db.session.flush() create_room(chatroom) + notify_created(chatroom, self.event, session.user) flash(_('Chatroom created'), 'success') return redirect(url_for_plugin('.manage_rooms', self.event)) return WPChatEventMgmt.render_template('manage_event_edit.html', self._conf, form=form) @@ -128,6 +131,7 @@ class RHChatManageEventAttach(RHChatManageEventBase): def _process(self): event_chatroom = ChatroomEventAssociation(event_id=self.event_id, chatroom=self.chatroom) db.session.add(event_chatroom) + notify_attached(self.chatroom, self.event, session.user) flash(_('Chatroom added'), 'success') return redirect(url_for_plugin('.manage_rooms', self.event)) @@ -142,6 +146,7 @@ class RHChatManageEventRemove(RHEventChatroomMixin, RHChatManageEventBase): def _process(self): reason = '{} has requested to delete this room.'.format(unicode(session.user.getStraightFullName(), 'utf-8')) chatroom_deleted = self.event_chatroom.delete(reason) + notify_deleted(self.chatroom, self.event, session.user, chatroom_deleted) if chatroom_deleted: flash(_('Chatroom deleted'), 'success') else: diff --git a/chat/indico_chat/notifications.py b/chat/indico_chat/notifications.py new file mode 100644 index 0000000..6a224af --- /dev/null +++ b/chat/indico_chat/notifications.py @@ -0,0 +1,87 @@ +# This file is part of Indico. +# Copyright (C) 2002 - 2014 European Organization for Nuclear Research (CERN). +# +# Indico is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# Indico is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Indico; if not, see . + +from __future__ import unicode_literals + +from indico.core.config import Config +from indico.core.plugins import get_plugin_template_module +from MaKaC.common.mail import GenericMailer +from MaKaC.webinterface.mail import GenericNotification + +from indico_chat.util import get_chat_admins + + +def notify_created(room, event, user): + """Notifies about the creation of a chatroom. + + :param room: the chatroom + :param event: the event + :param user: the user performing the action + """ + tpl = get_plugin_template_module('emails/created.txt', chatroom=room, event=event, user=user) + _send(event, tpl.get_subject(), tpl.get_body()) + + +def notify_attached(room, event, user): + """Notifies about an existing chatroom being attached to an event. + + :param room: the chatroom + :param event: the event + :param user: the user performing the action + """ + tpl = get_plugin_template_module('emails/attached.txt', chatroom=room, event=event, user=user) + _send(event, tpl.get_subject(), tpl.get_body()) + + +def notify_modified(room, event, user): + """Notifies about the modification of a chatroom. + + :param room: the chatroom + :param event: the event + :param user: the user performing the action + """ + tpl = get_plugin_template_module('emails/modified.txt', chatroom=room, event=event, user=user) + _send(event, tpl.get_subject(), tpl.get_body()) + + +def notify_deleted(room, event, user, room_deleted): + """Notifies about the deletion of a chatroom. + + :param room: the chatroom + :param event: the event + :param user: the user performing the action; `None` if due to event deletion + :param room_deleted: if the room has been deleted from the jabber server + """ + tpl = get_plugin_template_module('emails/deleted.txt', chatroom=room, event=event, user=user, + room_deleted=room_deleted) + _send(event, tpl.get_subject(), tpl.get_body()) + + +def _send(event, subject, body): + from indico_chat.plugin import ChatPlugin + + to_list = set(ChatPlugin.settings.get('notify_emails')) + if ChatPlugin.settings.get('notify_admins'): + to_list |= {u.getEmail() for u in get_chat_admins()} + if not to_list: + return + notification = { + 'fromAddr': Config.getInstance().getNoReplyEmail(), + 'toList': to_list, + 'subject': subject, + 'body': body + } + GenericMailer.sendAndLog(GenericNotification(notification), event, 'chat') diff --git a/chat/indico_chat/plugin.py b/chat/indico_chat/plugin.py index c350c73..3326153 100644 --- a/chat/indico_chat/plugin.py +++ b/chat/indico_chat/plugin.py @@ -37,6 +37,7 @@ from MaKaC.webinterface.wcomponents import SideMenuItem from indico_chat.blueprint import blueprint from indico_chat.models.chatrooms import ChatroomEventAssociation +from indico_chat.notifications import notify_deleted from indico_chat.views import WPChatEventPage, WPChatEventMgmt @@ -86,6 +87,8 @@ class ChatPlugin(IndicoPlugin): @property def default_settings(self): return {'admins': [], + 'notify_admins': False, + 'notify_emails': [], 'how_to_connect': render_plugin_template('how_to_connect.html'), 'chat_links': [{'title': 'Desktop Client', 'link': 'xmpp:{room}@{server}?join'}]} @@ -130,7 +133,8 @@ class ChatPlugin(IndicoPlugin): def event_deleted(self, event, **kwargs): for event_chatroom in ChatroomEventAssociation.find_for_event(event, include_hidden=True): - event_chatroom.delete() + chatroom_deleted = event_chatroom.delete() + notify_deleted(event_chatroom.chatroom, event, None, chatroom_deleted) class ChatroomCloner(EventCloner): diff --git a/chat/indico_chat/templates/emails/added.txt b/chat/indico_chat/templates/emails/added.txt new file mode 100644 index 0000000..af89e77 --- /dev/null +++ b/chat/indico_chat/templates/emails/added.txt @@ -0,0 +1,8 @@ +{% extends 'chat:emails/base.txt' %} + +{% block action %}added{% endblock %} + +{% block details -%} +You can find details on the following page: +{{ url_for_plugin('.manage_rooms', event, _external=true) }} +{%- endblock %} diff --git a/chat/indico_chat/templates/emails/attached.txt b/chat/indico_chat/templates/emails/attached.txt new file mode 100644 index 0000000..4f8419e --- /dev/null +++ b/chat/indico_chat/templates/emails/attached.txt @@ -0,0 +1,8 @@ +{% extends 'chat:emails/base.txt' %} + +{% block action %}attached{% endblock %} + +{% block details -%} +You can find details on the following page: +{{ url_for_plugin('.manage_rooms', event, _external=true) }} +{%- endblock %} diff --git a/chat/indico_chat/templates/emails/base.txt b/chat/indico_chat/templates/emails/base.txt new file mode 100644 index 0000000..cf0a59d --- /dev/null +++ b/chat/indico_chat/templates/emails/base.txt @@ -0,0 +1,15 @@ +{% macro get_subject() -%} + [Indico] [Chat] Chatroom {% block action %}{% endblock %}: '{{ chatroom.name }}' in '{{ event.getTitle() }}' +{%- endmacro %} + +{% macro get_body() -%} +A chatroom has been {{ self.action() }}{% if user %} by {{ user.getStraightFullName() }}{% endif %}: + +Chatroom: {{ chatroom.name }} +Event: {{ event.getTitle() }} + +{% block details %}{% endblock %} + +The event can be found here: +{{ event.getURL() }} +{%- endmacro %} diff --git a/chat/indico_chat/templates/emails/created.txt b/chat/indico_chat/templates/emails/created.txt new file mode 100644 index 0000000..371928d --- /dev/null +++ b/chat/indico_chat/templates/emails/created.txt @@ -0,0 +1,8 @@ +{% extends 'chat:emails/base.txt' %} + +{% block action %}created{% endblock %} + +{% block details -%} +You can find details on the following page: +{{ url_for_plugin('.manage_rooms', event, _external=true) }} +{%- endblock %} diff --git a/chat/indico_chat/templates/emails/deleted.txt b/chat/indico_chat/templates/emails/deleted.txt new file mode 100644 index 0000000..2b9fd0c --- /dev/null +++ b/chat/indico_chat/templates/emails/deleted.txt @@ -0,0 +1,11 @@ +{% extends 'chat:emails/base.txt' %} + +{% block action %}removed{% endblock %} + +{% block details -%} + {%- if room_deleted -%} + The room has also been deleted from the Jabber server. + {%- else -%} + The room still exists on the Jabber server as it is used by other events. + {%- endif -%} +{%- endblock %} diff --git a/chat/indico_chat/templates/emails/modified.txt b/chat/indico_chat/templates/emails/modified.txt new file mode 100644 index 0000000..76b6621 --- /dev/null +++ b/chat/indico_chat/templates/emails/modified.txt @@ -0,0 +1,8 @@ +{% extends 'chat:emails/base.txt' %} + +{% block action %}modified{% endblock %} + +{% block details -%} +You can find details on the following page: +{{ url_for_plugin('.manage_rooms', event, _external=true) }} +{%- endblock %}