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

datetime format is encoded (instead of pure ASCII characters transformed into string)

parent bc8bdfe4
No related branches found
No related tags found
1 merge request!102datetime format is encoded (instead of pure ASCII characters transformed into string)
Pipeline #
...@@ -22,6 +22,10 @@ DATE_FORMAT_TIME = "%H:%M" ...@@ -22,6 +22,10 @@ DATE_FORMAT_TIME = "%H:%M"
now = datetime.datetime.now() now = datetime.datetime.now()
def date_format_encoding():
return locale.getlocale(locale.LC_TIME)[1] or locale.getpreferredencoding()
@contextmanager @contextmanager
def setlocale(name): def setlocale(name):
saved = locale.setlocale(locale.LC_TIME) saved = locale.setlocale(locale.LC_TIME)
...@@ -31,9 +35,14 @@ def setlocale(name): ...@@ -31,9 +35,14 @@ def setlocale(name):
locale.setlocale(locale.LC_TIME, saved) 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): class MailTemplate(models.Model):
MAILS_TEMPLATE_GENERIC_TAGS = [ 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)", ("##DATE_SHORT##", "Current date when the mail will be generated (short format)",
now.strftime(DATE_FORMAT_SHORT)), now.strftime(DATE_FORMAT_SHORT)),
("##WORKER##", "The full name of the currently logged in user", "") ("##WORKER##", "The full name of the currently logged in user", "")
...@@ -49,7 +58,7 @@ class MailTemplate(models.Model): ...@@ -49,7 +58,7 @@ class MailTemplate(models.Model):
("##S_COUNTRY##", "Subject's country of residence", ""), ("##S_COUNTRY##", "Subject's country of residence", ""),
("##S_SEX##", "Subject's gender", "Male/Female"), ("##S_SEX##", "Subject's gender", "Male/Female"),
("##S_TYPE##", "Subject's type", "CONTROL/PATIENT"), ("##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_EMAIL##", "Subject's email address", ""),
("##S_PHONE_NUMBER##", "Subject's phone number", ""), ("##S_PHONE_NUMBER##", "Subject's phone number", ""),
...@@ -63,20 +72,20 @@ class MailTemplate(models.Model): ...@@ -63,20 +72,20 @@ class MailTemplate(models.Model):
("##S_DIAGNOSIS_YEAR##", "Subject's year of diagnosis", ""), ("##S_DIAGNOSIS_YEAR##", "Subject's year of diagnosis", ""),
("##S_MPOWER_ID##", "Subject's mPower identifier", ""), ("##S_MPOWER_ID##", "Subject's mPower identifier", ""),
("##S_ND_NUMBER##", "Subject's ND number", ""), ("##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 = [ MAILS_TEMPLATE_VISIT_TAGS = [
("##V_DATE_START_FULL##", "Visit's start date", now.strftime(DATETIME_FORMAT)), ("##V_DATE_START_FULL##", "Visit's start date", get_formatted_time(DATETIME_FORMAT)),
("##V_DATE_START_SHORT##", "Visit's start date", now.strftime(DATE_FORMAT_SHORT)), ("##V_DATE_START_SHORT##", "Visit's start date", get_formatted_time(DATE_FORMAT_SHORT)),
("##V_DATE_ENDS_FULL##", "Visit's end date", now.strftime(DATETIME_FORMAT)), ("##V_DATE_ENDS_FULL##", "Visit's end date", get_formatted_time(DATETIME_FORMAT)),
("##V_DATE_ENDS_SHORT##", "Visit's end date", now.strftime(DATE_FORMAT_SHORT)), ("##V_DATE_ENDS_SHORT##", "Visit's end date", get_formatted_time(DATE_FORMAT_SHORT)),
] ]
MAILS_TEMPLATE_APPOINTMENT_TAGS = [ MAILS_TEMPLATE_APPOINTMENT_TAGS = [
("##A_DATE_FULL##", "Appointment's date and time", now.strftime(DATETIME_FORMAT)), ("##A_DATE_FULL##", "Appointment's date and time", get_formatted_time(DATETIME_FORMAT)),
("##A_DATE_SHORT##", "Appointment's date", now.strftime(DATE_FORMAT_SHORT)), ("##A_DATE_SHORT##", "Appointment's date", get_formatted_time(DATE_FORMAT_SHORT)),
("##A_TIME##", "Appointment's time", now.strftime(DATE_FORMAT_TIME)), ("##A_TIME##", "Appointment's time", get_formatted_time(DATE_FORMAT_TIME)),
("##A_FLYING_TEAM##", "Appointment's flying team location", ""), ("##A_FLYING_TEAM##", "Appointment's flying team location", ""),
("##A_LOCATION##", "Appointment's location", "value can be 'Flying Team'"), ("##A_LOCATION##", "Appointment's location", "value can be 'Flying Team'"),
("##A_LOCATION_OR_FLYINGTEAM##", "Appointment's real location", ("##A_LOCATION_OR_FLYINGTEAM##", "Appointment's real location",
...@@ -150,8 +159,8 @@ class MailTemplate(models.Model): ...@@ -150,8 +159,8 @@ class MailTemplate(models.Model):
def _add_generic_replacements(self, replacements, worker): def _add_generic_replacements(self, replacements, worker):
current_datetime = datetime.datetime.now() current_datetime = datetime.datetime.now()
replacements.update({ replacements.update({
"##DATE_FULL##": current_datetime.strftime(DATE_FORMAT_FULL), "##DATE_FULL##": current_datetime.strftime(DATE_FORMAT_FULL).decode(date_format_encoding()),
"##DATE_SHORT##": current_datetime.strftime(DATE_FORMAT_SHORT), "##DATE_SHORT##": current_datetime.strftime(DATE_FORMAT_SHORT).decode(date_format_encoding()),
"##WORKER##": unicode(worker) "##WORKER##": unicode(worker)
}) })
...@@ -164,9 +173,12 @@ class MailTemplate(models.Model): ...@@ -164,9 +173,12 @@ class MailTemplate(models.Model):
worker_phone_number = "" worker_phone_number = ""
worker_email_address = "" worker_email_address = ""
if appointment.datetime_when is not None: if appointment.datetime_when is not None:
appointment_date_full = appointment.datetime_when.strftime(DATETIME_FORMAT) appointment_date_full = appointment.datetime_when.strftime(DATETIME_FORMAT).decode(
appointment_date_short = appointment.datetime_when.strftime(DATE_FORMAT_SHORT) date_format_encoding())
appointment_date_time = appointment.datetime_when.strftime(DATE_FORMAT_TIME) 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: else:
appointment_date_full = appointment_date_short = appointment_date_time = "" appointment_date_full = appointment_date_short = appointment_date_time = ""
replacements.update({ replacements.update({
...@@ -188,16 +200,17 @@ class MailTemplate(models.Model): ...@@ -188,16 +200,17 @@ class MailTemplate(models.Model):
def _add_visit_replacements(self, replacements, visit): def _add_visit_replacements(self, replacements, visit):
if visit is not None: if visit is not None:
replacements.update({ replacements.update({
"##V_DATE_START_FULL##": visit.datetime_begin.strftime(DATETIME_FORMAT), "##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), "##V_DATE_START_SHORT##": visit.datetime_begin.strftime(DATE_FORMAT_SHORT).decode(
"##V_DATE_ENDS_FULL##": visit.datetime_end.strftime(DATETIME_FORMAT), date_format_encoding()),
"##V_DATE_ENDS_SHORT##": visit.datetime_end.strftime(DATE_FORMAT_SHORT), "##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): def _add_subject_replacements(self, replacements, study_subject):
if study_subject is not None: if study_subject is not None:
if study_subject.subject.date_born 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: else:
date_born = None date_born = None
replacements.update({ replacements.update({
...@@ -208,7 +221,7 @@ class MailTemplate(models.Model): ...@@ -208,7 +221,7 @@ class MailTemplate(models.Model):
"##S_CITY##": study_subject.subject.city, "##S_CITY##": study_subject.subject.city,
"##S_COUNTRY##": unicode(study_subject.subject.country), "##S_COUNTRY##": unicode(study_subject.subject.country),
"##S_DIAGNOSIS_YEAR##": study_subject.year_of_diagnosis, "##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_DATE_BORN##": date_born,
"##S_DIAGNOSIS##": study_subject.diagnosis, "##S_DIAGNOSIS##": study_subject.diagnosis,
"##S_EMAIL##": study_subject.subject.email, "##S_EMAIL##": study_subject.subject.email,
......
import datetime import datetime
import locale import locale
import logging
import os
import platform import platform
import tempfile import tempfile
import os
from django.test import TestCase 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 from web.docx_helper import process_file
logger = logging.getLogger(__name__)
class TestDocxProcessor(TestCase): class TestDocxProcessor(TestCase):
def test_process_file(self): def test_process_file(self):
...@@ -26,7 +30,7 @@ class TestDocxProcessor(TestCase): ...@@ -26,7 +30,7 @@ class TestDocxProcessor(TestCase):
"##ADDRESS2##": "61-234, Poznan", "##ADDRESS2##": "61-234, Poznan",
"##COUNTRY##": "POLAND", "##COUNTRY##": "POLAND",
"##CONTENT##": "1", "##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) process_file(template_path, output_path, changes)
self.assertTrue(os.path.isfile(output_path)) self.assertTrue(os.path.isfile(output_path))
......
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