Repository: fossasia/open-event-server

Issue:
Add mails API 
based on the `Mail` model.

Endpoint required:
`GET /mails`, admin access required.
`GET /mails/id`, admin access required.


Discussion:

Resolving patch:
diff --git a/app/api/__init__.py b/app/api/__init__.py
--- a/app/api/__init__.py
+++ b/app/api/__init__.py
@@ -49,6 +49,7 @@
 from app.api.activities import ActivityList, ActivityDetail
 from app.api.orders import OrdersList, OrderDetail, OrderRelationship, ChargeList, OrdersListPost
 from app.api.event_statistics import EventStatisticsGeneralDetail
+from app.api.mails import MailList, MailDetail
 
 # users
 api.route(UserList, 'user_list', '/users')
@@ -93,6 +94,11 @@
 api.route(PageList, 'page_list', '/pages')
 api.route(PageDetail, 'page_detail', '/pages/<int:id>')
 
+
+# Mails API
+api.route(MailList, 'mail_list', '/mails')
+api.route(MailDetail, 'mail_detail', '/mails/<int:id>')
+
 # user-permissions
 api.route(UserPermissionList, 'user_permission_list', '/user-permissions')
 api.route(UserPermissionDetail, 'user_permission_detail', '/user-permissions/<int:id>')
@@ -402,6 +408,7 @@
 api.route(OrderRelationship, 'order_marketer', '/orders/<identifier>/relationships/marketer')
 api.route(OrderRelationship, 'order_discount', '/orders/<identifier>/relationships/discount-code')
 
+
 # Event Statistics API
 api.route(EventStatisticsGeneralDetail, 'event_statistics_general_detail', '/events/<int:id>/general-statistics',
           '/events/<identifier>/general-statistics')
diff --git a/app/api/mails.py b/app/api/mails.py
new file mode 100644
--- /dev/null
+++ b/app/api/mails.py
@@ -0,0 +1,52 @@
+from flask_rest_jsonapi import ResourceDetail, ResourceList
+from marshmallow_jsonapi.flask import Schema
+from marshmallow_jsonapi import fields
+
+from app.api.bootstrap import api
+from app.api.helpers.utilities import dasherize
+from app.models import db
+from app.models.mail import Mail
+
+
+class MailSchema(Schema):
+    """
+    Api schema for mail Model
+    """
+    class Meta:
+        """
+        Meta class for mail Api Schema
+        """
+        type_ = 'mail'
+        self_view = 'v1.mail_detail'
+        self_view_kwargs = {'id': '<id>'}
+        self_view_many = 'v1.mail_list'
+        inflect = dasherize
+
+    id = fields.Str(dump_only=True)
+    recipient = fields.Email(dump_only=True)
+    time = fields.DateTime(dump_only=True)
+    action = fields.Str(dump_only=True)
+    subject = fields.Str(dump_only=True)
+    message = fields.Str(dump_only=True)
+
+
+class MailList(ResourceList):
+    """
+    List and create mails
+    """
+    decorators = (api.has_permission('is_admin'),)
+    methods = ['GET']
+    schema = MailSchema
+    data_layer = {'session': db.session,
+                  'model': Mail}
+
+
+class MailDetail(ResourceDetail):
+    """
+    mail detail by id
+    """
+    methods = ['GET']
+    schema = MailSchema
+    decorators = (api.has_permission('is_admin'),)
+    data_layer = {'session': db.session,
+                  'model': Mail}
diff --git a/app/factories/mail.py b/app/factories/mail.py
new file mode 100644
--- /dev/null
+++ b/app/factories/mail.py
@@ -0,0 +1,15 @@
+import factory
+from app.models.mail import db, Mail
+import app.factories.common as common
+
+
+class MailFactory(factory.alchemy.SQLAlchemyModelFactory):
+    class Meta:
+        model = Mail
+        sqlalchemy_session = db.session
+
+    recipient = common.email_
+    time = common.date_
+    action = common.string_
+    subject = common.string_
+    message = common.string_


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -48,6 +48,7 @@
 from app.factories.email_notification import EmailNotificationFactory
 from app.factories.activities import ActivityFactory
 from app.factories.stripe_authorization import StripeAuthorizationFactory
+from app.factories.mail import MailFactory
 
 
 stash = {}
@@ -2169,6 +2170,33 @@ def page_delete(transaction):
         db.session.commit()
 
 
+# ------------------------- Mails -------------------------
+@hooks.before("Mails > Mail Collection > Show all mails")
+def mail_get_list(transaction):
+    """
+    GET /mails
+    :param transaction:
+    :return:
+    """
+    with stash['app'].app_context():
+        mail = MailFactory()
+        db.session.add(mail)
+        db.session.commit()
+
+
+@hooks.before("Mails > Mail Details > Get Mail Details")
+def mail_get_detail(transaction):
+    """
+    GET /mails/1
+    :param transaction:
+    :return:
+    """
+    with stash['app'].app_context():
+        mail = MailFactory()
+        db.session.add(mail)
+        db.session.commit()
+
+
 # ------------------------- Settings -------------------------
 @hooks.before("Settings > Settings Details > Show Settings")
 def settings_get_list(transaction):