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
+from app.models.mail import Mail, USER_CONFIRM, NEW_SESSION
 
 
 def send_email(to, action, subject, html):
@@ -83,3 +83,19 @@ def send_email_confirmation(email, link):
             email=email, link=link
         )
     )
+
+
+def send_email_new_session(email, event_name, link):
+    """email for new session"""
+    send_email(
+        to=email,
+        action=NEW_SESSION,
+        subject=MAILS[NEW_SESSION]['subject'].format(
+            event_name=event_name
+        ),
+        html=MAILS[NEW_SESSION]['message'].format(
+            email=email,
+            event_name=event_name,
+            link=link
+        )
+    )
diff --git a/app/api/sessions.py b/app/api/sessions.py
--- a/app/api/sessions.py
+++ b/app/api/sessions.py
@@ -8,6 +8,7 @@
 from app.api.events import Event
 from app.api.helpers.utilities import dasherize
 from app.models import db
+from app.settings import get_settings
 from app.models.session import Session
 from app.models.track import Track
 from app.models.speaker import Speaker
@@ -19,6 +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
 
 
 class SessionSchema(Schema):
@@ -128,10 +130,21 @@ def before_post(self, args, kwargs, data):
         require_relationship(['event'], data)
         data['creator_id'] = current_identity.id
 
+    def after_create_object(self, session, data, view_kwargs):
+        if session.event.get_organizer():
+            event_name = session.event.name
+            organizer_email = session.event.get_organizer().email
+            frontend_url = get_settings()['frontend_url']
+            link = "{}/{}/sessions/{}"\
+                .format(frontend_url, session.event_id, session.id)
+            send_email_new_session(organizer_email, event_name, link)
+
     decorators = (api.has_permission('create_event'),)
     schema = SessionSchema
     data_layer = {'session': db.session,
-                  'model': Session}
+                  'model': Session,
+                  'methods': {'after_create_object': after_create_object
+                              }}
 
 
 class SessionList(ResourceList):
diff --git a/app/models/event.py b/app/models/event.py
--- a/app/models/event.py
+++ b/app/models/event.py
@@ -9,7 +9,7 @@
 from app.api.helpers.db import get_count
 from app.models.helpers.versioning import clean_up_string, clean_html
 from app.models.email_notification import EmailNotification
-from app.models.user import ATTENDEE
+from app.models.user import ATTENDEE, ORGANIZER
 from app.models.custom_form import CustomForms, session_form_str, speaker_form_str
 from app.models.version import Version
 from app.models import db
@@ -238,6 +238,13 @@ def notification_settings(self, user_id):
         except:
             return None
 
+    def get_organizer(self):
+        """returns organizer of an event"""
+        for role in self.roles:
+            if role.role.name == ORGANIZER:
+                return role.user
+        return None
+
     def has_staff_access(self, user_id):
         """does user have role other than attendee"""
         access = False


Test patch: