diff --git a/smash/web/tests/functions.py b/smash/web/tests/functions.py index 617756cbd2fa6b5530da8a9afcb0b6dd82b8c6b2..01d3c9a27d80bfabbcc3ff011a934304633ef3ca 100644 --- a/smash/web/tests/functions.py +++ b/smash/web/tests/functions.py @@ -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" diff --git a/smash/web/tests/view/test_kit_request.py b/smash/web/tests/view/test_kit_request.py index e1c4a1e79613dc93f1c6c682f59c50d91a56a48c..387001f3bd804c87fef7db3dccac4de4897dd97f 100644 --- a/smash/web/tests/view/test_kit_request.py +++ b/smash/web/tests/view/test_kit_request.py @@ -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)) diff --git a/smash/web/views/kit.py b/smash/web/views/kit.py index 6448b09851f0412154b567ce5b077241b7448cd2..79c42ef5fb89430026ceaa05c20e766f7175e7ca 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,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