-
Notifications
You must be signed in to change notification settings - Fork 7
/
run.py
93 lines (77 loc) · 2.85 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import math
from fpl import fpl
from docs import API_PARAMETERS as parameters
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
VERSION = 1
CURRENT_YEAR = '2021'
NULL_RETURN = {
'version': VERSION,
'info': 'Learn more at github.com/codeforamerica/fplapi',
'name': 'FPL API'
}
CURRENT_USER_INCOME = 0.0
ALLOWED_INCOME_TYPES = ('annual', 'monthly')
def calculate_fpl_percentage(user_income_type, user_income, base_income):
user_income = user_income*12 if user_income_type == ALLOWED_INCOME_TYPES[1] else user_income
return round(100*(user_income/base_income), 2)
def calculate_rate(base, rate, size):
amount = base + (rate * (int(size) - 1))
return amount
def nice_amount(num):
nice = '${:,}'.format(num)
return nice
@app.route('/', methods=['GET'])
def index():
return render_template('index.html', parameters=parameters)
@app.route('/api', methods=['GET'])
def api():
# if the query includes arguments, do something with them
if (request.args):
# if no year is specified, default to CURRENT_YEAR
if (request.args.get('year')):
year = request.args.get('year')
else:
year = CURRENT_YEAR
base = fpl[year]['base']
rate = fpl[year]['rate']
# if no household size is specified, return an error
# TODO: return a different reponse with all household size info
if (request.args.get('size')):
household_size = request.args.get('size')
else:
return 'No household size specified in URL. Example: <code>size=4</code>.'
# If no income is specified, carry on with the computations
if (request.args.get('income')):
user_income = float(request.args.get('income'))
else:
user_income = CURRENT_USER_INCOME
# If no income type is specified, set annual as default type
if (request.args.get('income_type') and request.args.get('income_type') in ALLOWED_INCOME_TYPES):
user_income_type = request.args.get('income_type')
else:
user_income_type = ALLOWED_INCOME_TYPES[0]
income = calculate_rate(base, rate, household_size)
fpl_percentage = calculate_fpl_percentage(user_income_type, user_income, income)
return jsonify({
'request': {
'year': year,
'household_size': household_size,
'income': user_income
},
'info': {
'year_base': base,
'year_rate': rate
},
'amount': income,
'amount_nice': nice_amount(income),
'fpl_percentage': fpl_percentage
})
# otherwise return a version number and learn more
else:
return jsonify(NULL_RETURN)
if __name__ == '__main__':
app.run(debug=True)
def start():
return app