We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
We can create Sessions and Speakers even if it is disabled in the Event properties.
**Describe the bug**
We can create Sessions and Speakers even if the variable ```isSessionsSpeakersEnabled``` is set to false.

**To Reproduce**
Steps to reproduce the behavior:
1. Create an event with ```isSessionsSpeakersEnabled``` as false or update an existing one.
2. Now try to create a session or a speaker.

**Expected behavior**
It doesn't make sense to create sessions when it is disabled in the event.

**Additional context**
<!-- Add any other context about the problem here. -->
![in_event_its_false](https://user-images.githubusercontent.com/21277837/40687066-0c05ac4c-63b7-11e8-87d2-8103ab2a4dae.png)
![still_can_create_one](https://user-images.githubusercontent.com/21277837/40687070-106c0010-63b7-11e8-8ab9-de672b455eff.png)


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `app/api/speakers.py`
Content:
```
1 from flask import request
2 from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
3 from flask_rest_jsonapi.exceptions import ObjectNotFound
4 
5 from app.api.bootstrap import api
6 from app.api.helpers.db import safe_query
7 from app.api.helpers.permission_manager import has_access
8 from app.api.helpers.query import event_query
9 from app.api.helpers.utilities import require_relationship
10 from app.api.schema.speakers import SpeakerSchema
11 from app.models import db
12 from app.models.event import Event
13 from app.models.session import Session
14 from app.models.speaker import Speaker
15 from app.models.user import User
16 
17 
18 class SpeakerListPost(ResourceList):
19     """
20     List and create speakers
21     """
22 
23     def before_post(self, args, kwargs, data):
24         """
25         method to add user_id to view_kwargs before post
26         :param args:
27         :param kwargs:
28         :param data:
29         :return:
30         """
31         require_relationship(['event', 'user'], data)
32 
33         if not has_access('is_coorganizer', event_id=data['event']):
34                 event = safe_query(self, Event, 'id', data['event'], 'event_id')
35                 if event.state == "draft":
36                     raise ObjectNotFound({'parameter': 'event_id'},
37                                          "Event: {} not found".format(data['event_id']))
38 
39         if 'sessions' in data:
40             session_ids = data['sessions']
41             for session_id in session_ids:
42                 if not has_access('is_session_self_submitted', session_id=session_id):
43                     raise ObjectNotFound({'parameter': 'session_id'},
44                                          "Session: {} not found".format(session_id))
45 
46     schema = SpeakerSchema
47     methods = ['POST', ]
48     data_layer = {'session': db.session,
49                   'model': Speaker
50                   }
51 
52 
53 class SpeakerList(ResourceList):
54     """
55     List speakers based on different params from view_kwargs
56     """
57 
58     def query(self, view_kwargs):
59         """
60         query method for speakers list class
61         :param view_kwargs:
62         :return:
63         """
64         query_ = self.session.query(Speaker)
65         query_ = event_query(self, query_, view_kwargs)
66 
67         if view_kwargs.get('user_id'):
68             user = safe_query(self, User, 'id', view_kwargs['user_id'], 'user_id')
69             query_ = query_.join(User).filter(User.id == user.id)
70 
71         if view_kwargs.get('session_id'):
72             session = safe_query(self, Session, 'id', view_kwargs['session_id'], 'session_id')
73             # session-speaker :: many-to-many relationship
74             query_ = Speaker.query.filter(Speaker.sessions.any(id=session.id))
75             if 'Authorization' in request.headers and not has_access('is_coorganizer', event_id=session.event_id):
76                 if not has_access('is_session_self_submitted', session_id=session.id):
77                     query_ = query_.filter(Session.state == "approved" or Session.state == "accepted")
78 
79         return query_
80 
81     view_kwargs = True
82     schema = SpeakerSchema
83     methods = ['GET', ]
84     data_layer = {'session': db.session,
85                   'model': Speaker,
86                   'methods': {
87                       'query': query,
88                   }}
89 
90 
91 class SpeakerDetail(ResourceDetail):
92     """
93     Speakers Detail by id
94     """
95     decorators = (api.has_permission('is_coorganizer_or_user_itself', methods="PATCH,DELETE", fetch="event_id",
96                                      fetch_as="event_id", model=Speaker),)
97     schema = SpeakerSchema
98     data_layer = {'session': db.session,
99                   'model': Speaker}
100 
101 
102 class SpeakerRelationshipRequired(ResourceRelationship):
103     """
104     Speaker Relationship class for required entities
105     """
106     decorators = (api.has_permission('is_coorganizer_or_user_itself', methods="PATCH,DELETE", fetch="event_id",
107                                      fetch_as="event_id", model=Speaker),)
108     methods = ['GET', 'PATCH']
109     schema = SpeakerSchema
110     data_layer = {'session': db.session,
111                   'model': Speaker}
112 
113 
114 class SpeakerRelationshipOptional(ResourceRelationship):
115     """
116     Speaker Relationship class
117     """
118     decorators = (api.has_permission('is_coorganizer_or_user_itself', methods="PATCH,DELETE", fetch="event_id",
119                                      fetch_as="event_id", model=Speaker),)
120     schema = SpeakerSchema
121     data_layer = {'session': db.session,
122                   'model': Speaker}
123
```

Path: `app/api/sessions.py`
Content:
```
1 from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
2 
3 from app.api.bootstrap import api
4 from app.api.events import Event
5 from app.api.helpers.db import safe_query
6 from app.api.helpers.mail import send_email_new_session, send_email_session_accept_reject
7 from app.api.helpers.notification import send_notif_new_session_organizer, send_notif_session_accept_reject
8 from app.api.helpers.permissions import current_identity
9 from app.api.helpers.query import event_query
10 from app.api.helpers.utilities import require_relationship
11 from app.api.schema.sessions import SessionSchema
12 from app.models import db
13 from app.models.microlocation import Microlocation
14 from app.models.session import Session
15 from app.models.session_type import SessionType
16 from app.models.speaker import Speaker
17 from app.models.track import Track
18 from app.models.user import User
19 from app.settings import get_settings
20 
21 
22 class SessionListPost(ResourceList):
23     """
24     List Sessions
25     """
26     def before_post(self, args, kwargs, data):
27         """
28         before post method to check for required relationship and proper permission
29         :param args:
30         :param kwargs:
31         :param data:
32         :return:
33         """
34         require_relationship(['event'], data)
35         data['creator_id'] = current_identity.id
36 
37     def after_create_object(self, session, data, view_kwargs):
38         """
39         method to send email for creation of new session
40         mails session link to the concerned user
41         :param session:
42         :param data:
43         :param view_kwargs:
44         :return:
45         """
46         if session.event.get_organizer():
47             event_name = session.event.name
48             organizer = session.event.get_organizer()
49             organizer_email = organizer.email
50             frontend_url = get_settings()['frontend_url']
51             link = "{}/events/{}/sessions/{}"\
52                 .format(frontend_url, session.event_id, session.id)
53             send_email_new_session(organizer_email, event_name, link)
54             send_notif_new_session_organizer(organizer, event_name, link)
55 
56     decorators = (api.has_permission('create_event'),)
57     schema = SessionSchema
58     data_layer = {'session': db.session,
59                   'model': Session,
60                   'methods': {'after_create_object': after_create_object
61                               }}
62 
63 
64 class SessionList(ResourceList):
65     """
66     List Sessions
67     """
68 
69     def query(self, view_kwargs):
70         """
71         query method for SessionList class
72         :param view_kwargs:
73         :return:
74         """
75         query_ = self.session.query(Session)
76         if view_kwargs.get('track_id') is not None:
77             track = safe_query(self, Track, 'id', view_kwargs['track_id'], 'track_id')
78             query_ = query_.join(Track).filter(Track.id == track.id)
79         if view_kwargs.get('session_type_id') is not None:
80             session_type = safe_query(self, SessionType, 'id', view_kwargs['session_type_id'], 'session_type_id')
81             query_ = query_.join(SessionType).filter(SessionType.id == session_type.id)
82         if view_kwargs.get('microlocation_id') is not None:
83             microlocation = safe_query(self, Microlocation, 'id', view_kwargs['microlocation_id'], 'microlocation_id')
84             query_ = query_.join(Microlocation).filter(Microlocation.id == microlocation.id)
85         if view_kwargs.get('user_id') is not None:
86             user = safe_query(self, User, 'id', view_kwargs['user_id'], 'user_id')
87             query_ = query_.join(User).filter(User.id == user.id)
88         query_ = event_query(self, query_, view_kwargs)
89         if view_kwargs.get('speaker_id'):
90             speaker = safe_query(self, Speaker, 'id', view_kwargs['speaker_id'], 'speaker_id')
91             # session-speaker :: many-to-many relationship
92             query_ = Session.query.filter(Session.speakers.any(id=speaker.id))
93 
94         return query_
95 
96     view_kwargs = True
97     methods = ['GET']
98     schema = SessionSchema
99     data_layer = {'session': db.session,
100                   'model': Session,
101                   'methods': {
102                       'query': query
103                   }}
104 
105 
106 class SessionDetail(ResourceDetail):
107     """
108     Session detail by id
109     """
110     def before_get_object(self, view_kwargs):
111         """
112         before get method to get the resource id for fetching details
113         :param view_kwargs:
114         :return:
115         """
116         if view_kwargs.get('event_identifier'):
117             event = safe_query(self, Event, 'identifier', view_kwargs['event_identifier'], 'identifier')
118             view_kwargs['event_id'] = event.id
119 
120     def after_update_object(self, session, data, view_kwargs):
121         """ Send email if session accepted or rejected """
122         if 'state' in data and (session.state == 'accepted' or session.state == 'rejected'):
123             # Email for speaker
124             speakers = session.speakers
125             for speaker in speakers:
126                 frontend_url = get_settings()['frontend_url']
127                 link = "{}/events/{}/sessions/{}" \
128                     .format(frontend_url, session.event_id, session.id)
129                 send_email_session_accept_reject(speaker.email, session, link)
130                 send_notif_session_accept_reject(speaker, session.title, session.state, link)
131 
132             # Email for organizer
133             if session.event.get_organizer():
134                 organizer = session.event.get_organizer()
135                 organizer_email = organizer.email
136                 frontend_url = get_settings()['frontend_url']
137                 link = "{}/events/{}/sessions/{}" \
138                     .format(frontend_url, session.event_id, session.id)
139                 send_email_session_accept_reject(organizer_email, session,
140                                                  link)
141                 send_notif_session_accept_reject(organizer, session.title,
142                                                  session.state, link)
143 
144     decorators = (api.has_permission('is_speaker_for_session', methods="PATCH,DELETE"),)
145     schema = SessionSchema
146     data_layer = {'session': db.session,
147                   'model': Session,
148                   'methods': {'before_get_object': before_get_object,
149                               'after_update_object': after_update_object}}
150 
151 
152 class SessionRelationshipRequired(ResourceRelationship):
153     """
154     Session Relationship
155     """
156     schema = SessionSchema
157     decorators = (api.has_permission('is_speaker_for_session', methods="PATCH,DELETE"),)
158     methods = ['GET', 'PATCH']
159     data_layer = {'session': db.session,
160                   'model': Session}
161 
162 
163 class SessionRelationshipOptional(ResourceRelationship):
164     """
165     Session Relationship
166     """
167     schema = SessionSchema
168     decorators = (api.has_permission('is_speaker_for_session', methods="PATCH,DELETE"),)
169     data_layer = {'session': db.session,
170                   'model': Session}
171
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/app/api/sessions.py b/app/api/sessions.py
--- a/app/api/sessions.py
+++ b/app/api/sessions.py
@@ -2,7 +2,8 @@
 
 from app.api.bootstrap import api
 from app.api.events import Event
-from app.api.helpers.db import safe_query
+from app.api.helpers.db import safe_query, get_count
+from app.api.helpers.exceptions import ForbiddenException
 from app.api.helpers.mail import send_email_new_session, send_email_session_accept_reject
 from app.api.helpers.notification import send_notif_new_session_organizer, send_notif_session_accept_reject
 from app.api.helpers.permissions import current_identity
@@ -33,6 +34,8 @@
         """
         require_relationship(['event'], data)
         data['creator_id'] = current_identity.id
+        if get_count(db.session.query(Event).filter_by(id=int(data['event']), is_sessions_speakers_enabled=False)) > 0:
+            raise ForbiddenException({'pointer': ''}, "Sessions are disabled for this Event")
 
     def after_create_object(self, session, data, view_kwargs):
         """
diff --git a/app/api/speakers.py b/app/api/speakers.py
--- a/app/api/speakers.py
+++ b/app/api/speakers.py
@@ -3,7 +3,8 @@
 from flask_rest_jsonapi.exceptions import ObjectNotFound
 
 from app.api.bootstrap import api
-from app.api.helpers.db import safe_query
+from app.api.helpers.db import safe_query, get_count
+from app.api.helpers.exceptions import ForbiddenException
 from app.api.helpers.permission_manager import has_access
 from app.api.helpers.query import event_query
 from app.api.helpers.utilities import require_relationship
@@ -36,6 +37,9 @@
                     raise ObjectNotFound({'parameter': 'event_id'},
                                          "Event: {} not found".format(data['event_id']))
 
+        if get_count(db.session.query(Event).filter_by(id=int(data['event']), is_sessions_speakers_enabled=False)) > 0:
+            raise ForbiddenException({'pointer': ''}, "Speakers are disabled for this Event")
+
         if 'sessions' in data:
             session_ids = data['sessions']
             for session_id in session_ids:

{"golden_diff": "diff --git a/app/api/sessions.py b/app/api/sessions.py\n--- a/app/api/sessions.py\n+++ b/app/api/sessions.py\n@@ -2,7 +2,8 @@\n \n from app.api.bootstrap import api\n from app.api.events import Event\n-from app.api.helpers.db import safe_query\n+from app.api.helpers.db import safe_query, get_count\n+from app.api.helpers.exceptions import ForbiddenException\n from app.api.helpers.mail import send_email_new_session, send_email_session_accept_reject\n from app.api.helpers.notification import send_notif_new_session_organizer, send_notif_session_accept_reject\n from app.api.helpers.permissions import current_identity\n@@ -33,6 +34,8 @@\n         \"\"\"\n         require_relationship(['event'], data)\n         data['creator_id'] = current_identity.id\n+        if get_count(db.session.query(Event).filter_by(id=int(data['event']), is_sessions_speakers_enabled=False)) > 0:\n+            raise ForbiddenException({'pointer': ''}, \"Sessions are disabled for this Event\")\n \n     def after_create_object(self, session, data, view_kwargs):\n         \"\"\"\ndiff --git a/app/api/speakers.py b/app/api/speakers.py\n--- a/app/api/speakers.py\n+++ b/app/api/speakers.py\n@@ -3,7 +3,8 @@\n from flask_rest_jsonapi.exceptions import ObjectNotFound\n \n from app.api.bootstrap import api\n-from app.api.helpers.db import safe_query\n+from app.api.helpers.db import safe_query, get_count\n+from app.api.helpers.exceptions import ForbiddenException\n from app.api.helpers.permission_manager import has_access\n from app.api.helpers.query import event_query\n from app.api.helpers.utilities import require_relationship\n@@ -36,6 +37,9 @@\n                     raise ObjectNotFound({'parameter': 'event_id'},\n                                          \"Event: {} not found\".format(data['event_id']))\n \n+        if get_count(db.session.query(Event).filter_by(id=int(data['event']), is_sessions_speakers_enabled=False)) > 0:\n+            raise ForbiddenException({'pointer': ''}, \"Speakers are disabled for this Event\")\n+\n         if 'sessions' in data:\n             session_ids = data['sessions']\n             for session_id in session_ids:\n", "issue": "We can create Sessions and Speakers even if it is disabled in the Event properties.\n**Describe the bug**\r\nWe can create Sessions and Speakers even if the variable ```isSessionsSpeakersEnabled``` is set to false.\r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\n1. Create an event with ```isSessionsSpeakersEnabled``` as false or update an existing one.\r\n2. Now try to create a session or a speaker.\r\n\r\n**Expected behavior**\r\nIt doesn't make sense to create sessions when it is disabled in the event.\r\n\r\n**Additional context**\r\n<!-- Add any other context about the problem here. -->\r\n![in_event_its_false](https://user-images.githubusercontent.com/21277837/40687066-0c05ac4c-63b7-11e8-87d2-8103ab2a4dae.png)\r\n![still_can_create_one](https://user-images.githubusercontent.com/21277837/40687070-106c0010-63b7-11e8-8ab9-de672b455eff.png)\r\n\n", "before_files": [{"content": "from flask import request\nfrom flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship\nfrom flask_rest_jsonapi.exceptions import ObjectNotFound\n\nfrom app.api.bootstrap import api\nfrom app.api.helpers.db import safe_query\nfrom app.api.helpers.permission_manager import has_access\nfrom app.api.helpers.query import event_query\nfrom app.api.helpers.utilities import require_relationship\nfrom app.api.schema.speakers import SpeakerSchema\nfrom app.models import db\nfrom app.models.event import Event\nfrom app.models.session import Session\nfrom app.models.speaker import Speaker\nfrom app.models.user import User\n\n\nclass SpeakerListPost(ResourceList):\n    \"\"\"\n    List and create speakers\n    \"\"\"\n\n    def before_post(self, args, kwargs, data):\n        \"\"\"\n        method to add user_id to view_kwargs before post\n        :param args:\n        :param kwargs:\n        :param data:\n        :return:\n        \"\"\"\n        require_relationship(['event', 'user'], data)\n\n        if not has_access('is_coorganizer', event_id=data['event']):\n                event = safe_query(self, Event, 'id', data['event'], 'event_id')\n                if event.state == \"draft\":\n                    raise ObjectNotFound({'parameter': 'event_id'},\n                                         \"Event: {} not found\".format(data['event_id']))\n\n        if 'sessions' in data:\n            session_ids = data['sessions']\n            for session_id in session_ids:\n                if not has_access('is_session_self_submitted', session_id=session_id):\n                    raise ObjectNotFound({'parameter': 'session_id'},\n                                         \"Session: {} not found\".format(session_id))\n\n    schema = SpeakerSchema\n    methods = ['POST', ]\n    data_layer = {'session': db.session,\n                  'model': Speaker\n                  }\n\n\nclass SpeakerList(ResourceList):\n    \"\"\"\n    List speakers based on different params from view_kwargs\n    \"\"\"\n\n    def query(self, view_kwargs):\n        \"\"\"\n        query method for speakers list class\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        query_ = self.session.query(Speaker)\n        query_ = event_query(self, query_, view_kwargs)\n\n        if view_kwargs.get('user_id'):\n            user = safe_query(self, User, 'id', view_kwargs['user_id'], 'user_id')\n            query_ = query_.join(User).filter(User.id == user.id)\n\n        if view_kwargs.get('session_id'):\n            session = safe_query(self, Session, 'id', view_kwargs['session_id'], 'session_id')\n            # session-speaker :: many-to-many relationship\n            query_ = Speaker.query.filter(Speaker.sessions.any(id=session.id))\n            if 'Authorization' in request.headers and not has_access('is_coorganizer', event_id=session.event_id):\n                if not has_access('is_session_self_submitted', session_id=session.id):\n                    query_ = query_.filter(Session.state == \"approved\" or Session.state == \"accepted\")\n\n        return query_\n\n    view_kwargs = True\n    schema = SpeakerSchema\n    methods = ['GET', ]\n    data_layer = {'session': db.session,\n                  'model': Speaker,\n                  'methods': {\n                      'query': query,\n                  }}\n\n\nclass SpeakerDetail(ResourceDetail):\n    \"\"\"\n    Speakers Detail by id\n    \"\"\"\n    decorators = (api.has_permission('is_coorganizer_or_user_itself', methods=\"PATCH,DELETE\", fetch=\"event_id\",\n                                     fetch_as=\"event_id\", model=Speaker),)\n    schema = SpeakerSchema\n    data_layer = {'session': db.session,\n                  'model': Speaker}\n\n\nclass SpeakerRelationshipRequired(ResourceRelationship):\n    \"\"\"\n    Speaker Relationship class for required entities\n    \"\"\"\n    decorators = (api.has_permission('is_coorganizer_or_user_itself', methods=\"PATCH,DELETE\", fetch=\"event_id\",\n                                     fetch_as=\"event_id\", model=Speaker),)\n    methods = ['GET', 'PATCH']\n    schema = SpeakerSchema\n    data_layer = {'session': db.session,\n                  'model': Speaker}\n\n\nclass SpeakerRelationshipOptional(ResourceRelationship):\n    \"\"\"\n    Speaker Relationship class\n    \"\"\"\n    decorators = (api.has_permission('is_coorganizer_or_user_itself', methods=\"PATCH,DELETE\", fetch=\"event_id\",\n                                     fetch_as=\"event_id\", model=Speaker),)\n    schema = SpeakerSchema\n    data_layer = {'session': db.session,\n                  'model': Speaker}\n", "path": "app/api/speakers.py"}, {"content": "from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship\n\nfrom app.api.bootstrap import api\nfrom app.api.events import Event\nfrom app.api.helpers.db import safe_query\nfrom app.api.helpers.mail import send_email_new_session, send_email_session_accept_reject\nfrom app.api.helpers.notification import send_notif_new_session_organizer, send_notif_session_accept_reject\nfrom app.api.helpers.permissions import current_identity\nfrom app.api.helpers.query import event_query\nfrom app.api.helpers.utilities import require_relationship\nfrom app.api.schema.sessions import SessionSchema\nfrom app.models import db\nfrom app.models.microlocation import Microlocation\nfrom app.models.session import Session\nfrom app.models.session_type import SessionType\nfrom app.models.speaker import Speaker\nfrom app.models.track import Track\nfrom app.models.user import User\nfrom app.settings import get_settings\n\n\nclass SessionListPost(ResourceList):\n    \"\"\"\n    List Sessions\n    \"\"\"\n    def before_post(self, args, kwargs, data):\n        \"\"\"\n        before post method to check for required relationship and proper permission\n        :param args:\n        :param kwargs:\n        :param data:\n        :return:\n        \"\"\"\n        require_relationship(['event'], data)\n        data['creator_id'] = current_identity.id\n\n    def after_create_object(self, session, data, view_kwargs):\n        \"\"\"\n        method to send email for creation of new session\n        mails session link to the concerned user\n        :param session:\n        :param data:\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if session.event.get_organizer():\n            event_name = session.event.name\n            organizer = session.event.get_organizer()\n            organizer_email = organizer.email\n            frontend_url = get_settings()['frontend_url']\n            link = \"{}/events/{}/sessions/{}\"\\\n                .format(frontend_url, session.event_id, session.id)\n            send_email_new_session(organizer_email, event_name, link)\n            send_notif_new_session_organizer(organizer, event_name, link)\n\n    decorators = (api.has_permission('create_event'),)\n    schema = SessionSchema\n    data_layer = {'session': db.session,\n                  'model': Session,\n                  'methods': {'after_create_object': after_create_object\n                              }}\n\n\nclass SessionList(ResourceList):\n    \"\"\"\n    List Sessions\n    \"\"\"\n\n    def query(self, view_kwargs):\n        \"\"\"\n        query method for SessionList class\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        query_ = self.session.query(Session)\n        if view_kwargs.get('track_id') is not None:\n            track = safe_query(self, Track, 'id', view_kwargs['track_id'], 'track_id')\n            query_ = query_.join(Track).filter(Track.id == track.id)\n        if view_kwargs.get('session_type_id') is not None:\n            session_type = safe_query(self, SessionType, 'id', view_kwargs['session_type_id'], 'session_type_id')\n            query_ = query_.join(SessionType).filter(SessionType.id == session_type.id)\n        if view_kwargs.get('microlocation_id') is not None:\n            microlocation = safe_query(self, Microlocation, 'id', view_kwargs['microlocation_id'], 'microlocation_id')\n            query_ = query_.join(Microlocation).filter(Microlocation.id == microlocation.id)\n        if view_kwargs.get('user_id') is not None:\n            user = safe_query(self, User, 'id', view_kwargs['user_id'], 'user_id')\n            query_ = query_.join(User).filter(User.id == user.id)\n        query_ = event_query(self, query_, view_kwargs)\n        if view_kwargs.get('speaker_id'):\n            speaker = safe_query(self, Speaker, 'id', view_kwargs['speaker_id'], 'speaker_id')\n            # session-speaker :: many-to-many relationship\n            query_ = Session.query.filter(Session.speakers.any(id=speaker.id))\n\n        return query_\n\n    view_kwargs = True\n    methods = ['GET']\n    schema = SessionSchema\n    data_layer = {'session': db.session,\n                  'model': Session,\n                  'methods': {\n                      'query': query\n                  }}\n\n\nclass SessionDetail(ResourceDetail):\n    \"\"\"\n    Session detail by id\n    \"\"\"\n    def before_get_object(self, view_kwargs):\n        \"\"\"\n        before get method to get the resource id for fetching details\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if view_kwargs.get('event_identifier'):\n            event = safe_query(self, Event, 'identifier', view_kwargs['event_identifier'], 'identifier')\n            view_kwargs['event_id'] = event.id\n\n    def after_update_object(self, session, data, view_kwargs):\n        \"\"\" Send email if session accepted or rejected \"\"\"\n        if 'state' in data and (session.state == 'accepted' or session.state == 'rejected'):\n            # Email for speaker\n            speakers = session.speakers\n            for speaker in speakers:\n                frontend_url = get_settings()['frontend_url']\n                link = \"{}/events/{}/sessions/{}\" \\\n                    .format(frontend_url, session.event_id, session.id)\n                send_email_session_accept_reject(speaker.email, session, link)\n                send_notif_session_accept_reject(speaker, session.title, session.state, link)\n\n            # Email for organizer\n            if session.event.get_organizer():\n                organizer = session.event.get_organizer()\n                organizer_email = organizer.email\n                frontend_url = get_settings()['frontend_url']\n                link = \"{}/events/{}/sessions/{}\" \\\n                    .format(frontend_url, session.event_id, session.id)\n                send_email_session_accept_reject(organizer_email, session,\n                                                 link)\n                send_notif_session_accept_reject(organizer, session.title,\n                                                 session.state, link)\n\n    decorators = (api.has_permission('is_speaker_for_session', methods=\"PATCH,DELETE\"),)\n    schema = SessionSchema\n    data_layer = {'session': db.session,\n                  'model': Session,\n                  'methods': {'before_get_object': before_get_object,\n                              'after_update_object': after_update_object}}\n\n\nclass SessionRelationshipRequired(ResourceRelationship):\n    \"\"\"\n    Session Relationship\n    \"\"\"\n    schema = SessionSchema\n    decorators = (api.has_permission('is_speaker_for_session', methods=\"PATCH,DELETE\"),)\n    methods = ['GET', 'PATCH']\n    data_layer = {'session': db.session,\n                  'model': Session}\n\n\nclass SessionRelationshipOptional(ResourceRelationship):\n    \"\"\"\n    Session Relationship\n    \"\"\"\n    schema = SessionSchema\n    decorators = (api.has_permission('is_speaker_for_session', methods=\"PATCH,DELETE\"),)\n    data_layer = {'session': db.session,\n                  'model': Session}\n", "path": "app/api/sessions.py"}], "after_files": [{"content": "from flask import request\nfrom flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship\nfrom flask_rest_jsonapi.exceptions import ObjectNotFound\n\nfrom app.api.bootstrap import api\nfrom app.api.helpers.db import safe_query, get_count\nfrom app.api.helpers.exceptions import ForbiddenException\nfrom app.api.helpers.permission_manager import has_access\nfrom app.api.helpers.query import event_query\nfrom app.api.helpers.utilities import require_relationship\nfrom app.api.schema.speakers import SpeakerSchema\nfrom app.models import db\nfrom app.models.event import Event\nfrom app.models.session import Session\nfrom app.models.speaker import Speaker\nfrom app.models.user import User\n\n\nclass SpeakerListPost(ResourceList):\n    \"\"\"\n    List and create speakers\n    \"\"\"\n\n    def before_post(self, args, kwargs, data):\n        \"\"\"\n        method to add user_id to view_kwargs before post\n        :param args:\n        :param kwargs:\n        :param data:\n        :return:\n        \"\"\"\n        require_relationship(['event', 'user'], data)\n\n        if not has_access('is_coorganizer', event_id=data['event']):\n                event = safe_query(self, Event, 'id', data['event'], 'event_id')\n                if event.state == \"draft\":\n                    raise ObjectNotFound({'parameter': 'event_id'},\n                                         \"Event: {} not found\".format(data['event_id']))\n\n        if get_count(db.session.query(Event).filter_by(id=int(data['event']), is_sessions_speakers_enabled=False)) > 0:\n            raise ForbiddenException({'pointer': ''}, \"Speakers are disabled for this Event\")\n\n        if 'sessions' in data:\n            session_ids = data['sessions']\n            for session_id in session_ids:\n                if not has_access('is_session_self_submitted', session_id=session_id):\n                    raise ObjectNotFound({'parameter': 'session_id'},\n                                         \"Session: {} not found\".format(session_id))\n\n    schema = SpeakerSchema\n    methods = ['POST', ]\n    data_layer = {'session': db.session,\n                  'model': Speaker\n                  }\n\n\nclass SpeakerList(ResourceList):\n    \"\"\"\n    List speakers based on different params from view_kwargs\n    \"\"\"\n\n    def query(self, view_kwargs):\n        \"\"\"\n        query method for speakers list class\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        query_ = self.session.query(Speaker)\n        query_ = event_query(self, query_, view_kwargs)\n\n        if view_kwargs.get('user_id'):\n            user = safe_query(self, User, 'id', view_kwargs['user_id'], 'user_id')\n            query_ = query_.join(User).filter(User.id == user.id)\n\n        if view_kwargs.get('session_id'):\n            session = safe_query(self, Session, 'id', view_kwargs['session_id'], 'session_id')\n            # session-speaker :: many-to-many relationship\n            query_ = Speaker.query.filter(Speaker.sessions.any(id=session.id))\n            if 'Authorization' in request.headers and not has_access('is_coorganizer', event_id=session.event_id):\n                if not has_access('is_session_self_submitted', session_id=session.id):\n                    query_ = query_.filter(Session.state == \"approved\" or Session.state == \"accepted\")\n\n        return query_\n\n    view_kwargs = True\n    schema = SpeakerSchema\n    methods = ['GET', ]\n    data_layer = {'session': db.session,\n                  'model': Speaker,\n                  'methods': {\n                      'query': query,\n                  }}\n\n\nclass SpeakerDetail(ResourceDetail):\n    \"\"\"\n    Speakers Detail by id\n    \"\"\"\n    decorators = (api.has_permission('is_coorganizer_or_user_itself', methods=\"PATCH,DELETE\", fetch=\"event_id\",\n                                     fetch_as=\"event_id\", model=Speaker),)\n    schema = SpeakerSchema\n    data_layer = {'session': db.session,\n                  'model': Speaker}\n\n\nclass SpeakerRelationshipRequired(ResourceRelationship):\n    \"\"\"\n    Speaker Relationship class for required entities\n    \"\"\"\n    decorators = (api.has_permission('is_coorganizer_or_user_itself', methods=\"PATCH,DELETE\", fetch=\"event_id\",\n                                     fetch_as=\"event_id\", model=Speaker),)\n    methods = ['GET', 'PATCH']\n    schema = SpeakerSchema\n    data_layer = {'session': db.session,\n                  'model': Speaker}\n\n\nclass SpeakerRelationshipOptional(ResourceRelationship):\n    \"\"\"\n    Speaker Relationship class\n    \"\"\"\n    decorators = (api.has_permission('is_coorganizer_or_user_itself', methods=\"PATCH,DELETE\", fetch=\"event_id\",\n                                     fetch_as=\"event_id\", model=Speaker),)\n    schema = SpeakerSchema\n    data_layer = {'session': db.session,\n                  'model': Speaker}\n", "path": "app/api/speakers.py"}, {"content": "from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship\n\nfrom app.api.bootstrap import api\nfrom app.api.events import Event\nfrom app.api.helpers.db import safe_query, get_count\nfrom app.api.helpers.exceptions import ForbiddenException\nfrom app.api.helpers.mail import send_email_new_session, send_email_session_accept_reject\nfrom app.api.helpers.notification import send_notif_new_session_organizer, send_notif_session_accept_reject\nfrom app.api.helpers.permissions import current_identity\nfrom app.api.helpers.query import event_query\nfrom app.api.helpers.utilities import require_relationship\nfrom app.api.schema.sessions import SessionSchema\nfrom app.models import db\nfrom app.models.microlocation import Microlocation\nfrom app.models.session import Session\nfrom app.models.session_type import SessionType\nfrom app.models.speaker import Speaker\nfrom app.models.track import Track\nfrom app.models.user import User\nfrom app.settings import get_settings\n\n\nclass SessionListPost(ResourceList):\n    \"\"\"\n    List Sessions\n    \"\"\"\n    def before_post(self, args, kwargs, data):\n        \"\"\"\n        before post method to check for required relationship and proper permission\n        :param args:\n        :param kwargs:\n        :param data:\n        :return:\n        \"\"\"\n        require_relationship(['event'], data)\n        data['creator_id'] = current_identity.id\n        if get_count(db.session.query(Event).filter_by(id=int(data['event']), is_sessions_speakers_enabled=False)) > 0:\n            raise ForbiddenException({'pointer': ''}, \"Sessions are disabled for this Event\")\n\n    def after_create_object(self, session, data, view_kwargs):\n        \"\"\"\n        method to send email for creation of new session\n        mails session link to the concerned user\n        :param session:\n        :param data:\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if session.event.get_organizer():\n            event_name = session.event.name\n            organizer = session.event.get_organizer()\n            organizer_email = organizer.email\n            frontend_url = get_settings()['frontend_url']\n            link = \"{}/events/{}/sessions/{}\"\\\n                .format(frontend_url, session.event_id, session.id)\n            send_email_new_session(organizer_email, event_name, link)\n            send_notif_new_session_organizer(organizer, event_name, link)\n\n    decorators = (api.has_permission('create_event'),)\n    schema = SessionSchema\n    data_layer = {'session': db.session,\n                  'model': Session,\n                  'methods': {'after_create_object': after_create_object\n                              }}\n\n\nclass SessionList(ResourceList):\n    \"\"\"\n    List Sessions\n    \"\"\"\n\n    def query(self, view_kwargs):\n        \"\"\"\n        query method for SessionList class\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        query_ = self.session.query(Session)\n        if view_kwargs.get('track_id') is not None:\n            track = safe_query(self, Track, 'id', view_kwargs['track_id'], 'track_id')\n            query_ = query_.join(Track).filter(Track.id == track.id)\n        if view_kwargs.get('session_type_id') is not None:\n            session_type = safe_query(self, SessionType, 'id', view_kwargs['session_type_id'], 'session_type_id')\n            query_ = query_.join(SessionType).filter(SessionType.id == session_type.id)\n        if view_kwargs.get('microlocation_id') is not None:\n            microlocation = safe_query(self, Microlocation, 'id', view_kwargs['microlocation_id'], 'microlocation_id')\n            query_ = query_.join(Microlocation).filter(Microlocation.id == microlocation.id)\n        if view_kwargs.get('user_id') is not None:\n            user = safe_query(self, User, 'id', view_kwargs['user_id'], 'user_id')\n            query_ = query_.join(User).filter(User.id == user.id)\n        query_ = event_query(self, query_, view_kwargs)\n        if view_kwargs.get('speaker_id'):\n            speaker = safe_query(self, Speaker, 'id', view_kwargs['speaker_id'], 'speaker_id')\n            # session-speaker :: many-to-many relationship\n            query_ = Session.query.filter(Session.speakers.any(id=speaker.id))\n\n        return query_\n\n    view_kwargs = True\n    methods = ['GET']\n    schema = SessionSchema\n    data_layer = {'session': db.session,\n                  'model': Session,\n                  'methods': {\n                      'query': query\n                  }}\n\n\nclass SessionDetail(ResourceDetail):\n    \"\"\"\n    Session detail by id\n    \"\"\"\n    def before_get_object(self, view_kwargs):\n        \"\"\"\n        before get method to get the resource id for fetching details\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if view_kwargs.get('event_identifier'):\n            event = safe_query(self, Event, 'identifier', view_kwargs['event_identifier'], 'identifier')\n            view_kwargs['event_id'] = event.id\n\n    def after_update_object(self, session, data, view_kwargs):\n        \"\"\" Send email if session accepted or rejected \"\"\"\n        if 'state' in data and (session.state == 'accepted' or session.state == 'rejected'):\n            # Email for speaker\n            speakers = session.speakers\n            for speaker in speakers:\n                frontend_url = get_settings()['frontend_url']\n                link = \"{}/events/{}/sessions/{}\" \\\n                    .format(frontend_url, session.event_id, session.id)\n                send_email_session_accept_reject(speaker.email, session, link)\n                send_notif_session_accept_reject(speaker, session.title, session.state, link)\n\n            # Email for organizer\n            if session.event.get_organizer():\n                organizer = session.event.get_organizer()\n                organizer_email = organizer.email\n                frontend_url = get_settings()['frontend_url']\n                link = \"{}/events/{}/sessions/{}\" \\\n                    .format(frontend_url, session.event_id, session.id)\n                send_email_session_accept_reject(organizer_email, session,\n                                                 link)\n                send_notif_session_accept_reject(organizer, session.title,\n                                                 session.state, link)\n\n    decorators = (api.has_permission('is_speaker_for_session', methods=\"PATCH,DELETE\"),)\n    schema = SessionSchema\n    data_layer = {'session': db.session,\n                  'model': Session,\n                  'methods': {'before_get_object': before_get_object,\n                              'after_update_object': after_update_object}}\n\n\nclass SessionRelationshipRequired(ResourceRelationship):\n    \"\"\"\n    Session Relationship\n    \"\"\"\n    schema = SessionSchema\n    decorators = (api.has_permission('is_speaker_for_session', methods=\"PATCH,DELETE\"),)\n    methods = ['GET', 'PATCH']\n    data_layer = {'session': db.session,\n                  'model': Session}\n\n\nclass SessionRelationshipOptional(ResourceRelationship):\n    \"\"\"\n    Session Relationship\n    \"\"\"\n    schema = SessionSchema\n    decorators = (api.has_permission('is_speaker_for_session', methods=\"PATCH,DELETE\"),)\n    data_layer = {'session': db.session,\n                  'model': Session}\n", "path": "app/api/sessions.py"}]}