86 lines
4.2 KiB
Python
86 lines
4.2 KiB
Python
import csv
|
|
import os
|
|
from flask import render_template, request, url_for, session, redirect, current_app, send_file
|
|
from flask_login import current_user
|
|
from app.playgrounds import bp
|
|
from app.playgrounds import forms
|
|
from werkzeug.utils import secure_filename
|
|
from app.playgrounds.controller import load_csv_data, compute_risk_pool_assets, _fetch_data_from_fannie, load_default_rate_file, compute_mre
|
|
from datetime import datetime
|
|
# import app.auth.google
|
|
|
|
|
|
@bp.route('/risk_pool_generator', methods=['GET', 'POST'])
|
|
def generate_risk_pool():
|
|
# _fetch_data_from_fannie(2008)
|
|
form = forms.HpiValuePredictForm(request.form)
|
|
if form.validate_on_submit():
|
|
default_risks_file = form.default_risks.name
|
|
saved_def_risks_file_path = os.path.join(current_app.config['UPLOAD_PATH'],
|
|
f'{secure_filename(default_risks_file)}.csv')
|
|
|
|
example_homes_file = form.example_homes.name
|
|
saved_ex_homes_file_path = os.path.join(current_app.config['UPLOAD_PATH'],
|
|
f'{secure_filename(example_homes_file)}.csv')
|
|
|
|
pool_makeup_file = form.pool_makeup.name
|
|
saved_makeup_file_path = os.path.join(current_app.config['UPLOAD_PATH'],
|
|
f'{secure_filename(pool_makeup_file)}.csv')
|
|
|
|
hpi_accrual_file = form.hpi_accrual_rate.name
|
|
saved_hpi_acc_file_path = os.path.join(current_app.config['UPLOAD_PATH'],
|
|
f'{secure_filename(hpi_accrual_file)}.csv')
|
|
|
|
for f in [hpi_accrual_file, pool_makeup_file, example_homes_file, default_risks_file]:
|
|
|
|
if not os.path.exists(current_app.config['UPLOAD_PATH']):
|
|
os.makedirs(current_app.config['UPLOAD_PATH'])
|
|
|
|
csv_data = request.files[f].read().decode('utf-8').replace('\r', '')
|
|
f = os.path.join(current_app.config['UPLOAD_PATH'], f'{secure_filename(f)}.csv')
|
|
open(f, 'w+').write(csv_data)
|
|
|
|
home_data, hpi_accrual_data = load_csv_data(saved_def_risks_file_path, saved_ex_homes_file_path, saved_makeup_file_path, saved_hpi_acc_file_path)
|
|
|
|
risk_pool_take = form.risk_pool_take_percent.data
|
|
occupancy_fee_rate = form.tic_fee.data
|
|
|
|
rp_asset_liability_data = compute_risk_pool_assets(home_data, hpi_accrual_data, form.number_of_homes.data, risk_pool_take, occupancy_fee_rate)
|
|
|
|
time_str = datetime.now().strftime('%Y%m%d_%H%M%S')
|
|
out_file_path = os.path.join(current_app.config['UPLOAD_PATH'], f'Risk_Pool_Data_{time_str}.csv')
|
|
|
|
with open(out_file_path, 'w', newline='') as csv_out:
|
|
field_names = ['Time Period', 'Asset Token Value', 'HPI Token Value', 'Cash in Pool', 'Total Pool Assets', 'Pool Liability', 'Ratio', 'Home Count']
|
|
csv_writer = csv.DictWriter(csv_out, fieldnames=field_names)
|
|
csv_writer.writeheader()
|
|
for row in rp_asset_liability_data:
|
|
csv_writer.writerow(row)
|
|
|
|
return send_file(os.path.join('..', out_file_path), as_attachment=True)
|
|
|
|
return render_template('risk_pool_generator.html', form=form)
|
|
|
|
|
|
@bp.route('/mre', methods=['GET', 'POST'])
|
|
def mre_playground():
|
|
form = forms.MREForm(request.form)
|
|
|
|
if form.validate_on_submit():
|
|
# save the uploaded default rate file
|
|
default_risks_file = form.default_rates.name
|
|
saved_def_risks_file_path = os.path.join(current_app.config['UPLOAD_PATH'],
|
|
f'{secure_filename(default_risks_file)}.csv')
|
|
|
|
csv_data = request.files[default_risks_file].read().decode('utf-8').replace('\r', '')
|
|
f = os.path.join(current_app.config['UPLOAD_PATH'], f'{secure_filename(default_risks_file)}.csv')
|
|
open(f, 'w+').write(csv_data)
|
|
|
|
default_data = load_default_rate_file(saved_def_risks_file_path)
|
|
|
|
mre = compute_mre({}, form.occupant_income.data, form.occupant_fico.data, form.home_price.data, default_data)
|
|
return render_template('mre_generator.html', form=form, mre=f'{mre * 100:.2f}%')
|
|
|
|
return render_template('mre_generator.html', form=form, mre=None)
|
|
|