Repository: fossasia/open-event-server

Issue:
 Send email for completion of import event
- [x] Send email (with link to imported event) to the user importing the event.


Discussion:

Resolving patch:
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,8 @@
 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, EVENT_EXPORTED, EVENT_EXPORT_FAIL
+    AFTER_EVENT, MONTHLY_PAYMENT_EMAIL, MONTHLY_PAYMENT_FOLLOWUP_EMAIL, EVENT_EXPORTED, EVENT_EXPORT_FAIL, \
+    EVENT_IMPORTED, EVENT_IMPORT_FAIL
 from app.models.user import User
 
 
@@ -244,6 +245,30 @@ def send_export_mail(email, event_name, error_text=None, download_url=None):
         )
 
 
+def send_import_mail(email, event_name=None, error_text=None, event_url=None):
+    """followup export link in email"""
+    if error_text:
+        send_email(
+            to=email,
+            action=EVENT_IMPORT_FAIL,
+            subject=MAILS[EVENT_IMPORT_FAIL]['subject'],
+            html=MAILS[EVENT_IMPORT_FAIL]['message'].format(
+                error_text=error_text
+            )
+        )
+    elif event_url:
+        send_email(
+            to=email,
+            action=EVENT_IMPORTED,
+            subject=MAILS[EVENT_IMPORTED]['subject'].format(
+                event_name=event_name
+            ),
+            html=MAILS[EVENT_IMPORTED]['message'].format(
+                event_url=event_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,7 +14,7 @@
 from flask import url_for
 
 from app.api.helpers.request_context_task import RequestContextTask
-from app.api.helpers.mail import send_export_mail
+from app.api.helpers.mail import send_export_mail, send_import_mail
 from app.api.helpers.db import safe_query
 from import_helpers import update_import_job
 from app.models.event import Event
@@ -80,17 +80,19 @@ def export_event_task(self, email, event_id, settings):
 
 
 @celery.task(base=RequestContextTask, name='import.event', bind=True)
-def import_event_task(self, file, source_type, creator_id):
+def import_event_task(self, email, file, source_type, creator_id):
     """Import Event Task"""
     task_id = self.request.id.__str__()  # str(async result)
     try:
         logging.info('Importing started')
         result = import_event_task_base(self, file, source_type, creator_id)
-        update_import_job(task_id, result, 'SUCCESS')
-        logging.info('Importing done..')
+        update_import_job(task_id, result['id'], 'SUCCESS')
+        logging.info('Importing done..Sending email')
+        send_import_mail(email=email, event_name=result['event_name'], event_url=result['url'])
     except Exception as e:
         print(traceback.format_exc())
         result = {'__error': True, 'result': str(e)}
         update_import_job(task_id, str(e), e.status if hasattr(e, 'status') else 'FAILURE')
+        send_import_mail(email=email, error_text=str(e))
 
     return result
diff --git a/app/api/imports.py b/app/api/imports.py
--- a/app/api/imports.py
+++ b/app/api/imports.py
@@ -4,6 +4,7 @@
 
 from app.api.helpers.import_helpers import get_file_from_request, import_event_json, create_import_job
 from app.api.helpers.utilities import TASK_RESULTS
+from app.api.helpers.files import make_frontend_url
 
 import_routes = Blueprint('imports', __name__, url_prefix='/v1')
 
@@ -17,8 +18,8 @@ def import_event(source_type):
         file_path = None
         abort(404)
     from helpers.tasks import import_event_task
-    task = import_event_task.delay(
-        file=file_path, source_type=source_type, creator_id=current_identity.id)
+    task = import_event_task.delay(email=current_identity.email, file=file_path,
+                                   source_type=source_type, creator_id=current_identity.id)
     # create import job
     create_import_job(task.id)
 
@@ -38,6 +39,10 @@ def import_event_task_base(task_handle, file_path, source_type='json', creator_i
     if source_type == 'json':
         new_event = import_event_json(task_handle, file_path)
     if new_event:
-        return url_for('v1.event_detail', id=new_event.id)
+        url = make_frontend_url(path='/events/{identifier}'.format(identifier=new_event.identifier))
+        return {'url': url,
+                'id': new_event.id,
+                'event_name': new_event.name}
+
     else:
         return None


Test patch: