Repository: fossasia/open-event-server

Issue:
Add Activity API with relationships and docs
**I'm submitting a ...**  (check one with "x")
- [ ] bug report
- [x] feature request
- [ ] support request => Please do not submit support requests here, instead ask your query in out Gitter channel at https://gitter.im/fossasia/open-event-orga-server




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
@@ -36,8 +36,8 @@
 from app.api.access_codes import AccessCodeList, AccessCodeDetail, AccessCodeRelationshipRequired, \
     AccessCodeRelationshipOptional
 from app.api.modules import ModuleDetail
-from app.api.custom_placeholders import CustomPlaceholderDetail, CustomPlaceholderList, CustomPlaceholderRelationship
-
+from app.api.custom_placeholders import CustomPlaceholderList, CustomPlaceholderDetail, CustomPlaceholderRelationship
+from app.api.activities import ActivityList, ActivityDetail
 # users
 api.route(UserList, 'user_list', '/users')
 api.route(UserDetail, 'user_detail', '/users/<int:id>', '/notifications/<int:notification_id>/user',
@@ -342,3 +342,7 @@
           '/access-codes/<int:id>/relationships/user')
 api.route(AccessCodeRelationshipOptional, 'access_code_tickets',
           '/access-codes/<int:id>/relationships/tickets')
+
+# ticket_fees
+api.route(ActivityList, 'activity_list', '/activities')
+api.route(ActivityDetail, 'activity_detail', '/activities/<int:id>')
diff --git a/app/api/activities.py b/app/api/activities.py
new file mode 100644
--- /dev/null
+++ b/app/api/activities.py
@@ -0,0 +1,49 @@
+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.activity import Activity
+
+
+class ActivitySchema(Schema):
+    """
+    Api schema for Activity Model
+    """
+    class Meta:
+        """
+        Meta class for Activity Api Schema
+        """
+        type_ = 'activity'
+        self_view = 'v1.activity_detail'
+        self_view_kwargs = {'id': '<id>'}
+        inflect = dasherize
+
+    id = fields.Str(dump_only=True)
+    actor = fields.Str(allow_none=True)
+    time = fields.DateTime(allow_none=True)
+    action = fields.Str(allow_none=True)
+
+
+class ActivityList(ResourceList):
+    """
+    List and create activity
+    """
+    schema = ActivitySchema
+    methods = ['GET', ]
+    decorators = (api.has_permission('is_admin', methods="GET"),)
+    data_layer = {'session': db.session,
+                  'model': Activity}
+
+
+class ActivityDetail(ResourceDetail):
+    """
+    Activity detail by id
+    """
+    schema = ActivitySchema
+    methods = ['GET', ]
+    decorators = (api.has_permission('is_admin', methods="GET"),)
+    data_layer = {'session': db.session,
+                  'model': Activity}
diff --git a/app/factories/activities.py b/app/factories/activities.py
new file mode 100644
--- /dev/null
+++ b/app/factories/activities.py
@@ -0,0 +1,13 @@
+import factory
+from app.models.activity import db, Activity
+import app.factories.common as common
+
+
+class ActivityFactory(factory.alchemy.SQLAlchemyModelFactory):
+
+    class Meta:
+        model = Activity
+        sqlalchemy_session = db.session
+
+    actor = common.string_
+    action = common.url_


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -46,6 +46,7 @@
 from app.factories.custom_placeholder import CustomPlaceholderFactory
 from app.factories.user_permission import UserPermissionFactory
 from app.factories.email_notification import EmailNotificationFactory
+from app.factories.activities import ActivityFactory
 
 stash = {}
 api_username = "open_event_test_user@fossasia.org"
@@ -1510,6 +1511,33 @@ def role_delete(transaction):
         db.session.commit()
 
 
+# ------------------------- Activities -------------------------
+@hooks.before("Activity > Activity Collection > List all Activities")
+def activity_get_list(transaction):
+    """
+    GET /activities
+    :param transaction:
+    :return:
+    """
+    with stash['app'].app_context():
+        activity = ActivityFactory()
+        db.session.add(activity)
+        db.session.commit()
+
+
+@hooks.before("Activity > Activity Details > Get Activity Details")
+def activity_get_detail(transaction):
+    """
+    GET /activities/1
+    :param transaction:
+    :return:
+    """
+    with stash['app'].app_context():
+        activity = ActivityFactory()
+        db.session.add(activity)
+        db.session.commit()
+
+
 # ------------------------- Pages -------------------------
 @hooks.before("Pages > Page Collection > Page Sizes")
 def page_get_list(transaction):