Repository: fossasia/open-event-server

Issue:
Save notifications for completion of export event
Parent Issue #4196

- [x] Save notifications to the user  with the link for the exported event download.



Discussion:

Resolving patch:
diff --git a/app/api/helpers/notification.py b/app/api/helpers/notification.py
--- a/app/api/helpers/notification.py
+++ b/app/api/helpers/notification.py
@@ -2,7 +2,7 @@
 
 from app.api.helpers.db import save_to_db
 from app.models.notification import Notification, NEW_SESSION, SESSION_ACCEPT_REJECT, \
-    EVENT_IMPORTED, EVENT_IMPORT_FAIL
+    EVENT_IMPORTED, EVENT_IMPORT_FAIL, EVENT_EXPORTED, EVENT_EXPORT_FAIL
 from app.models.message_setting import MessageSettings
 from app.api.helpers.log import record_activity
 from app.api.helpers.system_notifications import NOTIFS
@@ -56,7 +56,7 @@ def send_notif_after_import(user, event_name=None, event_url=None, error_text=No
             message=NOTIFS[EVENT_IMPORT_FAIL]['message'].format(
                 error_text=error_text)
         )
-    else:
+    elif event_name:
         send_notification(
             user=user,
             action=EVENT_IMPORTED,
@@ -64,3 +64,23 @@ def send_notif_after_import(user, event_name=None, event_url=None, error_text=No
             message=NOTIFS[EVENT_IMPORTED]['message'].format(
                 event_name=event_name, event_url=event_url)
         )
+
+
+def send_notif_after_export(user, event_name, download_url=None, error_text=None):
+    """send notification after event import"""
+    if error_text:
+        send_notification(
+            user=user,
+            action=EVENT_EXPORT_FAIL,
+            title=NOTIFS[EVENT_EXPORT_FAIL]['title'].format(event_name=event_name),
+            message=NOTIFS[EVENT_EXPORT_FAIL]['message'].format(
+                error_text=error_text)
+        )
+    elif download_url:
+        send_notification(
+            user=user,
+            action=EVENT_EXPORTED,
+            title=NOTIFS[EVENT_EXPORTED]['title'].format(event_name=event_name),
+            message=NOTIFS[EVENT_EXPORTED]['message'].format(
+                event_name=event_name, download_url=download_url)
+        )
diff --git a/app/api/helpers/system_notifications.py b/app/api/helpers/system_notifications.py
--- a/app/api/helpers/system_notifications.py
+++ b/app/api/helpers/system_notifications.py
@@ -39,9 +39,9 @@
     },
     EVENT_IMPORTED: {
         'recipient': 'User',
-        'title': u'Event {event_name} has been exported',
+        'title': u'Event {event_name} has been imported',
         'message': (
-            u"Event <strong>{event_name}</strong> has been exported successfully."
+            u"Event <strong>{event_name}</strong> has been imported successfully."
             u"<br><br><a href='{event_url}' class='btn btn-info btn-sm'>View Event</a>"
         )
     },
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
@@ -15,7 +15,7 @@
 
 from app.api.helpers.request_context_task import RequestContextTask
 from app.api.helpers.mail import send_export_mail, send_import_mail
-from app.api.helpers.notification import send_notif_after_import
+from app.api.helpers.notification import send_notif_after_import, send_notif_after_export
 from app.api.helpers.db import safe_query
 from import_helpers import update_import_job
 from app.models.event import Event
@@ -61,6 +61,7 @@ def send_mail_via_smtp_task(config, payload):
 @celery.task(base=RequestContextTask, name='export.event', bind=True)
 def export_event_task(self, email, event_id, settings):
     event = safe_query(db, Event, 'id', event_id, 'event_id')
+    user = db.session.query(User).filter_by(email=email).first()
     try:
         logging.info('Exporting started')
         path = event_export_task_base(event_id, settings)
@@ -72,11 +73,13 @@ def export_event_task(self, email, event_id, settings):
         }
         logging.info('Exporting done.. sending email')
         send_export_mail(email=email, event_name=event.name, download_url=download_url)
+        send_notif_after_export(user=user, event_name=event.name, download_url=download_url)
     except Exception as e:
         print(traceback.format_exc())
         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))
+        send_notif_after_export(user=user, event_name=event.name, error_text=str(e))
 
     return result
 


Test patch: