Repository: fossasia/open-event-server

Issue:
Add session types api
parent issue: #3870 


Discussion:

Resolving patch:
diff --git a/app/api/session_types.py b/app/api/session_types.py
--- a/app/api/session_types.py
+++ b/app/api/session_types.py
@@ -46,7 +46,7 @@ def validate_length(self, data):
                          related_view_kwargs={'session_type_id': '<id>'},
                          schema='EventSchema',
                          type_='event')
-    sessions = Relationship(attribute='session',
+    sessions = Relationship(attribute='sessions',
                             self_view='v1.session_type_sessions',
                             self_view_kwargs={'id': '<id>'},
                             related_view='v1.session_list',
diff --git a/app/factories/session_type.py b/app/factories/session_type.py
new file mode 100644
--- /dev/null
+++ b/app/factories/session_type.py
@@ -0,0 +1,14 @@
+import factory
+from app.models.session_type import db, SessionType
+from app.factories.event import EventFactoryBasic
+import app.factories.common as common
+
+
+class SessionTypeFactory(factory.alchemy.SQLAlchemyModelFactory):
+    class Meta:
+        model = SessionType
+        sqlalchemy_session = db.session
+
+    event = factory.RelatedFactory(EventFactoryBasic)
+    name = common.string_
+    length = '00:30'
diff --git a/app/models/session_type.py b/app/models/session_type.py
--- a/app/models/session_type.py
+++ b/app/models/session_type.py
@@ -8,8 +8,8 @@ class SessionType(db.Model):
     length = db.Column(db.String, nullable=False)
     event_id = db.Column(
         db.Integer, db.ForeignKey('events.id', ondelete='CASCADE'))
-    events = db.relationship("Event", backref="session_type")
-    session = db.relationship('Session', backref="session_type")
+    event = db.relationship("Event", backref="session_type", foreign_keys=[event_id])
+    sessions = db.relationship('Session', backref="session_type")
 
     def __init__(self, name=None, length=None, event_id=None):
         self.name = name


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -33,6 +33,7 @@
 from app.factories.session import SessionFactory
 from app.factories.ticket import TicketFactory
 from app.factories.attendee import AttendeeFactory
+from app.factories.session_type import SessionTypeFactory
 from app.factories.track import TrackFactory
 
 stash = {}
@@ -491,7 +492,10 @@ def session_type_get_list(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        session_type = SessionTypeFactory()
+        db.session.add(session_type)
+        db.session.commit()
 
 
 @hooks.before("Session Type > Session Type Collection > Create Session Type")
@@ -501,7 +505,10 @@ def session_type_post(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
 
 
 @hooks.before("Session Type > Session Type Details > Session Type Details")
@@ -511,7 +518,10 @@ def session_type_get_detail(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        session_type = SessionTypeFactory()
+        db.session.add(session_type)
+        db.session.commit()
 
 
 @hooks.before("Session Type > Session Type Details > Update Session Type")
@@ -521,7 +531,10 @@ def session_type_patch(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        session_type = SessionTypeFactory()
+        db.session.add(session_type)
+        db.session.commit()
 
 
 @hooks.before("Session Type > Session Type Details > Delete Session Type")
@@ -531,7 +544,10 @@ def session_type_delete(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        session_type = SessionTypeFactory()
+        db.session.add(session_type)
+        db.session.commit()
 
 
 # ------------------------- Social Links -------------------------