From 376c7777d5541cf7304a9c660c9a6a19d416134a Mon Sep 17 00:00:00 2001
From: Carlos Vega <carlos.vega@uni.lu>
Date: Tue, 30 Oct 2018 11:34:45 +0100
Subject: [PATCH] if it fails, show the exception message in the log

---
 smash/web/views/kit.py | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/smash/web/views/kit.py b/smash/web/views/kit.py
index 6448b098..40be62c7 100644
--- a/smash/web/views/kit.py
+++ b/smash/web/views/kit.py
@@ -39,7 +39,8 @@ def get_kit_requests(user, start_date=None, end_date=None):
         if (end_date is not None) and (isinstance(end_date, unicode)):
             end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
 
-    appointment_types = AppointmentType.objects.filter(required_equipment__disposable=True)
+    appointment_types = AppointmentType.objects.filter(
+        required_equipment__disposable=True)
 
     appointments = Appointment.objects.filter(
         appointment_types__in=appointment_types,
@@ -83,7 +84,8 @@ def send_mail(data):
     end_date_str = " end of time"
     if data["end_date"] is not None:
         end_date_str = data["end_date"].strftime('%Y-%m-%d')
-    title = "Samples between " + data["start_date"].strftime('%Y-%m-%d') + " and " + end_date_str
+    title = "Samples between " + \
+        data["start_date"].strftime('%Y-%m-%d') + " and " + end_date_str
 
     cell_style = "padding: 8px; line-height: 1.42857143; vertical-align: top; " \
                  "font-size: 14px; font-family: 'Source Sans Pro','Helvetica Neue',Helvetica,Arial,sans-serif;"
@@ -106,8 +108,10 @@ def send_mail(data):
         if even:
             row_style = ' background-color: #f9f9f9;'
         email_body += "<tr style='" + row_style + "'>"
-        email_body += "<td style='" + cell_style + "'>" + appointment.datetime_when.strftime('%Y-%m-%d %H:%M') + "</td>"
-        email_body += "<td style='" + cell_style + "'>" + appointment.visit.subject.nd_number + "</td>"
+        email_body += "<td style='" + cell_style + "'>" + \
+            appointment.datetime_when.strftime('%Y-%m-%d %H:%M') + "</td>"
+        email_body += "<td style='" + cell_style + "'>" + \
+            appointment.visit.subject.nd_number + "</td>"
         email_body += "<td style='" + cell_style + "'>"
         for type in appointment.appointment_types.all():
             for item in type.required_equipment.all():
@@ -118,11 +122,14 @@ def send_mail(data):
         if appointment.flying_team is not None:
             location += " (" + unicode(appointment.flying_team) + ")"
         email_body += "<td style='" + cell_style + "'>" + location + "</td>"
-        email_body += "<td style='" + cell_style + "'>" + unicode(appointment.worker_assigned) + "</td>"
+        email_body += "<td style='" + cell_style + "'>" + \
+            unicode(appointment.worker_assigned) + "</td>"
         email_body += "</tr>"
     email_body += "</tbody></table>"
-    recipients = ConfigurationItem.objects.get(type=KIT_RECIPIENT_EMAIL_CONFIGURATION_TYPE).value
+    recipients = ConfigurationItem.objects.get(
+        type=KIT_RECIPIENT_EMAIL_CONFIGURATION_TYPE).value
     cc_recipients = []
+    logger.warn('Calling to send email')
     EmailSender().send_email(title, email_body, recipients, cc_recipients)
 
 
@@ -131,8 +138,11 @@ def kit_requests_send_mail(request, start_date, end_date=None):
     try:
         send_mail(data)
         messages.add_message(request, messages.SUCCESS, 'Mail sent')
-    except:
-        messages.add_message(request, messages.ERROR, 'There was problem with sending email')
+    except Exception as e:
+        logger.warn('Kit Request Send Mail Failed: |{}|\n{}'.format(
+            e.message, e.args))
+        messages.add_message(request, messages.ERROR,
+                             'There was problem with sending email')
     return wrap_response(request, 'equipment_and_rooms/kit_requests/kit_requests.html', get_kit_requests_data(request))
 
 
@@ -152,7 +162,8 @@ class KitRequestEmailSendJob(CronJobBase):
         date = now.replace(hour=hour, minute=minute)
         # TODO it's a hack assuming that we are in CEST
         date = pytz.utc.localize(date - datetime.timedelta(minutes=122))
-        jobs = CronJobLog.objects.filter(code=KitRequestEmailSendJob.code, message="mail sent", start_time__gte=date)
+        jobs = CronJobLog.objects.filter(
+            code=KitRequestEmailSendJob.code, message="mail sent", start_time__gte=date)
 
         if jobs.count() == 0:
             if pytz.utc.localize(datetime.datetime.utcnow()) > date:
@@ -170,7 +181,8 @@ class KitRequestEmailSendJob(CronJobBase):
             return "mail already sent"
 
     def match_day_of_week(self):
-        user_day_of_week = ConfigurationItem.objects.get(type=KIT_EMAIL_DAY_OF_WEEK_CONFIGURATION_TYPE).value
+        user_day_of_week = ConfigurationItem.objects.get(
+            type=KIT_EMAIL_DAY_OF_WEEK_CONFIGURATION_TYPE).value
         language = Language.objects.get(name="English")
         locale_name = language.locale
         if platform.system() == 'Windows':
@@ -180,7 +192,8 @@ class KitRequestEmailSendJob(CronJobBase):
         except:
             logger.error("Problem with setting locale: " + locale_name)
 
-        user_day_of_week_int = int(time.strptime(user_day_of_week, '%A').tm_wday) + 1
+        user_day_of_week_int = int(time.strptime(
+            user_day_of_week, '%A').tm_wday) + 1
         current_day_of_week_int = int(datetime.datetime.now().strftime("%w"))
 
         return user_day_of_week_int == current_day_of_week_int
-- 
GitLab