diff --git a/smash/web/models/mail_template.py b/smash/web/models/mail_template.py index 5d6e081bcf38a7f96834546549dc4f961e030407..d4f72d1867a4de358b55957fbf9cb3686f40b960 100644 --- a/smash/web/models/mail_template.py +++ b/smash/web/models/mail_template.py @@ -22,6 +22,10 @@ DATE_FORMAT_TIME = "%H:%M" now = datetime.datetime.now() +def date_format_encoding(): + return locale.getlocale(locale.LC_TIME)[1] or locale.getpreferredencoding() + + @contextmanager def setlocale(name): saved = locale.setlocale(locale.LC_TIME) @@ -31,9 +35,14 @@ def setlocale(name): locale.setlocale(locale.LC_TIME, saved) +def get_formatted_time(time_format): + return now.strftime(time_format).decode(date_format_encoding()) + + class MailTemplate(models.Model): MAILS_TEMPLATE_GENERIC_TAGS = [ - ("##DATE_FULL##", "Current date when the mail will be generated (long format)", now.strftime(DATE_FORMAT_FULL)), + ("##DATE_FULL##", "Current date when the mail will be generated (long format)", + get_formatted_time(DATE_FORMAT_FULL)), ("##DATE_SHORT##", "Current date when the mail will be generated (short format)", now.strftime(DATE_FORMAT_SHORT)), ("##WORKER##", "The full name of the currently logged in user", "") @@ -49,7 +58,7 @@ class MailTemplate(models.Model): ("##S_COUNTRY##", "Subject's country of residence", ""), ("##S_SEX##", "Subject's gender", "Male/Female"), ("##S_TYPE##", "Subject's type", "CONTROL/PATIENT"), - ("##S_DATE_BORN##", "Subject's date of birth", now.strftime(DATE_FORMAT_SHORT)), + ("##S_DATE_BORN##", "Subject's date of birth", get_formatted_time(DATE_FORMAT_SHORT)), ("##S_EMAIL##", "Subject's email address", ""), ("##S_PHONE_NUMBER##", "Subject's phone number", ""), @@ -63,20 +72,20 @@ class MailTemplate(models.Model): ("##S_DIAGNOSIS_YEAR##", "Subject's year of diagnosis", ""), ("##S_MPOWER_ID##", "Subject's mPower identifier", ""), ("##S_ND_NUMBER##", "Subject's ND number", ""), - ("##S_DATE_ADDED##", "Subject's date of creation", now.strftime(DATE_FORMAT_SHORT)), + ("##S_DATE_ADDED##", "Subject's date of creation", get_formatted_time(DATE_FORMAT_SHORT)), ] MAILS_TEMPLATE_VISIT_TAGS = [ - ("##V_DATE_START_FULL##", "Visit's start date", now.strftime(DATETIME_FORMAT)), - ("##V_DATE_START_SHORT##", "Visit's start date", now.strftime(DATE_FORMAT_SHORT)), - ("##V_DATE_ENDS_FULL##", "Visit's end date", now.strftime(DATETIME_FORMAT)), - ("##V_DATE_ENDS_SHORT##", "Visit's end date", now.strftime(DATE_FORMAT_SHORT)), + ("##V_DATE_START_FULL##", "Visit's start date", get_formatted_time(DATETIME_FORMAT)), + ("##V_DATE_START_SHORT##", "Visit's start date", get_formatted_time(DATE_FORMAT_SHORT)), + ("##V_DATE_ENDS_FULL##", "Visit's end date", get_formatted_time(DATETIME_FORMAT)), + ("##V_DATE_ENDS_SHORT##", "Visit's end date", get_formatted_time(DATE_FORMAT_SHORT)), ] MAILS_TEMPLATE_APPOINTMENT_TAGS = [ - ("##A_DATE_FULL##", "Appointment's date and time", now.strftime(DATETIME_FORMAT)), - ("##A_DATE_SHORT##", "Appointment's date", now.strftime(DATE_FORMAT_SHORT)), - ("##A_TIME##", "Appointment's time", now.strftime(DATE_FORMAT_TIME)), + ("##A_DATE_FULL##", "Appointment's date and time", get_formatted_time(DATETIME_FORMAT)), + ("##A_DATE_SHORT##", "Appointment's date", get_formatted_time(DATE_FORMAT_SHORT)), + ("##A_TIME##", "Appointment's time", get_formatted_time(DATE_FORMAT_TIME)), ("##A_FLYING_TEAM##", "Appointment's flying team location", ""), ("##A_LOCATION##", "Appointment's location", "value can be 'Flying Team'"), ("##A_LOCATION_OR_FLYINGTEAM##", "Appointment's real location", @@ -150,8 +159,8 @@ class MailTemplate(models.Model): def _add_generic_replacements(self, replacements, worker): current_datetime = datetime.datetime.now() replacements.update({ - "##DATE_FULL##": current_datetime.strftime(DATE_FORMAT_FULL), - "##DATE_SHORT##": current_datetime.strftime(DATE_FORMAT_SHORT), + "##DATE_FULL##": current_datetime.strftime(DATE_FORMAT_FULL).decode(date_format_encoding()), + "##DATE_SHORT##": current_datetime.strftime(DATE_FORMAT_SHORT).decode(date_format_encoding()), "##WORKER##": unicode(worker) }) @@ -164,9 +173,12 @@ class MailTemplate(models.Model): worker_phone_number = "" worker_email_address = "" if appointment.datetime_when is not None: - appointment_date_full = appointment.datetime_when.strftime(DATETIME_FORMAT) - appointment_date_short = appointment.datetime_when.strftime(DATE_FORMAT_SHORT) - appointment_date_time = appointment.datetime_when.strftime(DATE_FORMAT_TIME) + appointment_date_full = appointment.datetime_when.strftime(DATETIME_FORMAT).decode( + date_format_encoding()) + appointment_date_short = appointment.datetime_when.strftime(DATE_FORMAT_SHORT).decode( + date_format_encoding()) + appointment_date_time = appointment.datetime_when.strftime(DATE_FORMAT_TIME).decode( + date_format_encoding()) else: appointment_date_full = appointment_date_short = appointment_date_time = "" replacements.update({ @@ -188,16 +200,17 @@ class MailTemplate(models.Model): def _add_visit_replacements(self, replacements, visit): if visit is not None: replacements.update({ - "##V_DATE_START_FULL##": visit.datetime_begin.strftime(DATETIME_FORMAT), - "##V_DATE_START_SHORT##": visit.datetime_begin.strftime(DATE_FORMAT_SHORT), - "##V_DATE_ENDS_FULL##": visit.datetime_end.strftime(DATETIME_FORMAT), - "##V_DATE_ENDS_SHORT##": visit.datetime_end.strftime(DATE_FORMAT_SHORT), + "##V_DATE_START_FULL##": visit.datetime_begin.strftime(DATETIME_FORMAT).decode(date_format_encoding()), + "##V_DATE_START_SHORT##": visit.datetime_begin.strftime(DATE_FORMAT_SHORT).decode( + date_format_encoding()), + "##V_DATE_ENDS_FULL##": visit.datetime_end.strftime(DATETIME_FORMAT).decode(date_format_encoding()), + "##V_DATE_ENDS_SHORT##": visit.datetime_end.strftime(DATE_FORMAT_SHORT).decode(date_format_encoding()), }) def _add_subject_replacements(self, replacements, study_subject): if study_subject is not None: if study_subject.subject.date_born is not None: - date_born = study_subject.subject.date_born.strftime(DATE_FORMAT_SHORT) + date_born = study_subject.subject.date_born.strftime(DATE_FORMAT_SHORT).decode(date_format_encoding()) else: date_born = None replacements.update({ @@ -208,7 +221,7 @@ class MailTemplate(models.Model): "##S_CITY##": study_subject.subject.city, "##S_COUNTRY##": unicode(study_subject.subject.country), "##S_DIAGNOSIS_YEAR##": study_subject.year_of_diagnosis, - "##S_DATE_ADDED##": study_subject.date_added.strftime(DATE_FORMAT_SHORT), + "##S_DATE_ADDED##": study_subject.date_added.strftime(DATE_FORMAT_SHORT).decode(date_format_encoding()), "##S_DATE_BORN##": date_born, "##S_DIAGNOSIS##": study_subject.diagnosis, "##S_EMAIL##": study_subject.subject.email, diff --git a/smash/web/tests/test_process_file.py b/smash/web/tests/test_process_file.py index d7ef2bb4270ce3e6f845d01b0bc916f2b17be670..61b32c416309e0440feb08020853b6545727dbb6 100644 --- a/smash/web/tests/test_process_file.py +++ b/smash/web/tests/test_process_file.py @@ -1,14 +1,18 @@ import datetime import locale +import logging +import os import platform import tempfile -import os from django.test import TestCase -from functions import get_resource_path +from web.tests.functions import get_resource_path +from web.models.mail_template import date_format_encoding from web.docx_helper import process_file +logger = logging.getLogger(__name__) + class TestDocxProcessor(TestCase): def test_process_file(self): @@ -26,7 +30,7 @@ class TestDocxProcessor(TestCase): "##ADDRESS2##": "61-234, Poznan", "##COUNTRY##": "POLAND", "##CONTENT##": "1", - "##DATE##": datetime.datetime.now().date().strftime("%A %d %B %Y"), + "##DATE##": datetime.datetime.now().date().strftime("%A %d %B %Y").decode(date_format_encoding()), } process_file(template_path, output_path, changes) self.assertTrue(os.path.isfile(output_path)) diff --git a/smash/web/tests/test_statistics.py b/smash/web/tests/test_statistics.py index 92e19cf5a8dbfe588ae20a1a311197617e392c2c..af58590938e9870239cbfb1eb38c477e71b785b1 100644 --- a/smash/web/tests/test_statistics.py +++ b/smash/web/tests/test_statistics.py @@ -40,7 +40,7 @@ class TestStatistics(TestCase): statistics = self.statistics_manager.get_statistics_for_month(self.now.month, self.now.year) self.check_statistics(statistics, 0, 0, 1, {"C": [1, 1]}, ['Scheduled']) - statistics = self.statistics_manager.get_statistics_for_month(self.visit_end.month, self.now.year) + statistics = self.statistics_manager.get_statistics_for_month(self.visit_end.month, self.visit_end.year) self.check_statistics(statistics, 0, 1, 0, {"C": [0, 0]}, ['Scheduled']) def test_get_statistics_for_month_one_appointment_visit(self):