mirror of
https://github.com/lucaspalomodevelop/indico-plugins.git
synced 2026-03-20 02:26:12 +00:00
Add email notifications
This commit is contained in:
parent
f06cfaaa59
commit
2f99f4b972
@ -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:
|
||||
|
||||
87
chat/indico_chat/notifications.py
Normal file
87
chat/indico_chat/notifications.py
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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')
|
||||
@ -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):
|
||||
|
||||
8
chat/indico_chat/templates/emails/added.txt
Normal file
8
chat/indico_chat/templates/emails/added.txt
Normal file
@ -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 %}
|
||||
8
chat/indico_chat/templates/emails/attached.txt
Normal file
8
chat/indico_chat/templates/emails/attached.txt
Normal file
@ -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 %}
|
||||
15
chat/indico_chat/templates/emails/base.txt
Normal file
15
chat/indico_chat/templates/emails/base.txt
Normal file
@ -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 %}
|
||||
8
chat/indico_chat/templates/emails/created.txt
Normal file
8
chat/indico_chat/templates/emails/created.txt
Normal file
@ -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 %}
|
||||
11
chat/indico_chat/templates/emails/deleted.txt
Normal file
11
chat/indico_chat/templates/emails/deleted.txt
Normal file
@ -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 %}
|
||||
8
chat/indico_chat/templates/emails/modified.txt
Normal file
8
chat/indico_chat/templates/emails/modified.txt
Normal file
@ -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 %}
|
||||
Loading…
x
Reference in New Issue
Block a user