2019-01-30 00:39:35 +00:00
from datetime import date
2019-02-07 23:32:21 +00:00
#### Classes for policy, sections.
2019-01-30 00:39:35 +00:00
2019-02-07 23:32:21 +00:00
class Policy ( ) :
def __init__ ( self ) :
self . sections = [ ]
def add_section ( self , section ) :
self . sections . append ( section )
class Section ( ) :
2019-02-08 01:13:43 +00:00
def __init__ ( self , title = " Section " , html_description = " " , required = False ,
auto_submit = False , fields = { } ) :
2019-02-07 23:32:21 +00:00
self . title = title
self . html_description = html_description
self . required = required
self . auto_submit = auto_submit
self . fields = fields
self . rules = [ ]
def add_rule ( self , title = " Rule " , rule = None , rule_break_text = " " ) :
rule = {
" title " : title ,
" rule " : rule ,
" rule_break_text " : rule_break_text ,
}
self . rules . append ( rule )
#### Policy configuration begin here
pol = Policy ( )
2019-01-30 00:39:35 +00:00
#### General
#### Section 0
general_section = Section (
title = " General Info " ,
html_description = " " ,
fields = {
2019-02-07 23:32:21 +00:00
" destination " : { " label " : " Destination City " , " type " : " string " } ,
2019-01-30 00:39:35 +00:00
}
)
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? "
)
2019-02-07 23:32:21 +00:00
pol . add_section ( general_section )
2019-01-30 00:39:35 +00:00
#### Flight
#### Section 1
flight_section = Section (
title = " Flight Info " ,
html_description = " <p>Enter flight details here.</p> " ,
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 " } ,
2019-02-08 02:13:32 +00:00
" layovers " : { " label " : " Transit wait " , " type " : " integer " } ,
2019-01-30 00:39:35 +00:00
}
)
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 "
)
2019-02-07 23:32:21 +00:00
pol . add_section ( flight_section )
2019-01-30 00:39:35 +00:00
#### Lodging
#### Section 2
lodging_section = Section (
title = " Hotel Info " ,
2019-02-08 01:13:43 +00:00
html_description = " <p>Enter hotel info here. \n Per diem rates can be found at "
" <a href= ' https://www.gsa.gov/travel/plan-book/per-diem-rates ' ></a></p> " ,
2019-01-30 00:39:35 +00:00
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
2019-02-07 23:32:21 +00:00
lodging_section . add_rule (
2019-01-30 00:39:35 +00:00
title = " " ,
rule = nightly_rate_check ,
rule_break_text = " The average nightly rate cannot be more than the USGSA rate. "
)
2019-02-07 23:32:21 +00:00
pol . add_section ( lodging_section )
2019-01-30 00:39:35 +00:00
#### Local Transportation
#### Section 3
transport_section = Section (
title = " Local Transportation " ,
html_description = " <p>How much did you spend on local transportation, in total?</p> " ,
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. "
)
2019-02-07 23:32:21 +00:00
pol . add_section ( transport_section )
2019-01-30 00:39:35 +00:00
#### Per Diem
#### Section 4
per_diem_section = Section (
title = " Per Diem " ,
html_description = " <p>Enter info about meals and incidentals here. \n Per diem rates can be found at <a href= ' https://www.gsa.gov/travel/plan-book/per-diem-rates ' ></a></p> " ,
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. "
)
2019-02-07 23:32:21 +00:00
pol . add_section ( per_diem_section )