Skip to content
Snippets Groups Projects
Commit b06de6c0 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch 'bug/kit_request_mail_crash' into 'master'

Bug/kit request mail crash

Closes #259

See merge request NCER-PD/scheduling-system!176
parents d0152cb0 286d0f70
No related branches found
No related tags found
1 merge request!176Bug/kit request mail crash
Pipeline #7242 passed
......@@ -160,7 +160,7 @@ def create_appointment_type(code='C', default_duration=10, description='test'):
default_duration=default_duration,
description=description,
)
def create_contact_attempt(subject=None, worker=None):
if subject is None:
......@@ -309,6 +309,14 @@ def create_appointment(visit=None, when=None, length=30):
datetime_when=when)
def create_appointment_without_visit(when=None, length=30):
return Appointment.objects.create(
length=length,
location=get_test_location(),
status=Appointment.APPOINTMENT_STATUS_SCHEDULED,
datetime_when=when)
def create_configuration_item():
item = ConfigurationItem.objects.create()
item.type = "TEST"
......
......@@ -5,12 +5,13 @@ from django.urls import reverse
from web.models import Item, Appointment, AppointmentTypeLink
from web.tests import LoggedInTestCase
from web.tests.functions import create_appointment_type, create_appointment, create_visit
from web.tests.functions import create_appointment_type, create_appointment, create_visit, create_appointment_without_visit
from web.views.kit import get_kit_requests
from web.views.notifications import get_today_midnight_date
class ViewFunctionsTests(LoggedInTestCase):
def test_kit_requests(self):
response = self.client.get(reverse('web.views.kit_requests'))
self.assertEqual(response.status_code, 200)
......@@ -25,7 +26,8 @@ class ViewFunctionsTests(LoggedInTestCase):
appointment = create_appointment()
appointment.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2)
appointment.save()
AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=appointment_type)
AppointmentTypeLink.objects.create(
appointment=appointment, appointment_type=appointment_type)
response = self.client.get(reverse('web.views.kit_requests'))
self.assertEqual(response.status_code, 200)
......@@ -43,7 +45,8 @@ class ViewFunctionsTests(LoggedInTestCase):
appointment.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2)
appointment.status = Appointment.APPOINTMENT_STATUS_CANCELLED
appointment.save()
AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=appointment_type)
AppointmentTypeLink.objects.create(
appointment=appointment, appointment_type=appointment_type)
response = self.client.get(reverse('web.views.kit_requests'))
self.assertEqual(response.status_code, 200)
......@@ -60,7 +63,8 @@ class ViewFunctionsTests(LoggedInTestCase):
appointment = create_appointment()
appointment.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2)
appointment.save()
AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=appointment_type)
AppointmentTypeLink.objects.create(
appointment=appointment, appointment_type=appointment_type)
response = self.client.get(reverse('web.views.kit_requests'))
self.assertEqual(response.status_code, 200)
......@@ -79,17 +83,20 @@ class ViewFunctionsTests(LoggedInTestCase):
appointment1 = create_appointment(visit)
appointment1.datetime_when = get_today_midnight_date() + datetime.timedelta(days=3)
appointment1.save()
AppointmentTypeLink.objects.create(appointment=appointment1, appointment_type=appointment_type)
AppointmentTypeLink.objects.create(
appointment=appointment1, appointment_type=appointment_type)
appointment2 = create_appointment(visit)
appointment2.datetime_when = get_today_midnight_date() + datetime.timedelta(days=4)
appointment2.save()
AppointmentTypeLink.objects.create(appointment=appointment2, appointment_type=appointment_type)
AppointmentTypeLink.objects.create(
appointment=appointment2, appointment_type=appointment_type)
appointment3 = create_appointment(visit)
appointment3.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2)
appointment3.save()
AppointmentTypeLink.objects.create(appointment=appointment3, appointment_type=appointment_type)
AppointmentTypeLink.objects.create(
appointment=appointment3, appointment_type=appointment_type)
result = get_kit_requests(self.user)
self.assertEqual(appointment3, result['appointments'][0])
......@@ -112,8 +119,10 @@ class ViewFunctionsTests(LoggedInTestCase):
appointment1 = create_appointment(visit)
appointment1.datetime_when = get_today_midnight_date() + datetime.timedelta(days=3)
appointment1.save()
AppointmentTypeLink.objects.create(appointment=appointment1, appointment_type=appointment_type)
AppointmentTypeLink.objects.create(appointment=appointment1, appointment_type=appointment_type2)
AppointmentTypeLink.objects.create(
appointment=appointment1, appointment_type=appointment_type)
AppointmentTypeLink.objects.create(
appointment=appointment1, appointment_type=appointment_type2)
result = get_kit_requests(self.user)
......@@ -129,7 +138,8 @@ class ViewFunctionsTests(LoggedInTestCase):
appointment = create_appointment()
appointment.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2)
appointment.save()
AppointmentTypeLink.objects.create(appointment=appointment, appointment_type=appointment_type)
AppointmentTypeLink.objects.create(
appointment=appointment, appointment_type=appointment_type)
response = self.client.get(reverse('web.views.kit_requests_send_mail',
kwargs={'start_date': str(get_today_midnight_date().strftime("%Y-%m-%d"))}))
......@@ -138,3 +148,20 @@ class ViewFunctionsTests(LoggedInTestCase):
self.assertTrue(item_name in response.content)
self.assertEqual(1, len(mail.outbox))
def test_kit_request_send_mail_with_general_appointment(self):
item_name = "Test item to be ordered"
item = Item.objects.create(disposable=True, name=item_name)
appointment_type = create_appointment_type()
appointment_type.required_equipment.add(item)
appointment_type.save()
appointment = create_appointment_without_visit()
appointment.datetime_when = get_today_midnight_date() + datetime.timedelta(days=2)
appointment.save()
AppointmentTypeLink.objects.create(
appointment=appointment, appointment_type=appointment_type)
response = self.client.get(reverse('web.views.kit_requests_send_mail',
kwargs={'start_date': str(get_today_midnight_date().strftime("%Y-%m-%d"))}))
self.assertEqual(response.status_code, 200)
self.assertTrue(item_name in response.content)
self.assertEqual(1, len(mail.outbox))
......@@ -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,13 @@ 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>"
if appointment.visit is not None and appointment.visit.subject is not None:
email_body += "<td style='" + cell_style + "'>" + \
appointment.visit.subject.nd_number + "</td>"
else:
email_body += "<td style='" + cell_style + "'>" + '-' + "</td>"
email_body += "<td style='" + cell_style + "'>"
for type in appointment.appointment_types.all():
for item in type.required_equipment.all():
......@@ -118,11 +125,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 +141,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 +165,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 +184,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 +195,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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment