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

data inside table cells are modified by templates

parent 4d524b21
No related branches found
No related tags found
1 merge request!147Resolve "printing documents doesn't replace data from tables"
Pipeline #
......@@ -13,11 +13,17 @@ def process_file(path_to_docx, path_to_new_docx, changes_to_apply):
argument.
"""
doc = Document(path_to_docx)
for paragraph in doc.paragraphs:
for placeholder, replacement in changes_to_apply.items():
for placeholder, replacement in changes_to_apply.items():
for paragraph in doc.paragraphs:
if placeholder in paragraph.text:
paragraph.text = paragraph.text.replace(placeholder, replacement)
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
paragraph.text = paragraph.text.replace(placeholder, replacement)
doc.save(path_to_new_docx)
......
......@@ -239,7 +239,7 @@ class MailTemplate(models.Model):
'##A_WORKER_PHONE##': worker_phone_number,
'##A_WORKER_EMAIL##': worker_email_address,
"##A_ROOM##": str(appointment.room),
"##A_LENGTH##": appointment.length,
"##A_LENGTH##": str(appointment.length),
"##A_TYPES##": ", ".join([a.description for a in appointment.appointment_types.all()])
}
......@@ -261,7 +261,7 @@ class MailTemplate(models.Model):
if study_subject.subject.date_born is not None:
date_born = study_subject.subject.date_born.strftime(DATE_FORMAT_SHORT).decode(date_format_encoding())
else:
date_born = None
date_born = ""
return {
"##S_FULL_NAME##": unicode(study_subject),
"##S_FIRST_NAME##": study_subject.subject.first_name,
......@@ -269,17 +269,17 @@ class MailTemplate(models.Model):
"##S_ADDRESS##": study_subject.subject.address,
"##S_CITY##": study_subject.subject.city,
"##S_COUNTRY##": unicode(study_subject.subject.country),
"##S_DIAGNOSIS_YEAR##": study_subject.year_of_diagnosis,
"##S_DIAGNOSIS_YEAR##": str(study_subject.year_of_diagnosis),
"##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,
"##S_DIAGNOSIS##": unicode(study_subject.diagnosis),
"##S_EMAIL##": unicode(study_subject.subject.email),
"##S_SEX##": study_subject.subject.get_sex_display(),
"##S_MPOWER_ID##": study_subject.mpower_id,
"##S_ND_NUMBER##": study_subject.nd_number,
"##S_PHONE_NUMBER##": study_subject.subject.phone_number,
"##S_PHONE_NUMBER_2##": study_subject.subject.phone_number_2,
"##S_PHONE_NUMBER_3##": study_subject.subject.phone_number_3,
"##S_PHONE_NUMBER##": unicode(study_subject.subject.phone_number),
"##S_PHONE_NUMBER_2##": unicode(study_subject.subject.phone_number_2),
"##S_PHONE_NUMBER_3##": unicode(study_subject.subject.phone_number_3),
"##S_POST_CODE##": study_subject.subject.postal_code,
"##S_SCREENING_NUMBER##": study_subject.screening_number,
"##S_TYPE##": study_subject.get_type_display(),
......
File added
......@@ -115,6 +115,53 @@ class MailTemplateModelTests(TestCase):
voucher.issue_date.strftime(DATE_FORMAT_SHORT)
])
def test_apply_voucher_for_template_with_table(self):
template_name_french = "test_without_language"
subject = create_study_subject()
subject_template_french = MailTemplate(name=template_name_french, language=None,
context=MAIL_TEMPLATE_CONTEXT_VOUCHER,
template_file="template_with_tables.docx")
voucher = create_voucher(study_subject=subject)
stream = StringIO.StringIO()
subject_template_french.apply(voucher, self.user, stream)
doc = Document(stream)
worker_name = str(self.user.worker)
self.check_doc_contains(doc, [worker_name, str(subject), voucher.number, voucher.usage_partner.address,
voucher.expiry_date.strftime(DATE_FORMAT_SHORT),
voucher.issue_date.strftime(DATE_FORMAT_SHORT)
])
def test_valid_subject_replacement(self):
subject = create_study_subject()
replacements = MailTemplate.get_subject_replacements(subject)
for key in replacements:
self.assertIsNotNone(key)
self.assertIsNotNone(replacements[key], msg="None value for key: " + key)
def test_valid_appointment_replacement(self):
appointment = create_appointment()
replacements = MailTemplate.get_appointment_replacements(appointment)
for key in replacements:
self.assertIsNotNone(key)
self.assertIsNotNone(replacements[key], msg="None value for key: " + key)
def test_valid_generic_replacement(self):
replacements = MailTemplate.get_generic_replacements(self.user.worker)
for key in replacements:
self.assertIsNotNone(key)
self.assertIsNotNone(replacements[key], msg="None value for key: " + key)
def test_valid_generic_replacement_for_empty_worker(self):
replacements = MailTemplate.get_generic_replacements(None)
for key in replacements:
self.assertIsNotNone(key)
self.assertIsNotNone(replacements[key], msg="None value for key: " + key)
def test_get_mail_templates_for_context_without_language(self):
template_name_french = "test_without_language"
MailTemplate(name=template_name_french, language=None,
......@@ -156,17 +203,22 @@ class MailTemplateModelTests(TestCase):
def check_doc_contains(self, doc, needles):
founds = [False] * len(needles)
all_founds = False
count_found = 0
for paragraph in doc.paragraphs:
for i in range(0, len(needles)):
if not founds[i] and needles[i] in paragraph.text:
founds[i] = True
count_found += 1
if count_found == len(needles):
all_founds = True
break
if not all_founds:
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
for i in range(0, len(needles)):
if not founds[i] and needles[i] in paragraph.text:
founds[i] = True
count_found += 1
if count_found != len(needles):
for i in range(0, len(needles)):
self.assertTrue(founds[i], "{} was not found in the generated Word document".format(needles[i]))
......
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