-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
218 lines (163 loc) · 8.24 KB
/
Copy pathapp.py
File metadata and controls
218 lines (163 loc) · 8.24 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
from subprocess import check_output
import flask
from flask import request, redirect, url_for, make_response
from os import environ
import os
from flask import jsonify
from werkzeug import secure_filename
from clean_data import *
from create_csv import *
from datetime import datetime
## Build - delete csv file from db
## Build - API for Loan Officer data
##
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://microfinance:********@/microfinance', convert_unicode=True)
##
UPLOAD_FOLDER = '/groups/microfinance/csvfiles'
ALLOWED_EXTENSION = set(['csv'])
app = flask.Flask(__name__)
app.debug = True
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
## connection = engine.connect()
## connection.execute("sql command")
@app.route('/')
def index():
"""index page"""
return flask.render_template('index.html')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSION
def read_data(file_path):
connection = engine.connect()
file_path = file_path[file_path.index('csvfiles'):]
connection.execute("LOAD DATA LOCAL INFILE '" + file_path + "' IGNORE INTO TABLE Clients FIELDS TERMINATED BY ',' (@dummy, ClientID, Name)")
connection.execute("LOAD DATA LOCAL INFILE '" + file_path + "' IGNORE INTO TABLE Loans FIELDS TERMINATED BY ','(LoanID, ClientID, @dummy, LOID, DisbAmount, DisbDate, Category, @dummy, @dummy, @dummy, MatDate, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, BranchID)")
connection.execute("LOAD DATA LOCAL INFILE '" + file_path + "' IGNORE INTO TABLE LoanVal FIELDS TERMINATED BY ',' (LoanID, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, NextPmtAmt, NextPmt, Principal, @dummy, LateIntCollected, DaysPD, PDPrincipal, PDInterest, LateInt, PenaltyInt, ReportDate, LoanIDDate)")
connection.close()
@app.route('/fileupload', methods=['POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
path = (os.path.join(app.config['UPLOAD_FOLDER'], filename))
file.save(path)
clean_data(path,request.form['report_date'])
os.remove(path)
## function with database connection and mysql script
## takes the csv file path as an arguement
read_data(str(path[:path.index('.')] + request.form['report_date'] + '.csv'))
return flask.redirect('microfinance/')
return 'file upload failed'
@app.route('/OrgData',methods=['get'])
def get_org_data():
connection = engine.connect()
data = connection.execute("select LoanVal.ReportDate, COUNT(LoanVal.Principal) as 'loanClients', SUM(LoanVal.Principal) as 'principalBalance', SUM(IF( LoanVal.DaysPD > 1, LoanVal.Principal, 0)) as 'principal1Day', SUM(IF( LoanVal.DaysPD > 30, LoanVal.Principal, 0)) as 'Principal30Day' from Branch, Loans, LoanVal where Branch.BranchID = Loans.BranchID and Loans.LoanID = LoanVal.LoanID group by LoanVal.ReportDate;")
connection.close()
toJSON = []
for row in data:
row_as_dict = dict(row)
toJSON.append(row_as_dict)
for item in toJSON:
item['ReportDate'] = item['ReportDate'].strftime("%Y-%m-%d")
return jsonify(results = toJSON)
@app.route('/BranchData',methods=['get'])
def get_branch_data():
connection = engine.connect()
toJSON = []
branches = [10,11,12,13,14,15]
#branch = request.args['branch']
for branch in branches:
data = connection.execute("select LoanVal.ReportDate, COUNT(LoanVal.Principal) as 'loanClients', SUM(LoanVal.Principal) as 'principalBalance', SUM(IF( LoanVal.DaysPD > 1, LoanVal.Principal, 0)) as 'principal1Day', SUM(IF( LoanVal.DaysPD > 30, LoanVal.Principal, 0)) as 'Principal30Day' from Loans, LoanVal where Loans.LoanID = LoanVal.LoanID and Loans.BranchID = "+str(branch)+" group by LoanVal.ReportDate;")
branch_data = []
for row in data:
row_as_dict = dict(row)
branch_data.append(row_as_dict)
for item in branch_data:
item['ReportDate'] = item['ReportDate'].strftime("%Y-%m-%d")
if branch == 10:
headoffice = branch_data
if branch == 11:
arusha = branch_data
if branch == 12:
dar = branch_data
if branch == 13:
moshi = branch_data
if branch == 14:
tengeru = branch_data
if branch == 15:
himo = branch_data
connection.close()
return jsonify(ho = headoffice, ar = arusha, da = dar, mo = moshi, te = tengeru, hi = himo)
@app.route('/BranchDataFile',methods=['get'])
def get_branch_data_file():
connection = engine.connect()
branches = [10,11,12,13,14,15]
#branch = request.args['branch']
data_for_csv = []
for branch in branches:
data = connection.execute("select LoanVal.ReportDate, COUNT(LoanVal.Principal) as 'loanClients', SUM(LoanVal.Principal) as 'principalBalance', SUM(IF( LoanVal.DaysPD > 1, LoanVal.Principal, 0)) as 'principal1Day', SUM(IF( LoanVal.DaysPD > 30, LoanVal.Principal, 0)) as 'Principal30Day' from Loans, LoanVal where Loans.LoanID = LoanVal.LoanID and Loans.BranchID = "+str(branch)+" group by LoanVal.ReportDate;")
data_for_csv.append([branch,data])
connection.close()
(file_basename, server_path, file_size) = create_csv(data_for_csv)
return_file = open(server_path+file_basename, 'r')
response = make_response(return_file,200)
response.headers['Content-Description'] = 'File Transfer'
response.headers['Cache-Control'] = 'no-cache'
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = 'attachment; filename=%s' % file_basename
response.headers['Content-Length'] = file_size
return response
@app.route('/LoanOfficerData',methods=['GET'])
def get_loan_officer_data():
## needs to iterate through dates in db
## needs to accept arg BranchID
accepted_branches = ['10','11','12','13','14','15']
branch = request.args['branch']
if branch not in accepted_branches:
app.logger.debug("!!!!")
return flask.render_template('index.html'), 404
connection = engine.connect()
dates = []
date_query = connection.execute("select distinct ReportDate from LoanVal;")
for row in date_query:
row_as_dict = dict(row)
row_as_dict['ReportDate'] = row_as_dict['ReportDate'].strftime("%Y-%m-%d")
dates.append(row_as_dict)
loan_officer_data = []
for date in dates:
data = connection.execute("select LO.Name, COUNT(LoanVal.Principal) as 'loanClients', SUM(LoanVal.Principal) as 'principalBalance', SUM(IF( LoanVal.DaysPD > 1, LoanVal.Principal, 0)) as 'principal1Day', SUM(IF( LoanVal.DaysPD > 30, LoanVal.Principal, 0)) as 'Principal30Day' from LO, Loans, LoanVal where LO.LOID = Loans.LOID and Loans.LoanID = LoanVal.LoanID and LoanVal.ReportDate = '"+date['ReportDate']+"' and Loans.BranchID = "+branch+" group by LO.Name;")
temp = []
for row in data:
row_as_dict = dict(row)
temp.append(row_as_dict)
loan_officer_data.append([date['ReportDate'],temp])
connection.close()
return jsonify(results = loan_officer_data)
@app.route('/CsvDataFiles',methods=['GET','DELETE'])
def get_csv_files():
if request.method == 'GET':
csv_files = []
for files in os.walk(UPLOAD_FOLDER):
csv_files.append(files[2])
csv_files = csv_files[0]
return jsonify(files = csv_files)
if request.method == 'DELETE':
files_string = request.data
files_string = files_string.split('&')
files_delete = []
for item in files_string:
files_delete.append(item[item.index('=')+1:])
for item in files_delete:
date = item[item.index('.')-10:item.index('.')]
os.remove(UPLOAD_FOLDER+'/'+item)
connection = engine.connect()
connection.execute("DELETE FROM LoanVal WHERE ReportDate = '" + date + "';")
connection.close()
return "Deleted " + ' '.join(files_delete)
if __name__ == "__main__":
app.run(port=60050)