Repository: fossasia/open-event-server

Issue:
Save export job tasks in the DB
 Store the export job that was started by a particular email id in the Database. Continuation of #3731


Discussion:

Resolving patch:
diff --git a/app/api/exports.py b/app/api/exports.py
--- a/app/api/exports.py
+++ b/app/api/exports.py
@@ -5,6 +5,7 @@
 
 from app.api.helpers.export_helpers import export_event_json, create_export_job
 from app.api.helpers.utilities import TASK_RESULTS
+from flask_jwt import jwt_required
 
 export_routes = Blueprint('exports', __name__, url_prefix='/v1')
 
@@ -17,6 +18,7 @@
 
 
 @export_routes.route('/events/<string:event_id>/export/json', methods=['POST'])
+@jwt_required()
 def export_event(event_id):
     from helpers.tasks import export_event_task
 
@@ -29,10 +31,8 @@ def export_event(event_id):
     task = export_event_task.delay(
         event_id, settings)
     # create Job
-    try:
-        create_export_job(task.id, event_id)
-    except Exception:
-        pass
+    create_export_job(task.id, event_id)
+
     # in case of testing
     if current_app.config.get('CELERY_ALWAYS_EAGER'):
         # send_export_mail(event_id, task.get())
@@ -46,6 +46,7 @@ def export_event(event_id):
 
 
 @export_routes.route('/events/<string:event_id>/exports/<path:path>')
+@jwt_required()
 def export_download(event_id, path):
     if not path.startswith('/'):
         path = '/' + path
diff --git a/app/api/helpers/export_helpers.py b/app/api/helpers/export_helpers.py
--- a/app/api/helpers/export_helpers.py
+++ b/app/api/helpers/export_helpers.py
@@ -3,10 +3,12 @@
 import shutil
 from collections import OrderedDict
 from datetime import datetime
+import pytz
 
 import requests
 from flask import current_app as app
-from flask import request, g, url_for
+from flask import request, url_for
+from flask_jwt import current_identity
 
 from app.api.helpers.db import save_to_db
 from app.models import db
@@ -237,13 +239,14 @@ def create_export_job(task_id, event_id):
     export_job = ExportJob.query.filter_by(event_id=event_id).first()
     task_url = url_for('tasks.celery_task', task_id=task_id)
     if export_job:
+
         export_job.task = task_url
-        export_job.user_email = g.user.email
+        export_job.user_email = current_identity.email
         export_job.event = Event.query.get(event_id)
-        export_job.starts_at = datetime.now()
+        export_job.starts_at = datetime.now(pytz.utc)
     else:
         export_job = ExportJob(
-            task=task_url, user_email=g.user.email,
+            task=task_url, user_email=current_identity.email,
             event=Event.query.get(event_id)
         )
     save_to_db(export_job, 'ExportJob saved')
diff --git a/migrations/versions/bf3e8c2a01f6_.py b/migrations/versions/bf3e8c2a01f6_.py
new file mode 100644
--- /dev/null
+++ b/migrations/versions/bf3e8c2a01f6_.py
@@ -0,0 +1,48 @@
+"""empty message
+
+Revision ID: bf3e8c2a01f6
+Revises: dbb9670cd902
+Create Date: 2017-08-08 17:09:52.402284
+
+"""
+
+from alembic import op
+import sqlalchemy as sa
+import sqlalchemy_utils
+
+
+# revision identifiers, used by Alembic.
+revision = 'bf3e8c2a01f6'
+down_revision = 'dbb9670cd902'
+
+
+def upgrade():
+    # commands auto generated by Alembic - please adjust! #
+    op.create_table('export_jobs',
+    sa.Column('id', sa.Integer(), nullable=False),
+    sa.Column('task', sa.String(), nullable=False),
+    sa.Column('starts_at', sa.DateTime(timezone=True), nullable=True),
+    sa.Column('user_email', sa.String(), nullable=True),
+    sa.Column('event_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['event_id'], ['events.id'], ondelete='CASCADE'),
+    sa.PrimaryKeyConstraint('id')
+    )
+    op.create_table('stripe_authorizations',
+    sa.Column('id', sa.Integer(), nullable=False),
+    sa.Column('stripe_secret_key', sa.String(), nullable=True),
+    sa.Column('stripe_refresh_token', sa.String(), nullable=True),
+    sa.Column('stripe_publishable_key', sa.String(), nullable=True),
+    sa.Column('stripe_user_id', sa.String(), nullable=True),
+    sa.Column('stripe_email', sa.String(), nullable=True),
+    sa.Column('event_id', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['event_id'], ['events.id'], ondelete='CASCADE'),
+    sa.PrimaryKeyConstraint('id')
+    )
+    # end Alembic commands #
+
+
+def downgrade():
+    # commands auto generated by Alembic - please adjust! #
+    op.drop_table('stripe_authorizations')
+    op.drop_table('export_jobs')
+    # end Alembic commands #


Test patch: