diff --git a/edivorce/apps/core/templates/partials/review_user_responses.html b/edivorce/apps/core/templates/partials/review_user_responses.html index 14a5b16f..c3f6830b 100644 --- a/edivorce/apps/core/templates/partials/review_user_responses.html +++ b/edivorce/apps/core/templates/partials/review_user_responses.html @@ -1,6 +1,5 @@ {% load summary_format %} {% if questions %} - {% if step == 'your_children' %} @@ -11,7 +10,6 @@ {% format_children source=questions %}
- {% else %} diff --git a/edivorce/apps/core/templatetags/composites.py b/edivorce/apps/core/templatetags/composites.py index 432755df..f28664a7 100644 --- a/edivorce/apps/core/templatetags/composites.py +++ b/edivorce/apps/core/templatetags/composites.py @@ -5,6 +5,7 @@ users full responses. from django import template from .format_utils import date_formatter +from django.utils.html import format_html register = template.Library() @@ -17,7 +18,7 @@ def effective_date(context): if context['responses'].get('divorce_take_effect_on', '') == 'specific date': date = context['responses'].get('divorce_take_effect_on_specific_date', '') if date == '': - effective = '' + effective = format_html('') else: effective = date_formatter(date) return effective diff --git a/edivorce/apps/core/templatetags/format_utils.py b/edivorce/apps/core/templatetags/format_utils.py index 4cfdd181..9af81143 100644 --- a/edivorce/apps/core/templatetags/format_utils.py +++ b/edivorce/apps/core/templatetags/format_utils.py @@ -6,6 +6,7 @@ import locale import re from django import template +from django.utils.html import format_html from django.utils.safestring import mark_safe from django.utils.timesince import timesince @@ -48,7 +49,7 @@ def response(field, size=None, trail='', as_date=False): if field.strip(): return '%s%s' % (date_formatter(field) if as_date else field, trail) style = ('min-width: %spx' % size) if size is not None else '' - return '' % style + return format_html('', style) @register.simple_tag() diff --git a/edivorce/apps/core/templatetags/input_field.py b/edivorce/apps/core/templatetags/input_field.py index 46f9e005..227ef709 100644 --- a/edivorce/apps/core/templatetags/input_field.py +++ b/edivorce/apps/core/templatetags/input_field.py @@ -2,6 +2,7 @@ from datetime import datetime import json from django import template +from django.utils.html import format_html from ..models import UserResponse @@ -36,14 +37,25 @@ def money_input_field(context, input_type='number', name='', value_src=None, val if kwargs.get('step', None): step = kwargs.get('step') - tag = ['') - return ''.join(tag) + attributes = additional_attributes(**kwargs) + tag = format_html( + '{}{} />', + tag, + attributes) + + return tag @register.simple_tag(takes_context=True) @@ -52,18 +64,14 @@ def input_field(context, type, name='', value='', multiple='', **kwargs): Usage: when specifying data attributes in templates, use "data_" instead of "data-". """ if type == "textarea": - tag = ['') + value = context.get(name, '') + tag = format_html( + '', + name, + attributes, + value) else: # set initial value for textbox if type == "text": @@ -83,33 +91,40 @@ def input_field(context, type, name='', value='', multiple='', **kwargs): pass # conversion to current format not needed elif type == "number": value = context.get(name, '') - tag = ['') + tag = format_html( + '', + type, + name, + value, + attributes, + checked) - return ''.join(tag) + return tag -def additional_attributes(tag, **kwargs): +def additional_attributes(**kwargs): + attributes = '' for key, data_val in kwargs.items(): if str.startswith(key, 'data_'): key = str.replace(key, 'data_', 'data-') - tag.append(' ' + key + '="' + data_val + '"') - return tag + attributes = format_html('{} {}="{}"', attributes, key, data_val) + return attributes -@register.assignment_tag +@register.simple_tag def check_list(source, value): """ Check if given value is in the given source @@ -120,7 +135,7 @@ def check_list(source, value): return False -@register.assignment_tag +@register.simple_tag def multiple_values_to_list(source): try: return json.loads(source) diff --git a/edivorce/apps/core/templatetags/summary_format.py b/edivorce/apps/core/templatetags/summary_format.py index 59b43e1a..c1131815 100644 --- a/edivorce/apps/core/templatetags/summary_format.py +++ b/edivorce/apps/core/templatetags/summary_format.py @@ -4,6 +4,7 @@ from django import template import json from django.core.urlresolvers import reverse +from django.utils.html import format_html, format_html_join register = template.Library() @@ -28,67 +29,81 @@ def reformat_value(source, question_key): def process_list(lst, question_key): - tag = ["') - return ''.join(tag) + list_items = format_html_join( + '\n', + '
  • {0}
  • ', + ((value, '') for value in lst if value and not value.isspace())) + tag = format_html( + '', + list_items) + + return tag def reformat_list(source): text_list = source.split('\n') if len(text_list) > 1: - tag = ["') - return ''.join(tag) + list_items = format_html_join( + '\n', + '
  • {0}
  • ', + ((value, '') for value in text_list if value)) + tag = format_html( + '', + list_items) + return tag else: return text_list.pop() def format_row(question, response): - return ''.format( - question, response - ) + return format_html( + '', + question, + response) def format_review_row_heading(title, style=""): - return ''.format(title, style) + return format_html( + '', + title, + style) -def format_head(headings): - if len(headings) == 0: - return '', [] +# def format_head(headings): +# if len(headings) == 0: +# return '', [] - tags = [""] - head_order = list() - for key in headings[0].keys(): - tags.append(''.format(key.replace('_', ' ').title())) - head_order.append(key) - tags.append('') - return ''.join(tags), head_order +# tags = [""] +# head_order = list() +# for key in headings[0].keys(): +# tags.append(''.format(key.replace('_', ' ').title())) +# head_order.append(key) +# tags.append('') +# return ''.join(tags), head_order -def process_fact_sheet_list(data, header): - tags = list() - for item in data: - tags.append('') - for key in header: - tags.append(''.format(item.get(key, ''))) - tags.append('') - return ''.join(tags) +# def process_fact_sheet_list(data, header): +# tags = list() +# for item in data: +# tags.append('') +# for key in header: +# tags.append(''.format(item.get(key, ''))) +# tags.append('') +# return ''.join(tags) def format_fact_sheet(title, url, style=''): - return ''.format(style, url, title) + return format_html( + '', + style, + url, + title) @register.simple_tag(takes_context=True) @@ -152,12 +167,11 @@ def format_children(context, source): child_support_orders = {'have_court_order', 'what_parenting_arrangements', 'order_respecting_arrangement', 'order_for_child_support'} - tags = [] + tags = '' # process mapped questions first working_source = source.copy() - tags.append('') for title, questions in question_to_heading.items(): - tags.append(format_review_row_heading(title)) + tags = format_html('{}{}', tags, format_review_row_heading(title)) for question in questions: if question in fact_sheet_mapping: @@ -177,7 +191,7 @@ def format_children(context, source): show_fact_sheet = True if show_fact_sheet and len(fact_sheet_mapping[question]): - tags.append(format_fact_sheet(question, fact_sheet_mapping[question])) + tags = format_html('{}{}', tags, format_fact_sheet(question, fact_sheet_mapping[question])) else: item = list(filter(lambda x: x['question_id'] == question, working_source)) @@ -187,7 +201,7 @@ def format_children(context, source): item = item.pop() if context['derived']['wants_child_support'] is True: # make sure free form text is reformted to be bullet list. - tags.append(format_row(item['question__name'], reformat_list(item['value']))) + tags = format_html('{}{}{}', tags, format_row(item['question__name'], reformat_list(item['value']))) continue if len(item): @@ -197,12 +211,14 @@ def format_children(context, source): if q_id == 'claimant_children': child_counter = 1 for child in json.loads(item['value']): - tags.append(format_review_row_heading('Child {}'.format(child_counter), 'review-child-heading')) - tags.append(format_row('Child\'s name', child['child_name'])) - tags.append(format_row('Birth date', child['child_birth_date'])) - tags.append(format_row('Child now living with', child['child_live_with'])) - tags.append(format_row('Relationship to yourself (claimant 1)', child['child_relationship_to_you'])) - tags.append(format_row('Relationship to your spouse (claimant 2)', child['child_relationship_to_spouse'])) + tags = format_html( + '{}{}{}{}{}{}', + format_review_row_heading('Child {}'.format(child_counter), 'review-child-heading'), + format_row('Child\'s name', child['child_name']), + format_row('Birth date', child['child_birth_date']), + format_row('Child now living with', child['child_live_with']), + format_row('Relationship to yourself (claimant 1)', child['child_relationship_to_you']), + format_row('Relationship to your spouse (claimant 2)', child['child_relationship_to_spouse'])) child_counter = child_counter + 1 else: value = item['value'] @@ -220,16 +236,15 @@ def format_children(context, source): value = reformat_list(value) if isinstance(value, list): - tags.append(format_row(question_name, process_list(value, q_id))) + tags = format_html('{}{}', tags, format_row(question_name, process_list(value, q_id))) elif isinstance(value, str): if len(value): - tags.append(format_row(question_name, value)) + tags = format_html('{}{}', tags, format_row(question_name, value)) else: - tags.append(format_row(question_name, value)) - tags.append('') - tags.append('') - tags.append('') - return ''.join(tags) + tags = format_html('{}{}', tags, format_row(question_name, value)) + tags = format_html('{}', tags) + tags = format_html('{}', tags) + return tags @register.simple_tag @@ -238,10 +253,7 @@ def combine_address(source): Reformat address to combine them into one cell with multiple line Also show/hide optional questions """ - tags = [] - first_column = '' + tags = '' address_you = "" fax_you = "" @@ -258,7 +270,7 @@ def combine_address(source): if "email" not in q_id and "fax" not in q_id: if q_id == "address_to_send_official_document_country_you": continue - address_you += item["value"] + '
    ' + address_you = format_html('{}{}
    ', address_you, item["value"]) elif "fax" in q_id: fax_you = item["value"] elif "email" in q_id: @@ -267,7 +279,7 @@ def combine_address(source): if "email" not in q_id and "fax" not in q_id: if q_id == "address_to_send_official_document_country_spouse": continue - address_spouse += item["value"] + '
    ' + address_spouse = format_html('{}{}
    ', address_spouse, item["value"]) elif "fax" in q_id: fax_spouse = item["value"] elif "email" in q_id: @@ -281,27 +293,21 @@ def combine_address(source): effective_date = item['value'] if address_you != "": - tags.append(first_column + "What is the best address to send you official court documents?" - + second_column + address_you + end_tag) + tags = format_table_data(tags, "What is the best address to send you official court documents?", address_you) if fax_you != "": - tags.append(first_column + "Fax" + second_column + fax_you + end_tag) - + tags = format_table_data(tags, "Fax", fax_you) if email_you != "": - tags.append(first_column + "Email" + second_column + email_you + end_tag) - + tags = format_table_data(tags, "Email", email_you) if address_spouse != "": - tags.append(first_column + "What is the best address to send your spouse official court documents?" - + second_column + address_spouse + end_tag) + tags = format_table_data(tags, "What is the best address to send your spouse official court documents?", address_spouse) if fax_spouse != "": - tags.append(first_column + "Fax" + second_column + fax_spouse + end_tag) - + tags = format_table_data(tags, "Fax", fax_spouse) if email_spouse != "": - tags.append(first_column + "Email" + second_column + email_spouse + end_tag) - + tags = format_table_data(tags, "Email", email_spouse) if effective_date != "": - tags.append(first_column + "Divorce is to take effect on " + second_column + effective_date + end_tag) + tags = format_table_data(tags, "Divorce is to take effect on", effective_date) - return ''.join(tags) + return tags @register.simple_tag(takes_context=True) @@ -311,10 +317,7 @@ def marriage_tag(context, source): Also show/hide optional questions """ show_all = False - tags = [] - first_column = '' + tags = '' marriage_location = "" married_date = "" @@ -348,7 +351,7 @@ def marriage_tag(context, source): elif q_id.startswith('where_were_you_married'): if value == 'Other': continue - marriage_location += value + '
    ' + marriage_location = format_html('{}{}
    ', marriage_location, value) elif q_id == 'marital_status_before_you': marital_status_you_q = q_name marital_status_you = value @@ -357,17 +360,17 @@ def marriage_tag(context, source): marital_status_spouse = value if show_all and married_date != "": - tags.append(first_column + married_date_q + second_column + married_date + end_tag) + tags = format_table_data(tags, married_date_q, married_date) if common_law_date != "": - tags.append(first_column + common_law_date_q + second_column + common_law_date + end_tag) + tags = format_table_data(tags, common_law_date_q, common_law_date) if show_all and marriage_location != "": - tags.append(first_column + "Where were you married" + second_column + marriage_location + end_tag) + tags = format_table_data(tags, "Where were you married", marriage_location) if marital_status_you != "": - tags.append(first_column + marital_status_you_q + second_column + marital_status_you + end_tag) + tags = format_table_data(tags, marital_status_you_q, marital_status_you) if marital_status_spouse != "": - tags.append(first_column + marital_status_spouse_q + second_column + marital_status_spouse + end_tag) + tags = format_table_data(tags, marital_status_spouse_q, marital_status_spouse) - return ''.join(tags) + return tags @register.simple_tag @@ -376,10 +379,7 @@ def property_tag(source): Reformat your_property and debt step Also show/hide optional questions """ - tags = [] - first_column = '' + tags = '' division = division_detail = other_detail = None @@ -394,13 +394,13 @@ def property_tag(source): other_detail = item if division: - tags.append(first_column + division['question__name'] + second_column + division['value'] + end_tag) + tags = format_table_data(tags, division['question__name'], division['value']) if division and division['value'] == "Unequal division" and division_detail: - tags.append(first_column + division_detail['question__name'] + second_column + process_list(division_detail['value'].split('\n'), division_detail['question_id']) + end_tag) + tags = format_table_data(tags, division_detail['question__name'], process_list(division_detail['value'].split('\n'), division_detail['question_id'])) if other_detail and other_detail['value'].strip(): - tags.append(first_column + other_detail['question__name'] + second_column + process_list(other_detail['value'].split('\n'), other_detail['question_id']) + end_tag) + tags = format_table_data(tags, other_detail['question__name'], process_list(other_detail['value'].split('\n'), other_detail['question_id'])) - return ''.join(tags) + return tags @register.simple_tag @@ -409,10 +409,7 @@ def prequal_tag(source): Reformat prequalification step Also show/hide optional questions """ - tags = [] - first_column = '' + tags = '' marriage_status = lived_in_bc = live_at_least_year = separation_date = try_reconcile = reconciliation_period = None children_of_marriage = number_children_under_19 = number_children_over_19 = financial_support = certificate = provide_later = None @@ -456,39 +453,39 @@ def prequal_tag(source): divorce_reason['value'] = 'Lived apart for one year' if marriage_status: - tags.append(first_column + marriage_status['question__name'] + second_column + marriage_status['value'] + end_tag) + tags = format_table_data(tags, marriage_status['question__name'], marriage_status['value']) if lived_in_bc: - tags.append(first_column + lived_in_bc['question__name'] + second_column + lived_in_bc['value'] + end_tag) + tags = format_table_data(tags, lived_in_bc['question__name'], lived_in_bc['value']) if live_at_least_year: - tags.append(first_column + live_at_least_year['question__name'] + second_column + live_at_least_year['value'] + end_tag) + tags = format_table_data(tags, live_at_least_year['question__name'], live_at_least_year['value']) if separation_date: - tags.append(first_column + separation_date['question__name'] + second_column + separation_date['value'] + end_tag) + tags = format_table_data(tags, separation_date['question__name'], separation_date['value']) if try_reconcile: - tags.append(first_column + try_reconcile['question__name'] + second_column + try_reconcile['value'] + end_tag) + tags = format_table_data(tags, try_reconcile['question__name'], try_reconcile['value']) if try_reconcile and try_reconcile['value'] == 'YES' and reconciliation_period: - tags.append(first_column + reconciliation_period['question__name'] + second_column + reconciliation_period_reformat(reconciliation_period['value']) + end_tag) + tags = format_table_data(tags, reconciliation_period['question__name'], reconciliation_period_reformat(reconciliation_period['value'])) if children_of_marriage: - tags.append(first_column + children_of_marriage['question__name'] + second_column + children_of_marriage['value'] + end_tag) + tags = format_table_data(tags, children_of_marriage['question__name'], children_of_marriage['value']) if children_of_marriage and children_of_marriage['value'] == 'YES' and number_children_under_19: - tags.append(first_column + number_children_under_19['question__name'] + second_column + number_children_under_19['value'] + end_tag) + tags = format_table_data(tags, number_children_under_19['question__name'], number_children_under_19['value']) if children_of_marriage and children_of_marriage['value'] == 'YES' and number_children_over_19: - tags.append(first_column + number_children_over_19['question__name'] + second_column + number_children_over_19['value'] + end_tag) + tags = format_table_data(tags, number_children_over_19['question__name'], number_children_over_19['value']) if children_of_marriage and children_of_marriage['value'] == 'YES' and number_children_over_19 and financial_support and financial_support['value']: - tags.append(first_column + financial_support['question__name'] + second_column + '
    '.join(json.loads(financial_support['value'])) + end_tag) + tags = format_table_data(tags, financial_support['question__name'], '
    '.join(json.loads(financial_support['value']))) if certificate: - tags.append(first_column + certificate['question__name'] + second_column + certificate['value'] + end_tag) + tags = format_table_data(tags, certificate['question__name'], certificate['value']) if certificate and certificate['value'] == 'NO' and provide_later: - tags.append(first_column + provide_later['question__name'] + second_column + provide_later['value'] + end_tag) + tags = format_table_data(tags, provide_later['question__name'], provide_later['value']) if certificate and provide_later and certificate['value'] == 'NO' and provide_later['value'] == 'YES' and provide_later_reason: - tags.append(first_column + provide_later_reason['question__name'] + second_column + process_list(provide_later_reason['value'].split('\n'), provide_later_reason['question_id']) + end_tag) + tags = format_table_data(tags, provide_later_reason['question__name'], process_list(provide_later_reason['value'].split('\n'), provide_later_reason['question_id'])) if certificate and provide_later and certificate['value'] == 'NO' and provide_later['value'] == 'NO' and not_provide_later_reason: - tags.append(first_column + not_provide_later_reason['question__name'] + second_column + process_list(not_provide_later_reason['value'].split('\n'), not_provide_later_reason['question_id']) + end_tag) + tags = format_table_data(tags, not_provide_later_reason['question__name'], process_list(not_provide_later_reason['value'].split('\n'), not_provide_later_reason['question_id'])) if marriage_status and marriage_status['value'] == 'Living together in a marriage like relationship' and in_english: - tags.append(first_column + in_english['question__name'] + second_column + in_english['value'] + end_tag) + tags = format_table_data(tags, in_english['question__name'], in_english['value']) if divorce_reason: - tags.append(first_column + divorce_reason['question__name'] + second_column + divorce_reason['value'] + end_tag) + tags = format_table_data(tags, divorce_reason['question__name'], divorce_reason['value']) - return ''.join(tags) + return tags @register.simple_tag @@ -497,10 +494,7 @@ def personal_info_tag(source): Reformat your information and your spouse step Also show/hide optional questions """ - tags = [] - first_column = '' + tags = '' name = other_name = other_name_list = last_name_born = last_name_before = None birthday = occupation = lived_bc = moved_bc = None @@ -528,25 +522,33 @@ def personal_info_tag(source): moved_bc = item if name: - tags.append(first_column + name['question__name'] + second_column + name['value'] + end_tag) + tags = format_table_data(tags, name['question__name'], name['value']) if other_name: - tags.append(first_column + other_name['question__name'] + second_column + other_name['value'] + end_tag) + tags = format_table_data(tags, other_name['question__name'], other_name['value']) if other_name and other_name['value'] == 'YES' and other_name_list: - tags.append(first_column + other_name_list['question__name'] + second_column + process_list(json.loads(other_name_list['value']), other_name_list['question_id']) + end_tag) + tags = format_table_data(tags, other_name_list['question__name'], process_list(json.loads(other_name_list['value']), other_name_list['question_id'])) if last_name_born: - tags.append(first_column + last_name_born['question__name'] + second_column + last_name_born['value'] + end_tag) + tags = format_table_data(tags, last_name_born['question__name'], last_name_born['value']) if last_name_before: - tags.append(first_column + last_name_before['question__name'] + second_column + last_name_before['value'] + end_tag) + tags = format_table_data(tags, last_name_before['question__name'], last_name_before['value']) if birthday: - tags.append(first_column + birthday['question__name'] + second_column + birthday['value'] + end_tag) + tags = format_table_data(tags, birthday['question__name'], birthday['value']) if occupation: - tags.append(first_column + occupation['question__name'] + second_column + occupation['value'] + end_tag) + tags = format_table_data(tags, occupation['question__name'], occupation['value']) if lived_bc and moved_bc and lived_bc['value'] == "Moved to B.C. on": - tags.append(first_column + lived_bc['question__name'] + second_column + lived_bc['value'] + ' ' + moved_bc['value'] + end_tag) + tags = format_table_data(tags, lived_bc['question__name'], lived_bc['value'] + ' ' + moved_bc['value']) if lived_bc and lived_bc['value'] != "Moved to B.C. on" and lived_bc: - tags.append(first_column + lived_bc['question__name'] + second_column + lived_bc['value'] + end_tag) + tags = format_table_data(tags, lived_bc['question__name'], lived_bc['value']) + + return tags + - return ''.join(tags) +def format_table_data(tags, question, response): + return format_html( + '{}', + tags, + question, + response) def reconciliation_period_reformat(lst): @@ -559,5 +561,5 @@ def reconciliation_period_reformat(lst): lst = [] period = "" for f_date, t_date in lst: - period += "From " + f_date + " to " + t_date + "
    " + period = format_html('{}From {} to {}
    ', period, f_date, t_date) return period
    {0}{1}
    {0}{1}
    {0}
    {0}
    {}
    {}
    {}
    {}
    {2}
    {2}
    ' - second_column = '' - end_tag = '
    ' - second_column = '' - end_tag = '
    ' - second_column = '' - end_tag = '
    ' - second_column = '' - end_tag = '
    ' - second_column = '' - end_tag = '
    {}{}