diff --git a/smash/web/models/mail_template.py b/smash/web/models/mail_template.py index 15adb9bf16490aeb6d738689b93656365a81feff..dcd25850664665b63ed05f920b04fbb00df9568b 100644 --- a/smash/web/models/mail_template.py +++ b/smash/web/models/mail_template.py @@ -39,6 +39,13 @@ def get_formatted_time(time_format): return now.strftime(time_format).decode(date_format_encoding()) +def date_to_str(date, format): + if date is not None: + return date.strftime(format).decode(date_format_encoding()) + else: + return "" + + class MailTemplate(models.Model): MAILS_TEMPLATE_GENERIC_TAGS = [ ("##DATE_FULL##", "Current date when the mail will be generated (long format)", @@ -74,6 +81,36 @@ class MailTemplate(models.Model): ("##S_MPOWER_ID##", "Subject's mPower identifier", ""), ("##S_ND_NUMBER##", "Subject's ND number", ""), ("##S_DATE_ADDED##", "Subject's date of creation", get_formatted_time(DATE_FORMAT_SHORT)), + + ("##VIRUS_1_SAMPLE_COLLECTION_DATE##", "Virus test collection date in visit 1", + get_formatted_time(DATE_FORMAT_SHORT)), + ("##VIRUS_2_SAMPLE_COLLECTION_DATE##", "Virus test collection date in visit 2", + get_formatted_time(DATE_FORMAT_SHORT)), + ("##VIRUS_3_SAMPLE_COLLECTION_DATE##", "Virus test collection date in visit 3", + get_formatted_time(DATE_FORMAT_SHORT)), + ("##VIRUS_4_SAMPLE_COLLECTION_DATE##", "Virus test collection date in visit 4", + get_formatted_time(DATE_FORMAT_SHORT)), + ("##VIRUS_5_SAMPLE_COLLECTION_DATE##", "Virus test collection date in visit 5", + get_formatted_time(DATE_FORMAT_SHORT)), + + ("##VIRUS_1_RESULT##", "Virus test results in visit 1", "Positive"), + ("##VIRUS_2_RESULT##", "Virus test results in visit 2", "Negative"), + ("##VIRUS_3_RESULT##", "Virus test results in visit 3", "Inconclusive"), + ("##VIRUS_4_RESULT##", "Virus test results in visit 4", ""), + ("##VIRUS_5_RESULT##", "Virus test results in visit 5", ""), + + ("##VIRUS_1_IGA_STATUS##", "IgA Status in visit 1", "Positive"), + ("##VIRUS_2_IGA_STATUS##", "IgA Status in visit 2", "Negative"), + ("##VIRUS_3_IGA_STATUS##", "IgA Status in visit 3", "Borderline"), + ("##VIRUS_4_IGA_STATUS##", "IgA Status in visit 4", ""), + ("##VIRUS_5_IGA_STATUS##", "IgA Status in visit 5", ""), + + ("##VIRUS_1_IGG_STATUS##", "IgG Status in visit 1", "Positive"), + ("##VIRUS_2_IGG_STATUS##", "IgG Status in visit 2", "Negative"), + ("##VIRUS_3_IGG_STATUS##", "IgG Status in visit 3", "Borderline"), + ("##VIRUS_4_IGG_STATUS##", "IgG Status in visit 4", ""), + ("##VIRUS_5_IGG_STATUS##", "IgG Status in visit 5", ""), + ] MAILS_TEMPLATE_VISIT_TAGS = [ @@ -257,12 +294,10 @@ class MailTemplate(models.Model): @staticmethod def get_subject_replacements(study_subject): + result = {} 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).decode(date_format_encoding()) - else: - date_born = "" - return { + date_born = date_to_str(study_subject.subject.date_born, DATE_FORMAT_SHORT) + result = { "##S_FULL_NAME##": unicode(study_subject), "##S_FIRST_NAME##": study_subject.subject.first_name, "##S_LAST_NAME##": study_subject.subject.last_name, @@ -270,7 +305,7 @@ class MailTemplate(models.Model): "##S_CITY##": study_subject.subject.city, "##S_COUNTRY##": unicode(study_subject.subject.country), "##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_ADDED##": date_to_str(study_subject.date_added, DATE_FORMAT_SHORT), "##S_DATE_BORN##": date_born, "##S_DIAGNOSIS##": unicode(study_subject.diagnosis), "##S_EMAIL##": unicode(study_subject.subject.email), @@ -286,7 +321,39 @@ class MailTemplate(models.Model): '##S_MAIL_LANGUAGE##': str(study_subject.subject.default_written_communication_language), '##S_KNOWN_LANGUAGES##': ", ".join([l.name for l in study_subject.subject.languages.all()]) } - return {} + for i in range(1, 6): + virus_test_field = "##VIRUS_{}_RESULT##".format(i) + virus_test_value = virus_test_to_str(getattr(study_subject, "virus_test_{}".format(i)), + getattr(study_subject, "virus_test_{}_updated".format(i))) + + virus_test_date_field = "##VIRUS_{}_SAMPLE_COLLECTION_DATE##".format(i) + virus_test_date_value = date_to_str(getattr(study_subject, "virus_test_{}_collection_date".format(i)), + DATE_FORMAT_SHORT) + + virus_iga_status_field = "##VIRUS_{}_IGA_STATUS##".format(i) + virus_iga_status_value = getattr(study_subject, "virus_test_{}_iga_status".format(i)) + if virus_iga_status_value is None: + virus_iga_status_value = "" + + virus_igg_status_field = "##VIRUS_{}_IGG_STATUS##".format(i) + virus_igg_status_value = getattr(study_subject, "virus_test_{}_igg_status".format(i)) + if virus_igg_status_value is None: + virus_igg_status_value = "" + + if virus_test_date_value != "": + if virus_test_value == "": + virus_test_value = "N/A" + if virus_iga_status_value == "": + virus_iga_status_value = "N/A" + if virus_igg_status_value == "": + virus_igg_status_value = "N/A" + + result[virus_test_field] = virus_test_value + result[virus_test_date_field] = virus_test_date_value + result[virus_iga_status_field] = virus_iga_status_value + result[virus_igg_status_field] = virus_igg_status_value + + return result @staticmethod def get_voucher_replacements(voucher): @@ -310,3 +377,14 @@ class MailTemplate(models.Model): "##C_HOURS##": str(voucher.hours), } return {} + + +def virus_test_to_str(test, date): + if test is None and date is not None: + return "Inconclusive" + if test is None: + return "" + if test: + return "Positive" + else: + return "Negative"