From ba7789224bf241e642d3c05d16f8ac92b395f00b Mon Sep 17 00:00:00 2001
From: Patrick Jentsch <p.jentsch@uni-bielefeld.de>
Date: Thu, 19 Nov 2020 09:41:22 +0100
Subject: [PATCH] Create send_notification function

---
 web/app/models.py          |  3 ++-
 web/app/tasks/job_utils.py | 32 ++++++++++++++++----------------
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/web/app/models.py b/web/app/models.py
index e912bf17..4dd9dba9 100644
--- a/web/app/models.py
+++ b/web/app/models.py
@@ -140,7 +140,8 @@ class User(UserMixin, db.Model):
 
     @property
     def path(self):
-        return os.path.join(current_app.config['NOPAQUE_DATA_DIR'], str(self.id))
+        return os.path.join(current_app.config['NOPAQUE_DATA_DIR'],
+                            str(self.id))
 
     @property
     def password(self):
diff --git a/web/app/tasks/job_utils.py b/web/app/tasks/job_utils.py
index 7a4e2109..d4f845fd 100644
--- a/web/app/tasks/job_utils.py
+++ b/web/app/tasks/job_utils.py
@@ -41,13 +41,7 @@ def create_job_service(job):
     else:
         job.status = 'queued'
     finally:
-        msg = create_message(
-            job.creator.email,
-            'Status update for your Job "{}"'.format(job.title),
-            'tasks/email/notification',
-            job=job
-        )
-        mail.send(msg)
+        send_notification(job)
 
 
 def checkout_job_service(job):
@@ -85,8 +79,6 @@ def checkout_job_service(job):
                               + 'Details: {}'.format(e))
                 return
             else:
-                job.end_date = datetime.utcnow()
-                job.status = task_state
                 if task_state == 'complete':
                     job_results_dir = os.path.join(job.path, 'output')
                     job_results = filter(lambda x: x.endswith('.zip'),
@@ -94,14 +86,10 @@ def checkout_job_service(job):
                     for job_result in job_results:
                         job_result = JobResult(filename=job_result, job=job)
                         db.session.add(job_result)
+                job.end_date = datetime.utcnow()
+                job.status = task_state
     finally:
-        msg = create_message(
-            job.creator.email,
-            'Status update for your Job "{}"'.format(job.title),
-            'tasks/email/notification',
-            job=job
-        )
-        mail.send(msg)
+        send_notification(job)
 
 
 def remove_job_service(job):
@@ -134,3 +122,15 @@ def remove_job_service(job):
             logging.error('Remove "{}" service raised '.format(service_name)  # noqa
                           + '[docker-APIError] The server returned an error. '  # noqa
                           + 'Details: {}'.format(e))
+
+
+def send_notification(job):
+    if job.creator.setting_job_status_mail_notifications == 'none':
+        return
+    if (job.creator.setting_job_status_mail_notifications == 'end'
+            and job.status not in ['complete', 'failed']):
+        return
+    msg = create_message(job.creator.email,
+                         'Status update for your Job "{}"'.format(job.title),
+                         'tasks/email/notification', job=job)
+    mail.send(msg)
-- 
GitLab