Repository: fossasia/open-event-server

Issue:
Email for session submission, approval and rejection
Parent Issue #4196

Emails for following are to be done in this issue:

- [x] Email for session submission to Event Organizer
- [x] Email for session acceptance/rejection to Speaker
- [x] Email for session acceptance/rejection to Organizer


Discussion:

Resolving patch:
diff --git a/app/api/helpers/mail.py b/app/api/helpers/mail.py
--- a/app/api/helpers/mail.py
+++ b/app/api/helpers/mail.py
@@ -7,7 +7,7 @@
 from app.api.helpers.log import record_activity
 from app.api.helpers.system_mails import MAILS
 from app.api.helpers.utilities import string_empty
-from app.models.mail import Mail, USER_CONFIRM, NEW_SESSION
+from app.models.mail import Mail, USER_CONFIRM, NEW_SESSION, SESSION_ACCEPT_REJECT
 
 
 def send_email(to, action, subject, html):
@@ -99,3 +99,23 @@ def send_email_new_session(email, event_name, link):
             link=link
         )
     )
+
+
+def send_email_session_accept_reject(email, session, link):
+    """email for new session"""
+    session_name = session.title
+    session_acceptance = session.state
+    send_email(
+        to=email,
+        action=SESSION_ACCEPT_REJECT,
+        subject=MAILS[SESSION_ACCEPT_REJECT]['subject'].format(
+            session_name=session_name,
+            acceptance=session_acceptance
+        ),
+        html=MAILS[SESSION_ACCEPT_REJECT]['message'].format(
+            email=email,
+            session_name=session_name,
+            acceptance=session_acceptance,
+            link=link
+        )
+    )
diff --git a/app/api/sessions.py b/app/api/sessions.py
--- a/app/api/sessions.py
+++ b/app/api/sessions.py
@@ -20,7 +20,7 @@
 from app.api.helpers.permission_manager import has_access
 from app.api.helpers.exceptions import ForbiddenException
 from app.api.helpers.permissions import current_identity
-from app.api.helpers.mail import send_email_new_session
+from app.api.helpers.mail import send_email_new_session, send_email_session_accept_reject
 
 
 class SessionSchema(Schema):
@@ -48,6 +48,9 @@ def validate_date(self, data, original_data):
             if 'ends_at' not in data:
                 data['ends_at'] = session.ends_at
 
+            if 'event' not in data:
+                data['event'] = session.event_id
+
         if data['starts_at'] >= data['ends_at']:
             raise UnprocessableEntity({'pointer': '/data/attributes/ends-at'}, "ends-at should be after starts-at")
 
@@ -135,7 +138,7 @@ def after_create_object(self, session, data, view_kwargs):
             event_name = session.event.name
             organizer_email = session.event.get_organizer().email
             frontend_url = get_settings()['frontend_url']
-            link = "{}/{}/sessions/{}"\
+            link = "{}/events/{}/sessions/{}"\
                 .format(frontend_url, session.event_id, session.id)
             send_email_new_session(organizer_email, event_name, link)
 
@@ -194,11 +197,32 @@ def before_get_object(self, view_kwargs):
             event = safe_query(self, Event, 'identifier', view_kwargs['event_identifier'], 'identifier')
             view_kwargs['event_id'] = event.id
 
+    def after_update_object(self, session, data, view_kwargs):
+        """ Send email if session accepted or rejected """
+        if 'state' in data and (session.state == 'accepted' or session.state == 'rejected'):
+            # Email for speaker
+            speakers = session.speakers
+            for speaker in speakers:
+                frontend_url = get_settings()['frontend_url']
+                link = "{}/events/{}/sessions/{}" \
+                    .format(frontend_url, session.event_id, session.id)
+                send_email_session_accept_reject(speaker.email, session, link)
+
+            # Email for organizer
+            if session.event.get_organizer():
+                organizer_email = session.event.get_organizer().email
+                frontend_url = get_settings()['frontend_url']
+                link = "{}/events/{}/sessions/{}" \
+                    .format(frontend_url, session.event_id, session.id)
+                send_email_session_accept_reject(organizer_email, session,
+                                                 link)
+
     decorators = (api.has_permission('is_speaker_for_session', methods="PATCH,DELETE"),)
     schema = SessionSchema
     data_layer = {'session': db.session,
                   'model': Session,
-                  'methods': {'before_get_object': before_get_object}}
+                  'methods': {'before_get_object': before_get_object,
+                              'after_update_object': after_update_object}}
 
 
 class SessionRelationshipRequired(ResourceRelationship):


Test patch: