Repository: fossasia/open-event-server

Issue:
Add recipients, subject and message property to message_setting schema
**Is your feature request related to a problem? Please describe.**
The current schema doesn't include recipients, subject and message properties. These should be included as these are required to display in the frontend.

**Describe the solution you'd like**
Add it as properties in the schema.

**Additional context**
```python
MAILS = {
    EVENT_PUBLISH: {
        'recipient': 'Organizer, Speaker',
        'subject': u'{event_name} is Live',
        'message': (
            u"Hi {email}<br/>" +
            u"Event, {event_name}, is up and running and ready for action. Go ahead and check it out." +
            u"<br/> Visit this link to view it: {link}"
        )
    },
    INVITE_PAPERS: {
        'recipient': 'Speaker',
        'subject': u'Invitation to Submit Papers for {event_name}',
        'message': (
            u"Hi {email}<br/>" +
            u"You are invited to submit papers for event: {event_name}" +
            u"<br/> Visit this link to fill up details: {link}"
        )
    },
    SESSION_ACCEPT_REJECT: {
        'recipient': 'Speaker',
        'subject': u'Session {session_name} has been {acceptance}',
        'message': (
            u"Hi {email},<br/>" +
            u"The session <strong>{session_name}</strong> has been <strong>{acceptance}</strong> by the organizer. " +
            u"<br/> Visit this link to view the session: {link}"
        )
    },
    SESSION_SCHEDULE: {
        'recipient': 'Organizer, Speaker',
        'subject': u'Schedule for Session {session_name} has been changed',
        'message': (
            u"Hi {email},<br/>" +
            u"The schedule for session <strong>{session_name}</strong> has been changed. " +
            u"<br/> Visit this link to view the session: {link}"
        )
    },
    NEXT_EVENT: {
        'recipient': 'Organizer, Speaker',
        'subject': u'Event {event_name} is coming soon',
        'message': (
            u"Hi {email},<br/>" +
            u"Here are the upcoming events: {up_coming_events} .Get ready!! " +
            u"<br/> Visit this link to view the event: {link}"
        )
    },
    AFTER_EVENT: {
        'recipient': 'Organizer, Speaker',
        'subject': u'Event {event_name} is over',
        'message': (
            u"Hi {email},<br/>" +
            u"Thank You for participating in our event. We hope you enjoyed it. "
            u"Please check the list of more upcoming events" +
            u"Here are the upcoming events: {upcoming_events} .Get ready!! "
        ),
        'sent_at': '1 day after the event'
    },
    NEW_SESSION: {
        'recipient': 'Organizer',
        'subject': u'New session proposal for {event_name}',
        'message': (
            u"Hi {email},<br/>" +
            u"The event <strong>{event_name}</strong> has received a new session proposal. " +
            u"<br/> Visit this link to view the session: {link}"
        )
    },
    USER_REGISTER: {
        'recipient': 'User',
        'subject': u'Account Created on {app_name}',
        'message': (
            u"Your Account Has Been Created! Congratulations!" +
            u"<br/> Your login: {email}"
        )
    },
    USER_REGISTER_WITH_PASSWORD: {
        'recipient': 'User',
        'subject': u'Welcome to {app_name}',
        'message': (
            u"Your Account Has Been Created! Congratulations!" +
            u"<br/> <strong>Your login:</strong><br><strong>Email:</strong> {email}<br>"
        )
    },
    USER_CONFIRM: {
        'recipient': 'User',
        'subject': u'Email Confirmation to Create Account for Open-Event',
        'message': (
            u"Hi {email},<br/>" +
            u"Please visit this link to confirm your email: {link}"
        )
    },
    USER_CHANGE_EMAIL: {
        'recipient': 'User',
        'subject': u'Your email has been already changed',
        'message': (
            u"Hi {email},<br/>" +
            u"Your email has been already changed from {email} to {new_email}. You should verify your new email"
        )
    },
    PASSWORD_RESET: {
        'recipient': 'User',
        'subject': u'{app_name}: Password Reset',
        'message': (
            u"Please use the following link to reset your password.<br> <a href='{link}' target='_blank'>{link}</a>"
        )
    },
    PASSWORD_CHANGE: {
        'recipient': 'User',
        'subject': u'{app_name}: Password Change',
        'message': (
            u"Your password has been successfully changed. Please login with your new password."
        )
    },
    EVENT_ROLE: {
        'recipient': 'User',
        'subject': u'Invitation to be {role} at {event}',
        'message': (
            u"Hello {email},<br><br>" +
            u"You've been invited to be a <strong>{role}</strong> at <strong>{event}</strong>.<br>" +
            u"To accept the role please sign up using the following link: <a href='{link}' target='_blank'>Link</a>."
        )
    },
    TICKET_PURCHASED: {
        'recipient': 'User',
        'subject': u'Your order invoice and tickets for {event_name} ({invoice_id}) ',
        'message': (
            u"Hi, this is a confirmation mail of your tickets for the event {event_name}"
            u"<br/>Your order has been processed successfully." +
            u"<br/> <a href='{pdf_url}'>Click here</a> to view/download your invoice."
            u"<br><br><em>Looking forward to seeing you at the event."
            u"<br/>Login to manage your orders at https://eventyay.com </em>"
        )
    },
    TICKET_PURCHASED_ATTENDEE: {
        'recipient': 'Attendee',
        'subject': u'Your tickets for {event_name} ({invoice_id}) ',
        'message': (
            u"Hi, this is a confirmation mail of your tickets for the event {event_name}"
            u"<br/>Your order has been processed successfully." +
            u"<br/> <a href='{pdf_url}'>Click here</a> to view/download your ticket."
            u"<br><br><em>Looking forward to seeing you at the event."
        )
    },

    TICKET_PURCHASED_ORGANIZER: {
        'recipient': 'Organizer, Coorganizer',
        'subject': u'New ticket purchase for {event_name} by {buyer_email} ({invoice_id}) ',
        'message': (
            u"Hi, {buyer_email} just bought tickets for the event {event_name}"
            u"<br/>The order has been processed successfully." +
            u"<br/> <a href='{order_url}'>Click here</a> to view/download the invoice."
            u"<br/>Login to manage the orders at https://eventyay.com </em>"
        )
    },
    TICKET_CANCELLED: {
        'recipient': 'User',
        'subject': u'Your order for {event_name} has been cancelled ({invoice_id})',
        'message': (
            u"Hi,Your order for {event_name} has been cancelled has been cancelled by the organizer"
            u"<br/>Please contact the organizer for more info" +
            u"<br/>Message from the organizer: {cancel_note}"
            u"<br/> <a href='{order_url}'>Click here</a> to view/download the invoice."
            u"<br/>Login to manage the orders at https://eventyay.com </em>"
        )
    },
    EVENT_EXPORTED: {
        'recipient': 'User',
        'subject': u'Event {event_name} has been exported',
        'message': (
            u"Click on the following link to download the event." +
            u"<br> <a href='{download_url}'>Download</a>"
        )
    },
    EVENT_EXPORT_FAIL: {
        'recipient': 'User',
        'subject': u'Export of event {event_name} failed',
        'message': (
            u"The error was as follows - <br>" +
            u"<pre>{error_text}</pre>"
        )
    },
    MAIL_TO_EXPIRED_ORDERS: {
        'recipient': 'User',
        'subject': u'Tickets for {event_name} are still available ',
        'message': (
            u"This is just a gentle reminder that the payment for your order {invoice_id} is still left." +
            u"<br/> The tickets for this event are still available. <a href='{order_url}'>Click here</a> to "
            u"purchase your ticket for this event."
            u"<br><br><em>Looking forward to seeing you at the event.</em>"
        )
    },
    MONTHLY_PAYMENT_EMAIL: {
        'recipient': 'Organizer',
        'subject': u'{date} - Monthly service fee invoice for {event_name}',
        'message': (
            u"The total service fee for the ticket sales of {event_name} in the month of {date} is {amount}." +
            u"<br/> That payment for the same has to be made in two weeks. <a href='{payment_url}'>Click here</a> to "
            u"view your invoice and complete the payment."
            u"<br><br><em>Thank you for using {app_name}.</em>"
        ),
        'sent_at': '1st day of the month'
    },
    MONTHLY_PAYMENT_FOLLOWUP_EMAIL: {
        'recipient': 'Organizer',
        'subject': u'Past Due: {date} - Monthly service fee invoice for {event_name}',
        'message': (
            u"The total service fee for the ticket sales of {event_name} in the month of {date} is {amount}." +
            u"<br/> That payment for the same is past the due date. <a href='{payment_url}'>Click here</a> to "
            u"view your invoice and complete the payment to prevent loss of functionality."
            u"<br><br><em>Thank you for using {app_name}.</em>"
        ),
        'sent_at': '15th day of the month'
    },
    EVENT_IMPORTED: {
        'recipient': 'User',
        'subject': u'Event {event_name} has been imported',
        'message': (
            u"Click on the following link to manage your event" +
            u"<br> <a href='{event_url}'>Link</a>"
        )
    },
    EVENT_IMPORT_FAIL: {
        'recipient': 'User',
        'subject': u'Import of event failed',
        'message': (
            u"The error was as follows - <br>" +
            u"<pre>{error_text}</pre>"
        )
    }
```


Discussion:

Resolving patch:
diff --git a/app/api/helpers/system_notifications.py b/app/api/helpers/system_notifications.py
--- a/app/api/helpers/system_notifications.py
+++ b/app/api/helpers/system_notifications.py
@@ -31,7 +31,7 @@
         'recipient': 'User',
         'title': u'Event {event_name} has been exported',
         'message': (
-            u"Event <strong>{event_name}</strong> has been exported successfully."
+            u"Event <strong>{event_name}</strong> has been exported successfully." +
             u"<br><br><a href='{download_url}' class='btn btn-info btn-sm'>Download</a>"
         )
     },
@@ -39,7 +39,7 @@
         'recipient': 'User',
         'title': u'Export of event {event_name} failed',
         'message': (
-            u"The following error occurred:<br>"
+            u"The following error occurred:<br>" +
             u"<pre>{error_text}</pre>"
         )
     },
@@ -47,7 +47,7 @@
         'recipient': 'User',
         'title': u'Event {event_name} has been imported',
         'message': (
-            u"Event <strong>{event_name}</strong> has been imported successfully."
+            u"Event <strong>{event_name}</strong> has been imported successfully." +
             u"<br><br><a href='{event_url}' class='btn btn-info btn-sm'>View Event</a>"
         )
     },
@@ -55,7 +55,7 @@
         'recipient': 'User',
         'title': u'Import of event failed',
         'message': (
-            u"The following error occurred:<br>"
+            u"The following error occurred:<br>" +
             u"<pre>{error_text}</pre>"
         )
     },
@@ -64,8 +64,8 @@
         'subject': u'{date} - Monthly service fee invoice for {event_name}',
         'message': (
             u"The total service fee for the ticket sales of {event_name} in the month of {date} is {amount}." +
-            u"<br/> That payment for the same has to be made in two weeks. <a href='{payment_url}'>Click here</a> to "
-            u"view your invoice and complete the payment."
+            u"<br/> That payment for the same has to be made in two weeks. <a href='{payment_url}'>Click here</a> to " +
+            u"view your invoice and complete the payment." +
             u"<br><br><em>Thank you for using {app_name}.</em>"
         ),
         'sent_at': '1st day of the month'
@@ -75,8 +75,8 @@
         'subject': u'Past Due: {date} - Monthly service fee invoice for {event_name}',
         'message': (
             u"The total service fee for the ticket sales of {event_name} in the month of {date} is {amount}." +
-            u"<br/> That payment for the same is past the due date. <a href='{payment_url}'>Click here</a> to "
-            u"view your invoice and complete the payment to prevent loss of functionality."
+            u"<br/> That payment for the same is past the due date. <a href='{payment_url}'>Click here</a> to " +
+            u"view your invoice and complete the payment to prevent loss of functionality." +
             u"<br><br><em>Thank you for using {app_name}.</em>"
         ),
         'sent_at': '15th day of the month'
@@ -85,7 +85,7 @@
         'recipient': 'User',
         'title': u'Your order invoice and tickets ({invoice_id})',
         'message': (
-            u"Your order has been processed successfully."
+            u"Your order has been processed successfully." +
             u"<br><br><a href='{order_url}' class='btn btn-info btn-sm'>View Invoice</a>"
         )
     },
@@ -93,7 +93,7 @@
         'recipient': 'User',
         'title': u'Your ticket for {event_name}',
         'message': (
-            u"Your order has been processed successfully."
+            u"Your order has been processed successfully." +
             u"<br><br><a href='{pdf_url}' class='btn btn-info btn-sm'>View PDF</a>"
         )
     },
@@ -101,7 +101,7 @@
         'recipient': 'Organizer',
         'title': u'New ticket purchase for {event_name} : ({invoice_id}) ',
         'message': (
-            u"The order has been processed successfully."
+            u"The order has been processed successfully." +
             u"<br><br><a href='{order_url}' class='btn btn-info btn-sm'>View Invoice</a>"
         )
     },
@@ -116,7 +116,7 @@
         'recipient': 'User',
         'title': u'Your order for {event_name} has been cancelled ({invoice_id})',
         'message': (
-            u"Your order for {event_name} has been cancelled by the organizer"
+            u"Your order for {event_name} has been cancelled by the organizer" +
             u"<br/>Please contact the organizer for more info" +
             u"<br/>Message from the organizer: {cancel_note}."
         )
@@ -125,7 +125,7 @@
         'recipient': 'User',
         'title': u'Order ({invoice_id}) has been cancelled',
         'message': (
-            u"Order ({invoice_id}) has been cancelled"
+            u"Order ({invoice_id}) has been cancelled" +
             u"<br/>Cancel Note: {cancel_note}."
         )
     },
@@ -145,54 +145,55 @@
     },
     AFTER_EVENT: {
         'title': u'Event {event_name} completed',
-        'message': u"""The event <strong>{event_name}</strong> has been completed.<br><br>""",
+        'message': u"The event <strong>{event_name}</strong> has been completed.<br><br>",
         'recipient': 'User',
     },
     EVENT_PUBLISH: {
         'title': u'Event {event_name} has been published',
-        'message': u"""The event <strong>{event_name}</strong> has been published.<br><br>
-                   <a href='{link}' class='btn btn-info btn-sm'>View Event</a>""",
+        'message': u"The event <strong>{event_name}</strong> has been published.<br><br>" +
+                   u"<a href='{link}' class='btn btn-info btn-sm'>View Event</a>",
         'recipient': 'User',
     },
     EVENT_ROLE: {
         'title': u'Invitation to be {role_name} at {event_name}',
-        'message': u"""You've been invited to be a <strong>{role_name}</strong>
-            at <strong>{event_name}</strong>.<br><br>
-            To accept the role please sign up using the following link: <a href='{link}' target='_blank'>Link</a>.""",
+        'message': u"You've been invited to be a <strong>{role_name}</strong>" +
+                   u"at <strong>{event_name}</strong>.<br><br>" +
+                   u"To accept the role please sign up using the following link: " +
+                   u"<a href='{link}' target='_blank'>Link</a>.",
         'recipient': 'User',
     },
     NEW_SESSION: {
         'title': u'New session proposal for {event_name}',
-        'message': u"""The event <strong>{event_name}</strong> has received
-             a new session proposal.<br><br>
-            <a href='{link}' class='btn btn-info btn-sm'>View Session</a>""",
+        'message': u"The event <strong>{event_name}</strong> has received" +
+                   u"a new session proposal.<br><br>" +
+                   u"<a href='{link}' class='btn btn-info btn-sm'>View Session</a>",
         'recipient': 'Organizer',
     },
     SESSION_SCHEDULE: {
         'title': u'Schedule for Session {session_name} has been changed',
-        'message': u"""The schedule for session <strong>{session_name}</strong>
-             has been changed.<br><br>
-            <a href='{link}' class='btn btn-info btn-sm'>View Session</a>""",
+        'message': u"The schedule for session <strong>{session_name}</strong>" +
+                   u"has been changed.<br><br>" +
+                   u"<a href='{link}' class='btn btn-info btn-sm'>View Session</a>",
         'recipient': 'Organizer, Speaker',
     },
     NEXT_EVENT: {
         'title': u'Event {event_name} is coming soon',
-        'message': u"""Here are upcoming events: {up_coming_events}.<br><br>
-            <a href='{link}' class='btn btn-info btn-sm'>View Event</a>""",
+        'message': u"Here are upcoming events: {up_coming_events}.<br><br>" +
+                   u"<a href='{link}' class='btn btn-info btn-sm'>View Event</a>",
         'recipient': 'Organizer, Speaker',
     },
     SESSION_ACCEPT_REJECT: {
         'title': u'Session {session_name} has been {acceptance}',
-        'message': u"""The session <strong>{session_name}</strong> has been
-             <strong>{acceptance}</strong> by the Organizer.<br><br>
-            <a href='{link}' class='btn btn-info btn-sm'>View Session</a>""",
+        'message': u"The session <strong>{session_name}</strong> has been" +
+                   u"<strong>{acceptance}</strong> by the Organizer.<br><br>" +
+                   u"<a href='{link}' class='btn btn-info btn-sm'>View Session</a>",
         'recipient': 'Speaker',
     },
     INVITE_PAPERS: {
         'title': u'Invitation to Submit Papers for {event_name}',
-        'message': u"""You have been invited to submit papers for <strong>{event_name}</strong>.<br><br>
-            <a href='{cfs_link}' class='btn btn-info btn-sm'>View Call for Speakers</a>
-            <a href='{submit_link}' class='btn btn-success btn-sm'>Submit</a>""",
+        'message': u"You have been invited to submit papers for <strong>{event_name}</strong>.<br><br>" +
+                   u"<a href='{cfs_link}' class='btn btn-info btn-sm'>View Call for Speakers</a>" +
+                   u"<a href='{submit_link}' class='btn btn-success btn-sm'>Submit</a>",
         'recipient': 'Speaker',
     },
 }
