From f95bd9ecee99b3c047246515d64c17e641cc2b0c Mon Sep 17 00:00:00 2001 From: kououken Date: Thu, 14 Feb 2019 14:19:15 -0800 Subject: [PATCH] Implemented rule_violations in json for GET report. --- back/backend/policy.py | 20 ++++++++++---------- back/backend/views.py | 26 +++++++++++++++++++++++++- back/db.sqlite3 | Bin 105472 -> 105472 bytes 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/back/backend/policy.py b/back/backend/policy.py index 381405e..7af3281 100644 --- a/back/backend/policy.py +++ b/back/backend/policy.py @@ -45,7 +45,7 @@ general_section = Section( general_section.add_rule( title="Destination city check", - rule=lambda report, section: section.field.destination == "Timbuktu", + rule=lambda report, fields: fields['destination'] == "Timbuktu", rule_break_text="What did the cowboy say about Tim, his wild horse?" ) @@ -68,7 +68,7 @@ flight_section = Section( flight_section.add_rule( title="Airline fare pre-approval check", - rule=lambda report, section: section.fields.fare < 500, + rule=lambda report, fields: fields['fare'] < 500, rule_break_text="Fares cannot be more than $500" ) @@ -88,14 +88,14 @@ lodging_section = Section( } ) -def nightly_rate_check(report, section): - checkin_date = date(section.fields.checkin_date) - checkout_date = date(section.fields.checkout_date) +def nightly_rate_check(report, fields): + checkin_date = date(fields['checkin_date']) + checkout_date = date(fields['checkout_date']) duration = checkout_date - checkin_date - return section.fields.cost <= duration * section.fields.rate + return fields['cost'] <= duration * fields['rate'] lodging_section.add_rule( - title="", + title="Average nightly rate", rule=nightly_rate_check, rule_break_text="The average nightly rate cannot be more than the USGSA rate." ) @@ -108,14 +108,14 @@ transport_section = Section( title="Local Transportation", html_description="

How much did you spend on local transportation, in total?

", fields={ - "duration": {"label": "How many days was your trip?", "field_type": "decimal"}, + "duration": {"label": "How many days was your trip?", "field_type": "integer"}, "cost": {"label": "Total cost", "field_type": "decimal"} } ) transport_section.add_rule( title="Total cost check", - rule=lambda report, section: section.fields.cost <= section.fields.duration * 10, + rule=lambda report, fields: fields['cost'] <= fields['duration'] * 10, rule_break_text="Local transportation costs must be less than $10 per day, on average." ) @@ -136,7 +136,7 @@ per_diem_section = Section( per_diem_section.add_rule( title="Per Diem Cost Check", - rule=lambda report, section: section.fields.cost <= section.fields.duration * section.fields.rate, + rule=lambda report, fields: fields['cost'] <= fields['duration'] * fields['rate'], rule_break_text="The average cost per day for per diem expenses cannot be more than the rate specified by the USGSA." ) diff --git a/back/backend/views.py b/back/backend/views.py index 04fc6dd..4445435 100644 --- a/back/backend/views.py +++ b/back/backend/views.py @@ -28,15 +28,31 @@ def get_sections(r_id): # create a dict of arrays for section section_set = {"sections": []} queryset = Section.objects.filter(report_id=r_id) - for i in queryset: + for index in range(len(queryset)): + i = queryset[index] data = { "id": i.id, "completed": i.completed, "title": i.title, "html_description": i.html_description, + "rule_violations": [], } # append the fields for corresponding section data.update(get_fields(i.id)) + # process rules from the policy file if the section is completed + if i.completed: + rules = pol.sections[index].rules + for rule in rules: + try: + named_fields = generate_named_fields_for_section(data['fields']) + if not rule['rule'](data, named_fields): + info = { + "label": rule['title'], + "rule_break_text": rule['rule_break_text'], + } + data['rule_violations'].append(info) + except Exception as e: + print('Rule "{}" encountered an error. {}'.format(rule['title'], e)) # append section to the array section_set["sections"].append(data.copy()) @@ -67,6 +83,14 @@ def get_fields(s_id): return field_set +def generate_named_fields_for_section(fields): + result = {} + for field in fields: + key = field['field_name'] + value = field['value'] + result[key] = value + return result + # API Endpoints @api_view(['POST']) def report(request): diff --git a/back/db.sqlite3 b/back/db.sqlite3 index df940de7c8c054f4ee96162876249a47ea35ec8b..7617b83a92b94fe463a87a79d04c015cf53b7ac3 100644 GIT binary patch delta 3248 zcmbW34Qx}_6@cHp*ACAi4hkeGO$ap*LIUy2`+t5eKXHijA14MwVnayW#3XUzI59YnQ9HqzcXN4j)K|P@=Jpe ztb1jF6E%;jXhw}BWQ)bihz^NfUiAtdPSsV9CS?C0iH&Q5xSSz>A)k^@$VcP|eg|K~ zJFt!=?8cjn(iz@{PvUu_X{K^DegRM8Bp$_sxDWT>Aa2KgybhOV<1^nub9RC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