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

Merge remote-tracking branch 'origin/devel_1.0.x' into...

Merge remote-tracking branch 'origin/devel_1.0.x' into 383-saving-subject-with-custom-date_field-makes-subject-form-unusable
parents defbeb6e 07dc55c9
No related branches found
No related tags found
3 merge requests!316Merge 1.0.1,!3151.0.1 into master,!310Resolve "saving subject with custom date_field makes subject form unusable"
Pipeline #38445 passed
smasch (1.0.1-1) stable; urgency=low smasch (1.0.1-1) stable; urgency=low
* bug fix: readonly custom Date Field was inproperly persisted in forms * bug fix: unfinish visit button was always disabled (#386)
* bug fix: generating documents for templates did not work for subjects
created before custom field was added to the study (#388)
* bug fix: readonly custom Date Field was improperly persisted in forms
(#383) (#383)
-- Piotr Gawron <piotr.gawron@uni.lu> Mon, 15 Mar 2021 14:00:00 +0200 -- Piotr Gawron <piotr.gawron@uni.lu> Mon, 15 Mar 2021 14:00:00 +0200
......
...@@ -26,23 +26,27 @@ def process_file(path_to_docx, path_to_new_docx, changes_to_apply): ...@@ -26,23 +26,27 @@ def process_file(path_to_docx, path_to_new_docx, changes_to_apply):
doc.save(path_to_new_docx) doc.save(path_to_new_docx)
def merge_files(files, path_to_new_docx): def merge_files(files, path_to_new_docx):
''' """
When combining templates into a new Document object, python-docx does not properly copy the information of the images, then Word is not able to locate the original content referred by the XML tag in the document. To fix this problem (see #234 ) the first file is loaded and the rest of templates are concatenated to this. This way, the original image content and rId match and the images are shown adequately. When combining templates into a new Document object, python-docx does not properly copy the information of the
images, then Word is not able to locate the original content referred by the XML tag in the document. To fix this
problem (see #234 ) the first file is loaded and the rest of templates are concatenated to this. This way, the
original image content and rId match and the images are shown adequately.
See issue #235 See issue #235
''' """
first_file = files[0] first_file = files[0]
files = files[1:] files = files[1:]
merged_document = Document(first_file) #first file merged_document = Document(first_file) # first file
if len(files) > 0: if len(files) > 0:
merged_document.add_page_break() merged_document.add_page_break()
for index, file in enumerate(files): #rest of files if any for index, file in enumerate(files): # rest of files if any
sub_doc = Document(file) sub_doc = Document(file)
if index < len(files) - 1: if index < len(files) - 1:
sub_doc.add_page_break() sub_doc.add_page_break()
for element in sub_doc.element.body: for element in sub_doc.element.body:
merged_document.element.body.append(element) merged_document.element.body.append(element)
merged_document.save(path_to_new_docx) merged_document.save(path_to_new_docx)
\ No newline at end of file
...@@ -338,13 +338,13 @@ class MailTemplate(models.Model): ...@@ -338,13 +338,13 @@ class MailTemplate(models.Model):
"##S_ADDRESS##": study_subject.subject.address, "##S_ADDRESS##": study_subject.subject.address,
"##S_CITY##": study_subject.subject.city, "##S_CITY##": study_subject.subject.city,
"##S_COUNTRY##": str(study_subject.subject.country), "##S_COUNTRY##": str(study_subject.subject.country),
"##S_DIAGNOSIS_YEAR##": study_subject.get_custom_field_value('Year of diagnosis'), "##S_DIAGNOSIS_YEAR##": str(study_subject.get_custom_field_value('Year of diagnosis')),
"##S_DATE_ADDED##": date_to_str(study_subject.date_added, DATE_FORMAT_SHORT), "##S_DATE_ADDED##": date_to_str(study_subject.date_added, DATE_FORMAT_SHORT),
"##S_DATE_BORN##": date_born, "##S_DATE_BORN##": date_born,
"##S_DIAGNOSIS##": study_subject.get_custom_field_value('Diagnosis'), "##S_DIAGNOSIS##": str(study_subject.get_custom_field_value('Diagnosis')),
"##S_EMAIL##": str(study_subject.subject.email), "##S_EMAIL##": str(study_subject.subject.email),
"##S_SEX##": study_subject.subject.get_sex_display(), "##S_SEX##": study_subject.subject.get_sex_display(),
"##S_MPOWER_ID##": study_subject.get_custom_field_value('MPower ID'), "##S_MPOWER_ID##": str(study_subject.get_custom_field_value('MPower ID')),
"##S_ND_NUMBER##": study_subject.nd_number, "##S_ND_NUMBER##": study_subject.nd_number,
"##S_PHONE_NUMBER##": str(study_subject.subject.phone_number), "##S_PHONE_NUMBER##": str(study_subject.subject.phone_number),
"##S_PHONE_NUMBER_2##": str(study_subject.subject.phone_number_2), "##S_PHONE_NUMBER_2##": str(study_subject.subject.phone_number_2),
......
...@@ -70,7 +70,7 @@ ...@@ -70,7 +70,7 @@
<div class="col-md-6 form-group"> <div class="col-md-6 form-group">
<label class="col-sm-4 control-label"> <label class="col-sm-4 control-label">
{% if visFinished %} {% if visFinished %}
<i title="Only visits with one inmediate future visit (without appointments) can be unfinished." class="fa fa-info-circle"></i> <i title="Only visits with one immediate future visit (without appointments) can be unfinished." class="fa fa-info-circle"></i>
{% endif %} {% endif %}
Visit finished Visit finished
</label> </label>
...@@ -95,7 +95,7 @@ ...@@ -95,7 +95,7 @@
{% if visFinished %} {% if visFinished %}
<div class="col-sm-1"> <div class="col-sm-1">
{% if "unfinish_visit" not in permissions %} {% if "delete_visit" not in permissions %}
<div title="You don't have permissions to unfinish visits." class="btn btn-block btn-warning disabled"> <div title="You don't have permissions to unfinish visits." class="btn btn-block btn-warning disabled">
<i class="fa fa-undo"></i> <i class="fa fa-undo"></i>
</div> </div>
......
...@@ -4,13 +4,14 @@ import io ...@@ -4,13 +4,14 @@ import io
from django.test import TestCase from django.test import TestCase
from docx import Document from docx import Document
from web.models import MailTemplate from web.models import MailTemplate, StudySubject, Language
from web.models.constants import MAIL_TEMPLATE_CONTEXT_APPOINTMENT, MAIL_TEMPLATE_CONTEXT_VISIT, \ from web.models.constants import MAIL_TEMPLATE_CONTEXT_APPOINTMENT, MAIL_TEMPLATE_CONTEXT_VISIT, \
MAIL_TEMPLATE_CONTEXT_SUBJECT, MAIL_TEMPLATE_CONTEXT_VOUCHER MAIL_TEMPLATE_CONTEXT_SUBJECT, MAIL_TEMPLATE_CONTEXT_VOUCHER, CUSTOM_FIELD_TYPE_TEXT
from web.models.custom_data import CustomStudySubjectField
from web.models.mail_template import DATE_FORMAT_SHORT from web.models.mail_template import DATE_FORMAT_SHORT
from web.tests.functions import create_language, get_resource_path, create_appointment, create_user, \ from web.tests.functions import create_language, get_resource_path, create_appointment, create_user, \
create_study_subject, \ create_study_subject, \
create_visit, create_voucher, create_worker, create_flying_team create_visit, create_voucher, create_worker, create_flying_team, get_test_study
class MailTemplateModelTests(TestCase): class MailTemplateModelTests(TestCase):
...@@ -100,19 +101,32 @@ class MailTemplateModelTests(TestCase): ...@@ -100,19 +101,32 @@ class MailTemplateModelTests(TestCase):
self.check_doc_contains(doc, [flying_team_name]) self.check_doc_contains(doc, [flying_team_name])
def test_apply_subject(self): def test_apply_subject(self):
template_name_french = "test_fr"
subject = create_study_subject() subject = create_study_subject()
subject_template_french = MailTemplate(name=template_name_french, language=self.french_language, doc = self.create_doc_for_subject(subject, self.french_language, MAIL_TEMPLATE_CONTEXT_SUBJECT)
context=MAIL_TEMPLATE_CONTEXT_SUBJECT, worker_name = str(self.user.worker)
template_file=self.template_file)
stream = io.BytesIO() self.check_doc_contains(doc, [worker_name, str(subject), str(subject.subject.country), subject.nd_number,
subject_template_french.apply(subject, self.user, stream) subject.get_type_display()])
doc = Document(stream)
def test_apply_subject_with_non_existing_custom_field(self):
subject = create_study_subject()
field = CustomStudySubjectField.objects.create(name="Diagnosis", type=CUSTOM_FIELD_TYPE_TEXT,
study=get_test_study(), unique=True)
doc = self.create_doc_for_subject(subject, self.french_language, MAIL_TEMPLATE_CONTEXT_SUBJECT)
worker_name = str(self.user.worker) worker_name = str(self.user.worker)
self.check_doc_contains(doc, [worker_name, str(subject), str(subject.subject.country), subject.nd_number, self.check_doc_contains(doc, [worker_name, str(subject), str(subject.subject.country), subject.nd_number,
subject.get_type_display()]) subject.get_type_display()])
def create_doc_for_subject(self, subject: StudySubject, language: Language, context: str):
subject_template_french = MailTemplate(name="test_fr", language=language,
context=context,
template_file=self.template_file)
stream = io.BytesIO()
subject_template_french.apply(subject, self.user, stream)
doc = Document(stream)
return doc
def test_apply_voucher(self): def test_apply_voucher(self):
template_name_french = "test_without_language" template_name_french = "test_without_language"
subject = create_study_subject() subject = create_study_subject()
......
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