diff --git a/app/api/schema/message_settings.py b/app/api/schema/message_settings.py
--- a/app/api/schema/message_settings.py
+++ b/app/api/schema/message_settings.py
@@ -66,4 +66,8 @@ class Meta:
     notification_status = fields.Boolean(default=False)
     user_control_status = fields.Boolean(default=False)
     email_message = fields.Str(dump_only=True)
+    recipient = fields.Str(dump_only=True)
+    email_subject = fields.Str(dump_only=True)
+    notification_title = fields.Str(dump_only=True)
     notification_message = fields.Str(dump_only=True)
+    sent_at = fields.DateTime(dump_only=True)
diff --git a/app/models/message_setting.py b/app/models/message_setting.py
--- a/app/models/message_setting.py
+++ b/app/models/message_setting.py
@@ -22,7 +22,7 @@
 AFTER_EVENT = 'After Event'
 USER_REGISTER_WITH_PASSWORD = 'User Registration during Payment'
 TICKET_PURCHASED = 'Ticket(s) Purchased'
-TICKET_PURCHASED_ATTENDEE = 'Ticket(s) purchased to Attendee    '
+TICKET_PURCHASED_ATTENDEE = 'Ticket(s) purchased to Attendee'
 TICKET_PURCHASED_ORGANIZER = 'Ticket(s) Purchased to Organizer'
 TICKET_CANCELLED = 'Ticket(s) cancelled'
 TICKET_RESEND_ORGANIZER = 'Ticket Resend'
@@ -62,7 +62,7 @@ def __str__(self):
         return self.__repr__()
 
     @classmethod
-    def _email_message(self, action):
+    def _email_message(self, action, attr=None):
         message = {}
         if action in [INVITE_PAPERS, NEW_SESSION, USER_CONFIRM,
                       USER_REGISTER, PASSWORD_RESET, EVENT_ROLE,
@@ -73,38 +73,55 @@ def _email_message(self, action):
                       MAIL_TO_EXPIRED_ORDERS, MONTHLY_PAYMENT_EMAIL,
                       MONTHLY_PAYMENT_FOLLOWUP_EMAIL, EVENT_IMPORTED,
                       EVENT_IMPORT_FAIL, TICKET_PURCHASED_ORGANIZER,
-                      TICKET_CANCELLED, TICKET_PURCHASED_ATTENDEE, 
+                      TICKET_CANCELLED, TICKET_PURCHASED_ATTENDEE,
                       PASSWORD_CHANGE]:
             message = MAILS[action]
         else:
             message = MAILS.__dict__[action]
+        message = str(message.get(attr))
         return message
 
     @hybrid_property
     def email_message(self):
-        message = self._email_message(self.action)
+        message = self._email_message(self.action, attr='message')
+        return message
+
+    @hybrid_property
+    def recipient(self):
+        message = self._email_message(self.action, attr='recipient')
+        return message
+
+    @hybrid_property
+    def email_subject(self):
+        message = self._email_message(self.action, attr='subject')
         return message
 
     @classmethod
-    def _notification_message(self, action):
+    def _notification_message(self, action, attr=None):
         message = {}
         if action in [EVENT_ROLE, NEW_SESSION, SESSION_SCHEDULE,
                       NEXT_EVENT, SESSION_ACCEPT_REJECT, INVITE_PAPERS,
-                      AFTER_EVENT, EVENT_PUBLISH, USER_CHANGE_EMAIL, 
+                      AFTER_EVENT, EVENT_PUBLISH, USER_CHANGE_EMAIL,
                       PASSWORD_CHANGE, TICKET_PURCHASED,
                       TICKET_RESEND_ORGANIZER, EVENT_EXPORT_FAIL,
                       EVENT_EXPORTED, EVENT_IMPORT_FAIL, EVENT_IMPORTED,
                       MONTHLY_PAYMENT_NOTIF, MONTHLY_PAYMENT_FOLLOWUP_NOTIF,
                       TICKET_PURCHASED_ORGANIZER, TICKET_PURCHASED_ATTENDEE,
                       TICKET_CANCELLED, TICKET_CANCELLED_ORGANIZER]:
-            message = MAILS[action]
+            message = NOTIFS[action]
         else:
-            message = MAILS.__dict__[action]
+            message = NOTIFS.__dict__[action]
+        message = str(message.get(attr))
         return message
 
     @hybrid_property
     def notification_message(self):
-        message = self._notification_message(self.action)
+        message = self._notification_message(self.action, attr='message')
+        return message
+
+    @hybrid_property
+    def notification_title(self):
+        message = self._notification_message(self.action, attr='title')
         return message
 
     @property


Test patch: