Repository: fossasia/open-event-server

Issue:
Add creator field in Event API
Parent issue: #896 

Points - 
- Creator field cannot be modified by PUT
- Creator field cannot be set by POST. It is auto-set at POST request.



Discussion:

Resolving patch:
diff --git a/open_event/api/events.py b/open_event/api/events.py
--- a/open_event/api/events.py
+++ b/open_event/api/events.py
@@ -1,7 +1,10 @@
 from flask.ext.restplus import Resource, Namespace
 from flask import g
 
-from open_event.models.event import Event as EventModel, EventsUsers
+from open_event.models.event import Event as EventModel
+from open_event.models.users_events_roles import UsersEventsRoles
+from open_event.models.role import Role
+from open_event.models.user import ORGANIZER
 from open_event.helpers.data import save_to_db, update_version
 
 from .helpers.helpers import get_paginated_list, requires_auth
@@ -13,6 +16,11 @@
 
 api = Namespace('events', description='Events')
 
+EVENT_CREATOR = api.model('EventCreator', {
+    'id': fields.Integer(),
+    'email': fields.Email()
+})
+
 EVENT = api.model('Event', {
     'id': fields.Integer(required=True),
     'name': fields.String(required=True),
@@ -34,7 +42,8 @@
     'type': EventTypeField(),
     'topic': EventTopicField(),
     'privacy': fields.String(),
-    'ticket_url': fields.Uri()
+    'ticket_url': fields.Uri(),
+    'creator': fields.Nested(EVENT_CREATOR, allow_null=True)
 })
 
 EVENT_PAGINATED = api.clone('EventPaginated', PAGINATED_MODEL, {
@@ -43,6 +52,7 @@
 
 EVENT_POST = api.clone('EventPost', EVENT)
 del EVENT_POST['id']
+del EVENT_POST['creator']
 
 
 class EventDAO(BaseDAO):
@@ -64,19 +74,17 @@ def create(self, data, url):
         data = self.validate(data)
         payload = self.fix_payload(data)
         new_event = self.model(**payload)
-        # set user (owner)
-        a = EventsUsers()
-        a.user = g.user
-        a.editor = True
-        a.admin = True
-        new_event.users.append(a)
+        new_event.creator = g.user
         save_to_db(new_event, "Event saved")
+        # set organizer
+        role = Role.query.filter_by(name=ORGANIZER).first()
+        uer = UsersEventsRoles(g.user, new_event, role)
+        save_to_db(uer, 'UER saved')
         update_version(
             event_id=new_event.id,
             is_created=True,
             column_to_increment="event_ver"
         )
-
         # Return created resource with a 201 status code and its Location
         # (url) in the header.
         resource_location = url + '/' + str(new_event.id)


Test patch:
diff --git a/tests/api/test_post_api.py b/tests/api/test_post_api.py
--- a/tests/api/test_post_api.py
+++ b/tests/api/test_post_api.py
@@ -42,11 +42,13 @@ class TestPostApi(TestPostApiBase):
     Test POST APIs against 401 (unauthorized) and
     201 (successful) status codes
     """
-    def _test_model(self, name, data):
+    def _test_model(self, name, data, checks=[]):
         """
         Tests -
         1. Without login, try to do a POST request and catch 401 error
         2. Login and match 201 response code and correct response data
+        Param:
+            checks - list of strings to assert in successful response data
         """
         path = get_path() if name == 'event' else get_path(1, name + 's')
         response = self.post_request(path, data)
@@ -57,10 +59,12 @@ def _test_model(self, name, data):
         self.assertEqual(201, response.status_code, msg=response.data)
         self.assertIn('location', response.headers)
         self.assertIn('Test' + str(name).title(), response.data)
+        for string in checks:
+            self.assertIn(string, response.data, msg=string)
         return response
 
     def test_event_api(self):
-        self._test_model('event', POST_EVENT_DATA)
+        self._test_model('event', POST_EVENT_DATA, ['test@example.com'])
 
     def test_track_api(self):
         self._test_model('track', POST_TRACK_DATA)
diff --git a/tests/api/utils.py b/tests/api/utils.py
--- a/tests/api/utils.py
+++ b/tests/api/utils.py
@@ -16,8 +16,6 @@ def create_event(name='TestEvent'):
     event = Event(name=name,
                   start_time=datetime(2013, 8, 4, 12, 30, 45),
                   end_time=datetime(2016, 9, 4, 12, 30, 45))
-    event.owner = 1
-
     save_to_db(event, 'Event saved')
     return event.id