Repository: fossasia/open-event-server

Issue:
Send exported events link as email
Parent Issue #4196

- [x] Send email to the organizer or co organizer of an event the link to download the exported content of the event in zip format


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,7 +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
+from flask_jwt import jwt_required, current_identity
 
 export_routes = Blueprint('exports', __name__, url_prefix='/v1')
 
@@ -29,7 +29,7 @@ def export_event(event_id):
     settings['audio'] = request.json.get('audio', False)
     # queue task
     task = export_event_task.delay(
-        event_id, settings)
+        current_identity.email, event_id, settings)
     # create Job
     create_export_job(task.id, event_id)
 
diff --git a/app/api/helpers/mail.py b/app/api/helpers/mail.py
--- a/app/api/helpers/mail.py
+++ b/app/api/helpers/mail.py
@@ -10,7 +10,7 @@
 from app.api.helpers.system_mails import MAILS
 from app.api.helpers.utilities import string_empty, get_serializer, str_generator
 from app.models.mail import Mail, USER_CONFIRM, NEW_SESSION, USER_CHANGE_EMAIL, SESSION_ACCEPT_REJECT, EVENT_ROLE, \
-    AFTER_EVENT, MONTHLY_PAYMENT_EMAIL, MONTHLY_PAYMENT_FOLLOWUP_EMAIL
+    AFTER_EVENT, MONTHLY_PAYMENT_EMAIL, MONTHLY_PAYMENT_FOLLOWUP_EMAIL, EVENT_EXPORTED, EVENT_EXPORT_FAIL
 from app.models.user import User
 
 
@@ -218,6 +218,32 @@ def send_followup_email_for_monthly_fee_payment(email, event_name, previous_mont
     )
 
 
+def send_export_mail(email, event_name, error_text=None, download_url=None):
+    """followup export link in email"""
+    if error_text:
+        send_email(
+            to=email,
+            action=EVENT_EXPORT_FAIL,
+            subject=MAILS[EVENT_EXPORT_FAIL]['subject'].format(
+                event_name=event_name
+            ),
+            html=MAILS[EVENT_EXPORT_FAIL]['message'].format(
+                error_text=error_text
+            )
+        )
+    elif download_url:
+        send_email(
+            to=email,
+            action=EVENT_EXPORTED,
+            subject=MAILS[EVENT_EXPORTED]['subject'].format(
+                event_name=event_name
+            ),
+            html=MAILS[EVENT_EXPORTED]['message'].format(
+                download_url=download_url
+            )
+        )
+
+
 def send_email_change_user_email(user, email):
     s = get_serializer()
     hash = base64.b64encode(s.dumps([email, str_generator()]))
diff --git a/app/api/helpers/tasks.py b/app/api/helpers/tasks.py
--- a/app/api/helpers/tasks.py
+++ b/app/api/helpers/tasks.py
@@ -14,8 +14,10 @@
 from flask import url_for
 
 from app.api.helpers.request_context_task import RequestContextTask
-# from errors import BaseError, ServerError
-# from export_helpers import send_export_mail
+from app.api.helpers.mail import send_export_mail
+from app.api.helpers.db import safe_query
+from app.models.event import Event
+from app.models import db
 from app.api.exports import event_export_task_base
 from app.settings import get_settings
 
@@ -53,25 +55,23 @@ def send_mail_via_smtp_task(config, payload):
 
 
 @celery.task(base=RequestContextTask, name='export.event', bind=True)
-def export_event_task(self, event_id, settings):
+def export_event_task(self, email, event_id, settings):
+    event = safe_query(db, Event, 'id', event_id, 'event_id')
     try:
         logging.info('Exporting started')
         path = event_export_task_base(event_id, settings)
         # task_id = self.request.id.__str__()  # str(async result)
-        if get_settings()['storage_place'] == 'local' or get_settings()['storage_place'] == None:
-            download_url = url_for(
-                'exports.export_download', event_id=event_id, path=path
-            )
-        else:
-            download_url = path
+        download_url = path
 
         result = {
             'download_url': download_url
         }
+        logging.info('Exporting done.. sending email')
+        send_export_mail(email=email, event_name=event.name, download_url=download_url)
     except Exception as e:
         print(traceback.format_exc())
-        result = {'__error': True, 'result': e}
-    logging.info('Exporting done.. sending email')
-    # send email
-    # send_export_mail(event_id, result)
+        result = {'__error': True, 'result': str(e)}
+        logging.info('Error in exporting.. sending email')
+        send_export_mail(email=email, event_name=event.name, error_text=str(e))
+
     return result


Test patch: