Repository: fossasia/open-event-server

Issue:
event sub topics: add separate resource list for direct endpoints
parent issue #4101 


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
@@ -9,7 +9,8 @@
 from app.api.events import EventList, EventDetail, EventRelationship
 from app.api.event_types import EventTypeList, EventTypeDetail, EventTypeRelationship
 from app.api.event_topics import EventTopicList, EventTopicDetail, EventTopicRelationship
-from app.api.event_sub_topics import EventSubTopicList, EventSubTopicDetail, EventSubTopicRelationship
+from app.api.event_sub_topics import EventSubTopicList, EventSubTopicListPost, EventSubTopicDetail,\
+    EventSubTopicRelationshipRequired, EventSubTopicRelationshipOptional
 from app.api.microlocations import MicrolocationList, MicrolocationListPost, MicrolocationDetail, \
     MicrolocationRelationshipRequired, MicrolocationRelationshipOptional
 from app.api.sessions import SessionList, SessionListPost, SessionDetail, SessionRelationshipRequired, \
@@ -329,17 +330,18 @@
           '/event-topics/<int:id>/relationships/event-sub-topics')
 
 # event sub topics
+api.route(EventSubTopicListPost, 'event_sub_topic_list_post', '/event-sub-topics')
 api.route(EventSubTopicList, 'event_sub_topic_list',
           '/event-topics/<int:event_topic_id>/event-sub-topics')
 api.route(EventSubTopicDetail, 'event_sub_topic_detail', '/event-sub-topics/<int:id>',
           '/events/<int:event_id>/event-sub-topic',
           '/events/<event_identifier>/event-sub-topic',
           '/custom-placeholders/<int:custom_placeholder_id>/event-sub-topic')
-api.route(EventSubTopicRelationship, 'event_sub_topic_event',
+api.route(EventSubTopicRelationshipOptional, 'event_sub_topic_event',
           '/event-sub-topics/<int:id>/relationships/events')
-api.route(EventSubTopicRelationship, 'event_sub_topic_event_topic',
+api.route(EventSubTopicRelationshipRequired, 'event_sub_topic_event_topic',
           '/event-sub-topics/<int:id>/relationships/event-topic')
-api.route(EventSubTopicRelationship, 'event_sub_topic_custom_placeholder',
+api.route(EventSubTopicRelationshipOptional, 'event_sub_topic_custom_placeholder',
           '/event-sub-topics/<int:id>/relationships/custom-placeholder')
 
 # ticket_fees
diff --git a/app/api/event_sub_topics.py b/app/api/event_sub_topics.py
--- a/app/api/event_sub_topics.py
+++ b/app/api/event_sub_topics.py
@@ -11,6 +11,9 @@
 from app.api.helpers.permissions import jwt_required
 from app.api.helpers.db import safe_query
 from app.api.custom_placeholders import CustomPlaceholder
+from app.api.helpers.utilities import require_relationship
+from app.api.helpers.permission_manager import has_access
+from app.api.helpers.exceptions import ForbiddenException
 
 
 class EventSubTopicSchema(Schema):
@@ -53,9 +56,25 @@ class Meta:
                                       type_='custom-placeholder')
 
 
+class EventSubTopicListPost(ResourceList):
+    """
+    Create event sub topics
+    """
+    def before_post(self, args, kwargs, data):
+        require_relationship(['event_topic'], data)
+        if not has_access('is_admin'):
+            raise ForbiddenException({'source': ''}, 'Admin access is required.')
+
+    view_kwargs = True
+    methods = ['POST', ]
+    schema = EventSubTopicSchema
+    data_layer = {'session': db.session,
+                  'model': EventSubTopic}
+
+
 class EventSubTopicList(ResourceList):
     """
-    List and create event sub topics
+    List event sub topics
     """
 
     def query(self, view_kwargs):
@@ -71,25 +90,13 @@ def query(self, view_kwargs):
             query_ = query_.join(EventTopic).filter(EventTopic.id == event_topic.id)
         return query_
 
-    def before_create_object(self, data, view_kwargs):
-        """
-        method to create object before post
-        :param data:
-        :param view_kwargs:
-        :return:
-        """
-        if view_kwargs.get('event_topic_id'):
-            event_topic = safe_query(self, EventTopic, 'id', view_kwargs['event_topic_id'], 'event_topic_id')
-            data['event_topic_id'] = event_topic.id
-
     view_kwargs = True
-    decorators = (api.has_permission('is_admin', methods="POST"),)
+    methods = ['GET', ]
     schema = EventSubTopicSchema
     data_layer = {'session': db.session,
                   'model': EventSubTopic,
                   'methods': {
-                      'query': query,
-                      'before_create_object': before_create_object
+                      'query': query
                   }}
 
 
@@ -127,7 +134,18 @@ def before_get_object(self, view_kwargs):
                   }}
 
 
-class EventSubTopicRelationship(ResourceRelationship):
+class EventSubTopicRelationshipRequired(ResourceRelationship):
+    """
+    Event sub topic Relationship
+    """
+    decorators = (api.has_permission('is_admin', methods="PATCH"),)
+    methods = ['GET', 'PATCH']
+    schema = EventSubTopicSchema
+    data_layer = {'session': db.session,
+                  'model': EventSubTopic}
+
+
+class EventSubTopicRelationshipOptional(ResourceRelationship):
     """
     Event sub topic Relationship
     """
diff --git a/app/factories/event_sub_topic.py b/app/factories/event_sub_topic.py
--- a/app/factories/event_sub_topic.py
+++ b/app/factories/event_sub_topic.py
@@ -12,3 +12,4 @@ class Meta:
     event_topic = factory.RelatedFactory(EventTopicFactory)
     name = common.string_
     slug = common.slug_
+    event_topic_id = 1


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -2187,7 +2187,7 @@ def event_topic_delete(transaction):
 
 
 # ------------------------- Event Sub Topics -------------------------
-@hooks.before("Event Sub Topics > Event Sub Topics Collection > List All Event Sub Topics")
+@hooks.before("Event Sub Topics > Event Sub Topics Collection Get > List All Event Sub Topics")
 def event_sub_topic_get_list(transaction):
     """
     GET /event-topics/1/event-sub-topics
@@ -2200,10 +2200,10 @@ def event_sub_topic_get_list(transaction):
         db.session.commit()
 
 
-@hooks.before("Event Sub Topics > Event Sub Topics Collection > Create Event Sub Topic")
+@hooks.before("Event Sub Topics > Event Sub Topics Collection Post > Create Event Sub Topic")
 def event_sub_topic_post(transaction):
     """
-    POST /event-topics/1/event-sub-topics
+    POST /event-sub-topics
     :param transaction:
     :return:
     """