Repository: fossasia/open-event-server

Issue:
Session creation with send-email field throws internal server error
**Describe the bug**
POST request to /v1/sessions/ with send-email field in the body causes internal server error.

**Expected behavior**
A new session should be created

**Stacktrace**
```
Traceback (most recent call last):
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/rs/Pradeep/github/open-event-server/app/__init__.py", line 64, in __call__
    return self.app(environ, start_response)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/api.py", line 133, in decorated
    return self.check_permissions(view, view_args, view_kwargs, *args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/app/api/helpers/permission_manager.py", line 431, in permission_manager
    return permissions[args[0]](view, view_args, view_kwargs, *args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/app/api/helpers/permission_manager.py", line 285, in create_event
    return view(*view_args, **view_kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 32, in wrapper
    return func(*args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/views.py", line 88, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 68, in dispatch_request
    response = method(*args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 56, in wrapper
    return func(*args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 204, in post
    obj = self._data_layer.create_

object(data, kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/data_layers/alchemy.py", line 38, in create_object
    obj = self.model(**{key: value for (key, value) in data.items() if key not in relationship_fields})
TypeError: __init__() got an unexpected keyword argument 'send_email'
```



Discussion:

Resolving patch:
diff --git a/app/models/session.py b/app/models/session.py
--- a/app/models/session.py
+++ b/app/models/session.py
@@ -49,6 +49,7 @@ class Session(db.Model):
     submission_modifier = db.Column(db.String)
     is_mail_sent = db.Column(db.Boolean, default=False)
     last_modified_at = db.Column(db.DateTime(timezone=True), default=datetime.datetime.utcnow)
+    send_email = db.Column(db.Boolean, nullable=True)
 
     def __init__(self,
                  title=None,
@@ -76,7 +77,8 @@ def __init__(self,
                  is_mail_sent=False,
                  deleted_at=None,
                  submitted_at=None,
-                 last_modified_at=None):
+                 last_modified_at=None,
+                 send_email=None):
 
         if speakers is None:
             speakers = []
@@ -107,6 +109,7 @@ def __init__(self,
         self.submitted_at = submitted_at
         self.submission_modifier = submission_modifier
         self.last_modified_at = datetime.datetime.now(pytz.utc)
+        self.send_email = send_email
 
     @staticmethod
     def get_service_name():
diff --git a/migrations/versions/49f3a33f5437_.py b/migrations/versions/49f3a33f5437_.py
new file mode 100644
--- /dev/null
+++ b/migrations/versions/49f3a33f5437_.py
@@ -0,0 +1,30 @@
+"""empty message
+
+Revision ID: 49f3a33f5437
+Revises: a3cf002f4da8
+Create Date: 2018-06-08 01:37:24.165316
+
+"""
+
+from alembic import op
+import sqlalchemy as sa
+import sqlalchemy_utils
+
+
+# revision identifiers, used by Alembic.
+revision = '49f3a33f5437'
+down_revision = 'a3cf002f4da8'
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('sessions', sa.Column('send_email', sa.Boolean(), nullable=True))
+    op.add_column('sessions_version', sa.Column('send_email', sa.Boolean(), autoincrement=False, nullable=True))
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.drop_column('sessions_version', 'send_email')
+    op.drop_column('sessions', 'send_email')
+    # ### end Alembic commands ###


Test patch: