From a70d9aa6bcd3b303b4247e85b0e0f4db1de19b2e Mon Sep 17 00:00:00 2001 From: kououken Date: Tue, 29 Jan 2019 16:39:35 -0800 Subject: [PATCH 1/2] Moved policy files into 'backend/' and fixed (most) formatting issues. --- .../policies => backend}/hasher.py | 8 +- back/backend/policy.py | 263 ++++++++++++++++++ back/reimbursinator/policies/simple_policy.py | 144 ---------- 3 files changed, 267 insertions(+), 148 deletions(-) rename back/{reimbursinator/policies => backend}/hasher.py (71%) create mode 100644 back/backend/policy.py delete mode 100644 back/reimbursinator/policies/simple_policy.py diff --git a/back/reimbursinator/policies/hasher.py b/back/backend/hasher.py similarity index 71% rename from back/reimbursinator/policies/hasher.py rename to back/backend/hasher.py index c831986..1d4352f 100644 --- a/back/reimbursinator/policies/hasher.py +++ b/back/backend/hasher.py @@ -1,13 +1,13 @@ import hashlib hasher = hashlib.md5() -with open ('simple_policy.py', 'rb') as afile: +with open('simple_policy.py', 'rb') as afile: buf = afile.read() hasher.update(buf) print("md5 of simple: " + hasher.hexdigest()) hasher = hashlib.md5() -with open ('moderate_policy.py', 'rb') as afile: +with open('moderate_policy.py', 'rb') as afile: buf = afile.read() hasher.update(buf) print("md5 of moderate: " + hasher.hexdigest()) @@ -15,13 +15,13 @@ print("md5 of moderate: " + hasher.hexdigest()) hasher = hashlib.sha1() -with open ('simple_policy.py', 'rb') as afile: +with open('simple_policy.py', 'rb') as afile: buf = afile.read() hasher.update(buf) print("sha1 of simple: " + hasher.hexdigest()) hasher = hashlib.sha1() -with open ('moderate_policy.py', 'rb') as afile: +with open('moderate_policy.py', 'rb') as afile: buf = afile.read() hasher.update(buf) print("sha1 of moderate: " + hasher.hexdigest()) diff --git a/back/backend/policy.py b/back/backend/policy.py new file mode 100644 index 0000000..1ba7390 --- /dev/null +++ b/back/backend/policy.py @@ -0,0 +1,263 @@ +# simple_policy.py + +from datetime import date + +from policy import Policy, Section + + + +# - For the rules, should one refer to fields by 'section.fields.x' + +# or by the section name eg. 'general_section.fields.x'? + + + + + +#### General + +#### Section 0 + +general_section = Section( + + title="General Info", + + html_description="", + + fields={ + + "destination": {"label": "Destination City", "type": "string"} + + } + +) + + + +general_section.add_rule( + + title="Destination city check", + + rule=lambda report, section: section.field.destination == "Timbuktu", + + rule_break_text="What did the cowboy say about Tim, his wild horse?" + +) + + + +Policy.add_section(general_section) + + + +#### Flight + +#### Section 1 + +flight_section = Section( + + title="Flight Info", + + html_description="

Enter flight details here.

", + + fields={ + + "international": {"label": "Is this an international flight?", "type": "boolean"}, + + "departure_date": {"label": "Departure date", "type": "date"}, + + "return_date": {"label": "Return date", "type": "date"}, + + "fare": {"label": "Fare", "type": "decimal"}, + + } + +) + + + +flight_section.add_rule( + + title="Airline fare pre-approval check", + + rule=lambda report, section: section.fields.fare < 500, + + rule_break_text="Fares cannot be more than $500" + +) + + + +Policy.add_section(flight_section) + + + +#### Lodging + +#### Section 2 + +lodging_section = Section( + + title="Hotel Info", + + html_description="

Enter hotel info here.\nPer diem rates can be found at

", + + fields={ + + "check-in_date": {"label": "Check-in date", "type": "date"}, + + "check-out_date": {"label": "Check-out date", "type": "date"}, + + "rate": {"label": "Per diem nightly rate", "type": "decimal"}, + + "cost": {"label": "Total Cost", "type": "decimal"} + + } + +) + + + +def nightly_rate_check(report, section): + + checkin_date = date(section.fields.checkin_date) + + checkout_date = date(section.fields.checkout_date) + + duration = checkout_date - checkin_date + + return section.fields.cost <= duration * section.fields.rate + + + +section.add_rule( + + title="", + + rule=nightly_rate_check, + + rule_break_text="The average nightly rate cannot be more than the USGSA rate." + +) + + + +Policy.add_section(lodging_section) + + + +#### Local Transportation + +#### Section 3 + +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?", "type": "decimal"}, + + "cost": {"label": "Total cost", "type": "decimal"} + + } + +) + + + +transport_section.add_rule( + + title="Total cost check", + + rule=lambda report, section: section.fields.cost <= section.fields.duration * 10, + + rule_break_text="Local transportation costs must be less than $10 per day, on average." + +) + + + +Policy.add_section(transport_section) + + + +#### Per Diem + +#### Section 4 + +per_diem_section = Section( + + title="Per Diem", + + html_description="

Enter info about meals and incidentals here.\nPer diem rates can be found at

", + + fields={ + + "duration": {"label": "How many days was your trip?", "type": "decimal"}, + + "rate": {"label": "What is the per diem rate for your destination?", "type": "decimal"}, + + "cost": {"label": "Total Cost for meals and incidentals", "type": "decimal"} + + } + +) + + + +per_diem_section.add_rule( + + title="Per Diem Cost Check", + + rule=lambda report, section: section.fields.cost <= section.fields.duration * section.fields.rate, + + rule_break_text="The average cost per day for per diem expenses cannot be more than the rate specified by the USGSA." + +) + + + +Policy.add_section(per_diem_section) + + + +''' + +Section( + + title="", + + html_description="

", + + fields={ + + "": {"label": "", "type": ""} + + } + +) + + + +section.add_rule( + + title="", + + rule=lambda report, section: boolean_statement, + + rule_break_text="" + +) + + + +#// or, for a rule which doesn’t apply to a specific section... + +#// + +#// add_general_rule(...) + +''' \ No newline at end of file diff --git a/back/reimbursinator/policies/simple_policy.py b/back/reimbursinator/policies/simple_policy.py deleted file mode 100644 index 16f1554..0000000 --- a/back/reimbursinator/policies/simple_policy.py +++ /dev/null @@ -1,144 +0,0 @@ -# simple_policy.py -from datetime import date - - -#TODO: -# - For the rules, should one refer to fields by 'section.fields.x' or by the section name eg. 'general_section.fields.x'? - - -#### General -#### Section 0 -general_section = Section( - title = "General Info", - html_description = "", - fields = { - "destination": {"label": "Destination City", "type": "string"} - } -) - -general_section.add_rule( - title = "Destination city check", - rule = lambda report, section: - if section.fields.destination == "Timbuktu": - return True - else: - return False - , - rule_break_text = "What did the cowboy say about Tim, his wild horse?" -) - - - -#### Flight -#### Section 1 -flight_section = Section( - title = "Flight Info", - html_description = "

Enter flight details here.

", - fields = { - "international": {"label": "Is this an international flight?", "type": "boolean"}, - "departure_date": {"label": "Departure date", "type": "date"}, - "return_date": {"label": "Return date", "type": "date"}, - "fare": {"label": "Fare", "type": "decimal"}, - } -) - -flight_section.add_rule( - title = "Airline fare pre-approval check", - rule = lambda report, section: - return section.fields.fare < 500 - , - rule_break_text = "Fares cannot be more than $500" -) - - - -#### Lodging -#### Section 2 -lodging_section = Section( - title = "Hotel Info", - html_description = "

Enter hotel info here.\nPer diem rates can be found at

", - fields = { - "check-in_date": {"label": "Check-in date", "type": "date"}, - "check-out_date": {"label": "Check-out date", "type": "date"}, - "rate": {"label": "Per diem nightly rate", "type": "decimal"}, - "cost": {"label": "Total Cost", "type": "decimal"} - } -) - -section.add_rule( - title = "", - rule = lambda report, section: - check-in_date = date(section.fields.check-in_date) - check-out_date = date(section.fields.check-out_date) - duration = check-out_date - check-in_date - return section.fields.cost <= duration * section.fields.rate - , - rule_break_text = "The average nightly rate cannot be more than the USGSA rate." -) - - - - -#### Local Transportation -#### Section 3 -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?", "type": "decimal"}, - "cost": {"label": "Total cost", "type": "decimal"} - } -) - -transport_section.add_rule( - title = "Total cost check", - rule = lambda report, section: - return section.fields.cost <= section.fields.duration * 10 - , - rule_break_text = "Local transportation costs must be less than $10 per day, on average." -) - - - - -#### Per Diem -#### Section 4 -per_diem_section = Section( - title = "Per Diem", - html_description = "

Enter info about meals and incidentals here.\nPer diem rates can be found at

", - fields = { - "duration": {"label": "How many days was your trip?", "type": "decimal"}, - "rate": {"label": "What is the per diem rate for your destination?", "type": "decimal"}, - "cost": {"label": "Total Cost for meals and incidentals", "type": "decimal"} - } -) - -per_diem_section.add_rule( - title = "Per Diem Cost Check", - rule = lambda report, section: - return section.fields.cost <= section.fields.duration * section.fields.rate - , - rule_break_text = "The average cost per day for per diem expenses cannot be more than the rate specified by the USGSA." -) - - - -''' -Section( - title = "", - html_description = "

", - fields = { - "": {"label": "", "type": ""} - } -) - -section.add_rule( - title = "", - rule = lambda report, section: return boolean_statement, - rule_break_text = "" -) - -#// or, for a rule which doesn’t apply to a specific section... -#// -#// add_general_rule(...) -''' From 875641fbe438466f5ac51b498705cdfebdb7e8a6 Mon Sep 17 00:00:00 2001 From: kououken Date: Tue, 29 Jan 2019 16:56:56 -0800 Subject: [PATCH 2/2] Fixed policy file spacing. --- back/backend/policy.py | 133 +---------------------------------------- 1 file changed, 1 insertion(+), 132 deletions(-) diff --git a/back/backend/policy.py b/back/backend/policy.py index 1ba7390..95e401b 100644 --- a/back/backend/policy.py +++ b/back/backend/policy.py @@ -1,263 +1,132 @@ # simple_policy.py - from datetime import date - from policy import Policy, Section - - # - For the rules, should one refer to fields by 'section.fields.x' - # or by the section name eg. 'general_section.fields.x'? - - - #### General - #### Section 0 - general_section = Section( - title="General Info", - html_description="", - fields={ - "destination": {"label": "Destination City", "type": "string"} - } - ) - - general_section.add_rule( - title="Destination city check", - rule=lambda report, section: section.field.destination == "Timbuktu", - rule_break_text="What did the cowboy say about Tim, his wild horse?" - ) - - Policy.add_section(general_section) - - #### Flight - #### Section 1 - flight_section = Section( - title="Flight Info", - html_description="

Enter flight details here.

", - fields={ - "international": {"label": "Is this an international flight?", "type": "boolean"}, - "departure_date": {"label": "Departure date", "type": "date"}, - "return_date": {"label": "Return date", "type": "date"}, - "fare": {"label": "Fare", "type": "decimal"}, - } - ) - - flight_section.add_rule( - title="Airline fare pre-approval check", - rule=lambda report, section: section.fields.fare < 500, - rule_break_text="Fares cannot be more than $500" - ) - - Policy.add_section(flight_section) - - #### Lodging - #### Section 2 - lodging_section = Section( - title="Hotel Info", - html_description="

Enter hotel info here.\nPer diem rates can be found at

", - fields={ - "check-in_date": {"label": "Check-in date", "type": "date"}, - "check-out_date": {"label": "Check-out date", "type": "date"}, - "rate": {"label": "Per diem nightly rate", "type": "decimal"}, - "cost": {"label": "Total Cost", "type": "decimal"} - } - ) - - def nightly_rate_check(report, section): - checkin_date = date(section.fields.checkin_date) - checkout_date = date(section.fields.checkout_date) - duration = checkout_date - checkin_date - return section.fields.cost <= duration * section.fields.rate - - section.add_rule( - title="", - rule=nightly_rate_check, - rule_break_text="The average nightly rate cannot be more than the USGSA rate." - ) - - Policy.add_section(lodging_section) - - #### Local Transportation - #### Section 3 - 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?", "type": "decimal"}, - "cost": {"label": "Total cost", "type": "decimal"} - } - ) - - transport_section.add_rule( - title="Total cost check", - rule=lambda report, section: section.fields.cost <= section.fields.duration * 10, - rule_break_text="Local transportation costs must be less than $10 per day, on average." - ) - - Policy.add_section(transport_section) - - #### Per Diem - #### Section 4 - per_diem_section = Section( - title="Per Diem", - html_description="

Enter info about meals and incidentals here.\nPer diem rates can be found at

", - fields={ - "duration": {"label": "How many days was your trip?", "type": "decimal"}, - "rate": {"label": "What is the per diem rate for your destination?", "type": "decimal"}, - "cost": {"label": "Total Cost for meals and incidentals", "type": "decimal"} - } - ) - - per_diem_section.add_rule( - title="Per Diem Cost Check", - rule=lambda report, section: section.fields.cost <= section.fields.duration * section.fields.rate, - rule_break_text="The average cost per day for per diem expenses cannot be more than the rate specified by the USGSA." - ) - - Policy.add_section(per_diem_section) - - ''' - Section( - title="", - html_description="

", - fields={ - "": {"label": "", "type": ""} - } - ) - - section.add_rule( - title="", - rule=lambda report, section: boolean_statement, - rule_break_text="" - ) - - #// or, for a rule which doesn’t apply to a specific section... - #// - #// add_general_rule(...) - -''' \ No newline at end of file +'''