From 2acdce8e280c07a695424c6b9c1f3a768ce6064f Mon Sep 17 00:00:00 2001 From: Benard Ebinu Date: Thu, 18 Jan 2018 15:24:56 -0800 Subject: [PATCH] DIV-724: programmatically displaying fact sheet e if precondition is met --- .../apps/core/templatetags/summary_format.py | 191 +++++++++++++----- .../apps/core/utils/question_step_mapping.py | 8 +- edivorce/apps/core/views/main.py | 2 + 3 files changed, 148 insertions(+), 53 deletions(-) diff --git a/edivorce/apps/core/templatetags/summary_format.py b/edivorce/apps/core/templatetags/summary_format.py index f6df1ddf..49e5fdc6 100644 --- a/edivorce/apps/core/templatetags/summary_format.py +++ b/edivorce/apps/core/templatetags/summary_format.py @@ -1,3 +1,5 @@ +from collections import OrderedDict + from django import template import json @@ -38,7 +40,7 @@ def process_list(lst, question_key): else: for value in lst: if value and not value.isspace(): - tag.append('
  • ' + value + '
  • ') + tag.append('
  • ' + str(value) + '
  • ') tag.append('') return ''.join(tag) @@ -49,68 +51,153 @@ def format_row(question, response): ) -@register.simple_tag -def format_children(source): +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 + + +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, responses): + if len(responses) == 0: + return '' + + tags = [''] + tags.append('

    {}

    '.format(title)) + + for response in responses: + value = response['value'] + try: + value = json.loads(response['value']) + except: + pass + + if isinstance(value, list): + thead, header = format_head(value) + tags.append(""" +

    +

    {0}

    + + + {1} + + + """.format(response['question__name'], thead)) + + tags.append(process_fact_sheet_list(value, header)) + + tags.append(""" + +
    + """) + tags.append('') + return ''.join(tags) + + +@register.simple_tag(takes_context=True) +def format_children(context, source): """ :param source: :return: """ - question_to_heading = { - 'Your Children': { - 'claimant_children' - }, - 'What are you asking for?': { - 'have_separation_agreement', - 'have_court_order', - 'order_respecting_arrangement', - 'order_for_child_support', - 'child_support_act' - }, - 'Income & expenses': { - 'how_will_calculate_income', - 'annual_gross_income', - 'spouse_annual_gross_income' - }, - 'Are you or your spouse claiming undue hardship?': { - 'special_extraordinary_expenses' - }, - 'Payor & medical expenses': { - 'child_support_payor', - 'claimants_agree_to_child_support_amount', - 'medical_coverage_available', - 'child_support_payments_in_arrears' - }, - 'Other fact sheets': { - - } - } + question_to_heading = OrderedDict() + question_to_heading['Your Children'] = [ + 'claimant_children' + ] + question_to_heading['What are you asking for?'] = [ + 'have_separation_agreement', + 'have_court_order', + 'order_respecting_arrangement', + 'order_for_child_support', + 'child_support_act' + ] + question_to_heading['Income & expenses'] = [ + 'how_will_calculate_income', + 'annual_gross_income', + 'spouse_annual_gross_income' + ] + question_to_heading['Are you or your spouse claiming undue hardship?'] = [ + 'special_extraordinary_expenses', + 'claiming_undue_hardship', + 'Undue Hardship (Fact Sheet E)' + ] + question_to_heading['Payor & medical expenses'] = [ + 'child_support_payor', + 'claimants_agree_to_child_support_amount', + 'medical_coverage_available', + 'child_support_payments_in_arrears' + ] + question_to_heading['Other fact sheets'] = [ + ] + + fact_sheet_mapping = OrderedDict() + fact_sheet_mapping['Undue Hardship (Fact Sheet E)'] = [ + 'claimant_debts', + 'claimant_expenses', + 'supporting_non_dependents', + 'supporting_dependents', + 'supporting_disabled', + 'undue_hardship', + 'income_others', + 'total_income_others', + ] tags = [] # process mapped questions first working_source = source.copy() for title, questions in question_to_heading.items(): tags.append(format_row('{}'.format(title), '')) - for item in working_source: - q_id = item['question_id'] - if q_id in questions: - if q_id == 'claimant_children': - for child in json.loads(item['value']): - 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 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'])) - else: - value = item['value'] - try: - value = json.loads(item['value']) - except: - pass - if isinstance(value, list): - tags.append(format_row(item['question__name'], process_list(value, q_id))) - else: - tags.append(format_row(item['question__name'], value)) + + for question in questions: + if question in fact_sheet_mapping: + show_fact_sheet = False + if question == 'Undue Hardship (Fact Sheet E)' and context['derived']['show_fact_sheet_e']: + show_fact_sheet = True + + if show_fact_sheet: + responses = list(filter(lambda x: x['question_id'] in fact_sheet_mapping[question], working_source)) + tags.append(format_fact_sheet(question, responses)) + else: + item = list(filter(lambda x: x['question_id'] == question, working_source)) + + if len(item): + item = item.pop() + q_id = item['question_id'] + if q_id in questions: + if q_id == 'claimant_children': + for child in json.loads(item['value']): + 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 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'])) + else: + value = item['value'] + try: + value = json.loads(item['value']) + except: + pass + if isinstance(value, list): + tags.append(format_row(item['question__name'], process_list(value, q_id))) + else: + tags.append(format_row(item['question__name'], value)) return ''.join(tags) diff --git a/edivorce/apps/core/utils/question_step_mapping.py b/edivorce/apps/core/utils/question_step_mapping.py index 3cbcef3b..2563c9d1 100644 --- a/edivorce/apps/core/utils/question_step_mapping.py +++ b/edivorce/apps/core/utils/question_step_mapping.py @@ -101,7 +101,13 @@ question_step_mapping = { 'you_spouse_entered_agreement', 'claiming_undue_hardship', 'claimant_debts', - 'claimant_expenses' + 'claimant_expenses', + 'supporting_non_dependents', + 'supporting_dependents', + 'supporting_disabled', + 'undue_hardship', + 'income_others', + 'total_income_others', ], 'spousal_support': ['spouse_support_details', 'spouse_support_act'], 'property_and_debt': ['deal_with_property_debt', diff --git a/edivorce/apps/core/views/main.py b/edivorce/apps/core/views/main.py index ca53a1f5..b2f582fa 100644 --- a/edivorce/apps/core/views/main.py +++ b/edivorce/apps/core/views/main.py @@ -5,6 +5,7 @@ from django.shortcuts import render, redirect, render_to_response from django.utils import timezone from django.template import RequestContext +from edivorce.apps.core.utils.derived import get_derived_data from ..decorators import bceid_required, intercept from ..utils.question_step_mapping import list_of_registries from ..utils.step_completeness import get_step_status, is_complete, get_formatted_incomplete_list @@ -193,6 +194,7 @@ def question(request, step, sub_step=None): responses_dict['registries'] = sorted(list_of_registries) responses_dict['sub_step'] = sub_step + responses_dict['derived'] = get_derived_data(get_responses_from_db(request.user)) return render(request, template_name=template, context=responses_dict)