-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
310 lines (256 loc) · 9.96 KB
/
app.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from flask import Flask, render_template, redirect, url_for, request, session, jsonify, send_from_directory
from models import db, User, Case, Report, Predictive, Favorite
from passlib.hash import sha256_crypt
from forms import LoginForm, SignupForm
import simplejson as json
from sqlalchemy import func
app = Flask(__name__)
app.secret_key = 'shhhhitsasecret'
app.config[
'SQLALCHEMY_DATABASE_URI'] = 'postgres://rfacbcjizycwdl:58ec71e5deca626dea647631b65719669587a5b56f69c6b7072dceeeda368d29@ec2-50-17-203-51.compute-1.amazonaws.com:5432/dmiofp34d7sht';
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
# General Redirect
@app.route('/')
def redir():
return redirect(url_for('index'))
# Index
@app.route('/index')
def index():
# TODO: Cache this stuff. No need to make the call on every page load
yearOptions = db.session.query(Case.year).distinct().all()
yearOptions = sorted([y[0] for y in yearOptions])
typeOptions = db.session.query(Case.primary_type).distinct().all()
typeOptions = sorted(t[0] for t in typeOptions)
districtOptions = db.session.query(Case.district).distinct().all()
districtOptions = sorted(d[0] for d in districtOptions)
if 'username' in session:
user = User.query.filter_by(username=session['username']).first()
return render_template('index.html', title='Home', session_username=user.username, yearOptions=yearOptions, typeOptions=typeOptions, districtOptions=districtOptions)
else:
return render_template('index.html', title='Home', yearOptions=yearOptions, typeOptions=typeOptions, districtOptions=districtOptions)
# Signup
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
existing_user = User.query.filter_by(username=username).first()
if existing_user:
# TODO: Username already exists. Do something meaninful
return redirect(url_for('signup'))
else:
user = User(username=username, password=sha256_crypt.hash(password))
db.session.add(user)
db.session.commit()
return redirect(url_for('index'))
else:
return render_template('signup.html', title="Sign Up")
# Login
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username).first()
if user is None:
flash('Email is required')
return redirect(url_for('login'))
elif sha256_crypt.verify(password, user.password):
session['username'] = user.username
return redirect(url_for('index'))
else:
# TODO: Invalid Credentials
flash('Invalid login')
return redirect(url_for('login'))
else:
return render_template('login.html', title="Log In", form=LoginForm())
# Logout
@app.route('/logout', methods=['POST'])
def logout():
session.clear()
return redirect(url_for('index'))
# Save current filters
@app.route('/save', methods=['POST'])
def save():
if request.method == 'POST':
typeFilter = request.form['type']
yearFilter = request.form['year']
districtFilter = request.form['district']
user = User.query.filter_by(username=session['username']).first()
report = Report()
report.uid = user.uid
if typeFilter:
report.type = typeFilter
if yearFilter and yearFilter.isdigit():
report.year = int(yearFilter)
if districtFilter and districtFilter.isdigit():
report.district = int(districtFilter)
# report = Report(type=typeFilter, year=int(yearFilter), district=int(districtFilter), uid=user.uid)
db.session.add(report)
db.session.commit()
flash('Report saved!')
return jsonify( { 'result': True } )
# Delete Report
@app.route('/delete/<rid>', methods=['POST'])
def delete_report(rid):
user = User.query.filter_by(username=session['username']).first()
Report.query.filter_by(uid=user.uid, rid=rid).delete()
db.session.commit()
return redirect(url_for('saved_reports'))
# Saved Reports
@app.route('/saved-reports', methods=['GET', 'POST'])
def saved_reports():
if request.method == 'GET':
user = User.query.filter_by(username=session['username']).first()
saved_reports = Report.query.filter_by(uid=user.uid).all()
relationships = Favorite.query.filter_by(uid=user.uid).all()
cids_favorited = [f.cid for f in relationships]
favorited_reports = Case.query.filter(Case.id.in_(cids_favorited)).all()
return render_template('saved-reports.html', title="Saved Reports", saved_reports=saved_reports, favorited_reports=favorited_reports, session_username=user.username)
# Forecast
@app.route('/forecast', methods=['GET'])
def forecast():
if request.method == 'GET':
if 'username' in session:
user = User.query.filter_by(username=session['username']).first()
return render_template('forecast.html', title='Forecast', session_username=user.username)
return render_template('forecast.html', title='Forecast')
# Favorite from details page
@app.route('/favorite/<cid>', methods=['POST'])
def favorite(cid):
session_user = User.query.filter_by(username=session['username']).first()
case_to_favorite = Case.query.filter_by(id=cid).first()
new_favorite = Favorite(uid=session_user.uid, cid=case_to_favorite.id)
new_url = "/detail?id="+str(case_to_favorite.id)
db.session.add(new_favorite)
db.session.commit()
return redirect(new_url)
# Unfavorite from details page
@app.route('/unfavorite/<cid>/<from_url>', methods=['POST'])
def unfavorite(cid, from_url):
session_user = User.query.filter_by(username=session['username']).first()
case_to_unfavorite = Case.query.filter_by(id=cid).first()
delete_favorite = Favorite.query.filter_by(uid=session_user.uid, cid=case_to_unfavorite.id).first()
db.session.delete(delete_favorite)
db.session.commit()
if from_url == "saved_reports":
return redirect(url_for('saved_reports'))
else:
new_url = "/detail?id="+str(case_to_unfavorite.id)
return redirect(new_url)
# Details
@app.route('/detail')
def detail():
if 'username' in session:
user = User.query.filter_by(username=session['username']).first()
case = request.args.get('id')
if Favorite.query.filter_by(uid=user.uid, cid=case).first():
favorited = True
else:
favorited = False
return render_template('detail.html', title='Details', session_username=user.username, favorited=favorited, case=case)
return render_template('detail.html', title='Details', favorited=False)
############# API ENDPOINTS #############
# Get Crimes By Year
@app.route('/api/breakdown/year')
def crimes_by_year():
primary_type = request.args.get('type')
district = request.args.get('district')
query = db.session.query(Case.year, func.count(Case.id)).group_by(Case.year)
filters = []
if primary_type:
filters.append(Case.primary_type == primary_type)
if district and district.isdigit():
filters.append(Case.district == int(district))
query = query.filter(*filters)
breakdown = query.all()
ret = []
for b in breakdown:
ret.append({
'year': int(b[0]),
'count': int(b[1])
})
return jsonify(ret)
# Get Crimes by Type
@app.route('/api/breakdown/type')
def crimes_by_type():
year = request.args.get('year')
district = request.args.get('district')
filters = []
query = db.session.query(Case.primary_type, func.count(Case.id)).group_by(Case.primary_type)
if year and year.isdigit():
filters.append(Case.year == int(year))
if district and district.isdigit():
filters.append(Case.district == int(district))
query = query.filter(*filters)
breakdown = query.all()
ret = []
for b in breakdown:
ret.append({
'type': b[0],
'count': int(b[1])
})
return jsonify(ret)
# Get Crimes by District
@app.route('/api/breakdown/district')
def crimes_by_district():
year = request.args.get('year')
primary_type = request.args.get('type')
query = db.session.query(Case.district, func.count(Case.id)).group_by(Case.district)
filters = []
if year and year.isdigit():
filters.append(Case.year == int(year))
if primary_type:
filters.append(Case.primary_type == primary_type)
query = query.filter(*filters)
breakdown = query.all()
ret = []
for b in breakdown:
ret.append({
'district': int(b[0]),
'count': int(b[1])
})
return jsonify(ret)
# Get Dataset
@app.route('/api/data')
def get_dataset():
year = request.args.get('year')
primary_type = request.args.get('type')
district = request.args.get('district')
filters = []
query = db.session.query(Case).order_by(Case.date)
if year and year.isdigit():
filters.append(Case.year == int(year))
if primary_type:
filters.append(Case.primary_type == primary_type)
if district and district.isdigit():
filters.append(Case.district == int(district))
query = query.filter(*filters).limit(100)
dataset = query.all()
ret = []
for c in dataset:
ret.append(c.to_json())
return jsonify(ret)
# Get Crime by Id
@app.route('/api/cases/<id>')
def get_case_by_id(id):
if id:
crime = Case.query.filter_by(id=int(id)).first()
return jsonify(crime.to_json())
# Chicago Json
@app.route('/api/chicago')
def get_chicago():
return send_from_directory('static/data', 'chicago.json')
# Predictive Json
@app.route('/api/predictive')
def get_predictive():
#dataset = Predictive.query.limit(100).all()
dataset = Predictive.query.order_by(Predictive.id.desc()).limit(312).all()
dataset = dataset[::-1]
ret = []
for c in dataset:
ret.append(c.to_json())
return jsonify(ret)
if __name__ == '__main__':
app.run()