From 61a73e91a691b10217cc8772c18148e174de7c3d Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Fri, 1 Dec 2017 18:31:28 +0100 Subject: [PATCH] datetime format is encoded (instead of pure ASCII characters transformed into string) --- smash/web/models/mail_template.py | 55 +++++++++++++++++----------- smash/web/tests/test_process_file.py | 10 +++-- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/smash/web/models/mail_template.py b/smash/web/models/mail_template.py index 5d6e081b..d4f72d18 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 d7ef2bb4..61b32c41 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)) -- GitLab