Repository: fossasia/open-event-server

Issue:
Event Copyright: Add separate resource class for direct endpoint 
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
@@ -27,7 +27,7 @@
 from app.api.roles import RoleList, RoleDetail
 from app.api.session_types import SessionTypeList, SessionTypeListPost, SessionTypeDetail,\
     SessionTypeRelationshipRequired, SessionTypeRelationshipOptional
-from app.api.event_copyright import EventCopyrightList, EventCopyrightDetail, EventCopyrightRelationship
+from app.api.event_copyright import EventCopyrightListPost, EventCopyrightDetail, EventCopyrightRelationshipRequired
 from app.api.pages import PageList, PageDetail
 from app.api.user_permission import UserPermissionList, UserPermissionDetail
 from app.api.tax import TaxList, TaxListPost, TaxDetail, TaxRelationship
@@ -262,12 +262,11 @@
 api.route(SpeakerRelationshipOptional, 'speaker_session', '/speakers/<int:id>/relationships/sessions')
 
 # event_copyright
-api.route(EventCopyrightList, 'event_copyright_list', '/event-copyrights', '/events/<int:event_id>/event-copyright',
-          '/events/<event_identifier>/event-copyright')
+api.route(EventCopyrightListPost, 'event_copyright_list_post', '/event-copyrights')
 api.route(EventCopyrightDetail, 'event_copyright_detail',
           '/event-copyright/<int:id>', '/events/<int:event_id>/event-copyright',
           '/events/<event_identifier>/event-copyright')
-api.route(EventCopyrightRelationship, 'copyright_event',
+api.route(EventCopyrightRelationshipRequired, 'copyright_event',
           '/event-copyright/<int:id>/relationships/event')
 
 # custom_placeholder
diff --git a/app/api/event_copyright.py b/app/api/event_copyright.py
--- a/app/api/event_copyright.py
+++ b/app/api/event_copyright.py
@@ -13,6 +13,8 @@
 from app.models.event import Event
 from app.api.helpers.exceptions import UnprocessableEntity
 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 EventCopyrightSchema(Schema):
@@ -39,21 +41,15 @@ class Meta:
                          type_='event')
 
 
-class EventCopyrightList(ResourceList):
+class EventCopyrightListPost(ResourceList):
     def before_post(self, args, kwargs, data):
         require_relationship(['event'], data)
+        if not has_access('is_coorganizer', event_id=data['event']):
+            raise ForbiddenException({'source': ''}, 'Co-organizer access is required.')
 
     def before_create_object(self, data, view_kwargs):
-        event = None
-        if view_kwargs.get('event_id'):
-            event = safe_query(self, Event, 'id', view_kwargs['event_id'], 'event_id')
-        elif view_kwargs.get('event_identifier'):
-            event = safe_query(self, Event, 'identifier', view_kwargs['event_identifier'], 'event_identifier')
-        if event:
-            data['event_id'] = event.id
-
         try:
-            self.session.query(EventCopyright).filter_by(event_id=data['event_id']).one()
+            self.session.query(EventCopyright).filter_by(event_id=data['event']).one()
         except NoResultFound:
             pass
         else:
@@ -62,7 +58,6 @@ def before_create_object(self, data, view_kwargs):
 
     methods = ['POST', ]
     view_kwargs = True
-    decorators = (api.has_permission('is_coorganizer', fetch="event_id", fetch_as="event_id"),)
     schema = EventCopyrightSchema
     data_layer = {'session': db.session,
                   'model': EventCopyright,
@@ -93,7 +88,7 @@ def before_get_object(self, view_kwargs):
                   }}
 
 
-class EventCopyrightRelationship(ResourceRelationship):
+class EventCopyrightRelationshipRequired(ResourceRelationship):
 
     decorators = (api.has_permission('is_coorganizer', fetch="event_id", fetch_as="event_id",
                                      model=EventCopyright, methods="PATCH"),)


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -255,13 +255,10 @@ def event_delete(transaction):
 @hooks.before("Copyright > Event Copyright > Create Event Copyright")
 def copyright_post(transaction):
     """
-    POST /events/1/event-copyright
+    POST /event-copyrights
     :param transaction:
     :return:
     """
-    # Skip until docs for direct endpoints added
-    transaction['skip'] = True
-
     with stash['app'].app_context():
         event = EventFactoryBasic()
         db.session.add(event)