-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
592 lines (431 loc) · 19.4 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import os
from flask import Flask, session, flash, g, request, json
from flask_debugtoolbar import DebugToolbarExtension
from models import db, connect_db, User, UserHousehold, Household, SellerExpertise, OwnershipOccupancy, Associations, Roof, Basement, RoleType, FrequencyType, AssociationType
from sqlalchemy.exc import IntegrityError
CURR_USER_KEY = "curr_user"
app = Flask(__name__)
app.app_context().push()
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://react-capstone-2-dbserver.postgres.database.azure.com:5432/?user=dbadmin&password=Dbpassword#&sslmode=require&dbname=postgres'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY') # DEV
# app.config['SECRET_KEY'] = 'supersecret' # LOCAL
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
app.config['CORS_HEADERS'] = 'Content-Type'
debug = DebugToolbarExtension(app)
connect_db(app)
# db.create_all()
############################## AUTH ROUTES ##############################
def do_login(user):
"""Log a user in."""
session[CURR_USER_KEY] = user.id
def do_logout():
"""Log a user out."""
if CURR_USER_KEY in session:
del session[CURR_USER_KEY]
@app.before_request
def add_user_to_g():
"""If we are logged in, add curr_user to Flask global."""
if CURR_USER_KEY in session:
g.user = User.query.get(session[CURR_USER_KEY])
print('g.user added to session: ' + str(g.user))
else:
g.user = None
@app.after_request
def after_request(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = '*'
return response
@app.route("/api/register", methods=["GET", "POST", "OPTIONS"])
def register():
print(request.json)
username = request.json.get("username")
password = request.json.get("password")
if CURR_USER_KEY in session:
del session[CURR_USER_KEY]
try:
existingUser = User.query.filter(username == User.username).all()
if existingUser:
return ("Error: Username Already Taken")
user = User.register(username, password)
db.session.add(user)
db.session.commit()
except IntegrityError as e:
flash("Username already taken", 'danger')
do_login(user)
session["user_id"] = user.id
flash("You are now registered!", "success")
return str(user.id)
@app.route("/api/login", methods=["GET", "POST", "OPTIONS"])
def login():
print('Login start')
print(request.json)
username = request.json.get("username")
password = request.json.get("password")
user = User.authenticate(username, password)
if user:
do_login(user)
print(user)
flash(f"Welcome, {user.username}!", "success")
# Keep User Logged In
session["user_id"] = user.id
print(user.id)
return str(user.id)
else:
return ("Error: Login not found")
@app.route('/api/logout')
def logout():
"""Handle user logout."""
do_logout()
flash('You have been logged out.', 'success')
############################## USER ROUTES ##############################
@app.route("/api/user", methods=["GET", "OPTIONS"])
def getCurrentUser():
print("ARGS: " + request.args.get("userId"))
userId = request.args.get("userId")
print(userId)
# Get user by id
currentUser = User.query.filter(User.id == userId).one()
# Return user as json
return currentUser.as_dict()
# TODO Update
############################## HOUSEHOLD ROUTES ##############################
def row2dict(r):
return {c.name: str(getattr(r, c.name)) for c in r.__table__.columns}
@app.route("/api/households", methods=["GET", "OPTIONS"])
def getUserHouseholds():
print("ARGS: " + str(request.args))
userId = request.args.get("userId")
# Get list of households for user
households = UserHousehold.query\
.filter(UserHousehold.userID == userId)\
.all()
if not households:
return []
# Create returnable json list of households, turn each row into dict object
householdsList = json.dumps(households, default = lambda x:x.as_dict())
# Return households as json
return householdsList
@app.route("/api/household", methods=["GET", "OPTIONS"])
def getHousehold():
print("ARGS: " + str(request.args))
householdId = request.args.get("householdId")
# Get household by id
household = Household.query.filter(Household.id == householdId).one()
# Return household as json
return household.as_dict()
@app.route("/api/household", methods=["POST", "OPTIONS"])
def createHousehold():
print("ARGS: " + str(request.json))
# Create new household class
newHousehold = Household()
userId = request.json.get("userId")
newHousehold.name = request.json.get("name")
newHousehold.street_address = request.json.get("address")
newHousehold.city = request.json.get("city")
newHousehold.state = request.json.get("state")
newHousehold.zip = request.json.get("zip")
newHousehold.notes = request.json.get("notes")
# Add household to DB
db.session.add(newHousehold)
db.session.commit()
# Create new record for user household class
newUserHousehold = UserHousehold()
newUserHousehold.userID = userId
newUserHousehold.householdID = newHousehold.id
# Add user household to DB
db.session.add(newUserHousehold)
db.session.commit()
# Return household as json
return newHousehold.as_dict()
@app.route("/api/household", methods=["PATCH", "OPTIONS"])
def updateHousehold():
print(request.json)
householdId = request.json.get("id")
# Get household by id
household = Household.query.filter(Household.id == householdId).one()
# Update household class
household.name = request.json.get("name")
household.street_address = request.json.get("address")
household.city = request.json.get("city")
household.state = request.json.get("state")
household.zip = request.json.get("zip")
household.notes = request.json.get("notes")
# Update household in DB
db.session.commit()
# Return household as json
return household.as_dict()
@app.route("/api/household/delete", methods=["POST", "OPTIONS"])
def deleteHousehold():
print(request.json)
householdId = request.json.get("householdId")
# Get household by id
household = Household.query.filter(Household.id == householdId).one()
db.session.delete(household)
db.session.commit()
# Return if complete
return "Deleted"
############################## SELLER EXPERTISE ROUTES ##############################
@app.route("/api/sellerExpertise", methods=["GET", "OPTIONS"])
def getSellerExpertise():
print("ARGS: " + str(request.args))
householdId = request.args.get("householdId")
# Get sellerExpertise by id
# query by house id, if result set has a row, return row. else, return empty {}
expertiseRowCount = SellerExpertise.query.filter(SellerExpertise.id == householdId).count()
if expertiseRowCount > 0:
sellerExpertise = SellerExpertise.query.filter(SellerExpertise.id == householdId).one()
return sellerExpertise.as_dict()
# Return sellerExpertise as json
return ""
@app.route("/api/sellerExpertise", methods=["PATCH", "OPTIONS"])
def updateSellerExpertise():
print(request.json)
householdId = request.json.get("id")
# Get sellerExpertise by householdId
sellerExpertise = SellerExpertise.query.filter(SellerExpertise.id == householdId).one()
# Update sellerExpertise class
sellerExpertise.hasExpertise = request.json.get("hasExpertise")
sellerExpertise.isLandlord = request.json.get("isLandlord")
sellerExpertise.isRealEstateLicensee = request.json.get("isRealEstateLicensee")
sellerExpertise.notes = request.json.get("notes")
# Update sellerExpertise in DB
db.session.commit()
# Return sellerExpertise as json
return sellerExpertise.as_dict()
@app.route("/api/sellerExpertise", methods=["POST", "OPTIONS"])
def createSellerExpertise():
print(request.json)
# Create new seller expertise class
newSellerExpertise = SellerExpertise()
newSellerExpertise.householdID = request.json.get("householdId")
newSellerExpertise.hasExpertise = request.json.get("hasExpertise")
newSellerExpertise.isLandlord = request.json.get("isLandlord")
newSellerExpertise.isRealEstateLicensee = request.json.get("isRealEstateLicensee")
newSellerExpertise.notes = request.json.get("notes")
# Add seller expertise to DB
db.session.add(newSellerExpertise)
db.session.commit()
# Return sellerExpertise as json
return newSellerExpertise.as_dict()
############################## OWNERSHIP / OCCUPANCY ROUTES ##############################
@app.route("/api/ownershipOccupancy", methods=["GET", "OPTIONS"])
def getOwnershipOccupancy():
print("ARGS: " + str(request.args))
householdId = request.args.get("householdId")
# Get ownershipOccupancy by id
# query by house id, if result set has a row, return row. else, return empty {}
ownershipRowCount = OwnershipOccupancy.query.filter(OwnershipOccupancy.id == householdId).count()
if ownershipRowCount > 0:
ownershipOccupancy = OwnershipOccupancy.query.filter(OwnershipOccupancy.id == householdId).one()
return ownershipOccupancy.as_dict()
# Return ownershipOccupancy as json
return ""
@app.route("/api/ownershipOccupancy", methods=["PATCH", "OPTIONS"])
def updateOwnershipOccupancy():
print(request.json)
householdId = request.json.get("id")
# Get ownershipOccupancy by id
ownershipOccupancy = OwnershipOccupancy.query.filter(OwnershipOccupancy.id == householdId).one()
# Update ownershipOccupancy class
ownershipOccupancy.householdID = request.json.get("householdId")
ownershipOccupancy.roleTypeID = request.json.get("roleTypeId")
ownershipOccupancy.mostRecentOccupation = request.json.get("mostRecentOccupation")
ownershipOccupancy.isOccupiedBySeller = request.json.get("isOccupiedBySeller")
ownershipOccupancy.sellerOccupancyHistory = request.json.get("sellerOccupancyHistory")
ownershipOccupancy.hasHadPets = request.json.get("hasHadPets")
ownershipOccupancy.purchaseDate = request.json.get("PurchaseDate")
ownershipOccupancy.notes = request.json.get("notes")
# Update ownershipOccupancy in DB
db.session.commit()
# Return ownershipOccupancy as json
return ownershipOccupancy.as_dict()
@app.route("/api/ownershipOccupancy", methods=["POST", "OPTIONS"])
def createOwnershipOccupancy():
print(request.json)
# Create new ownershipOccupancy class
newOwnershipOccupancy = OwnershipOccupancy()
newOwnershipOccupancy.householdID = request.json.get("householdId")
newOwnershipOccupancy.roleTypeID = request.json.get("roleTypeId")
newOwnershipOccupancy.mostRecentOccupation = request.json.get("mostRecentOccupation")
newOwnershipOccupancy.isOccupiedBySeller = request.json.get("isOccupiedBySeller")
newOwnershipOccupancy.sellerOccupancyHistory = request.json.get("sellerOccupancyHistory")
newOwnershipOccupancy.hasHadPets = request.json.get("hasHadPets")
newOwnershipOccupancy.purchaseDate = request.json.get("PurchaseDate")
newOwnershipOccupancy.notes = request.json.get("notes")
# Add ownershipOccupancy to DB
db.session.add(newOwnershipOccupancy)
db.session.commit()
# Return ownershipOccupancy as json
return newOwnershipOccupancy.as_dict()
############################## ASSOCIATIONS ROUTES ##############################
@app.route("/api/associations", methods=["GET", "OPTIONS"])
def getAssociations():
print("ARGS: " +str(request.args))
householdId = request.args.get("householdId")
# Get associations by id
# query by house id, if result set has a row, return row. else, return empty {}
associationsRowCount = Associations.query.filter(Associations.id == householdId).count()
if associationsRowCount > 0:
associations = Associations.query.filter(Associations.id == householdId).one()
return associations.as_dict()
# Return associations as json
return ""
@app.route("/api/associations", methods=["PATCH", "OPTIONS"])
def updateAssociations():
print(request.json)
householdId = request.json.get("id")
# Get associations by id
associations = Associations.query.filter(Associations.id == householdId).one()
# Update associations class
associations.householdID = request.json.get("householdId")
associations.associationTypeID = request.json.get("associationTypeID")
associations.frequencyTypeID = request.json.get("frequencyTypeID")
associations.fees = request.json.get("fees")
associations.initiationFees = request.json.get("initiationFees")
associations.communityMaintenance = request.json.get("communityMaintenance")
associations.notes = request.json.get("notes")
# Update associations in DB
db.session.commit()
# Return associations as json
return associations.as_dict()
@app.route("/api/associations", methods=["POST", "OPTIONS"])
def createAssociations():
print(request.json)
# Create new associations class
newAssociations = Associations()
newAssociations.householdID = request.json.get("householdId")
newAssociations.associationTypeID = request.json.get("associationTypeID")
newAssociations.frequencyTypeID = request.json.get("frequencyTypeID")
newAssociations.fees = request.json.get("fees")
newAssociations.initiationFees = request.json.get("initiationFees")
newAssociations.communityMaintenance = request.json.get("communityMaintenance")
newAssociations.notes = request.json.get("notes")
# Add associations to DB
db.session.add(newAssociations)
db.session.commit()
# Return associations as json
return newAssociations.as_dict()
############################## ROOF ROUTES ##############################
@app.route("/api/roof", methods=["GET", "OPTIONS"])
def getRoof():
print("ARGS: " +str(request.args))
householdId = request.args.get("householdId")
# Get roof by id
# query by house id, if result set has a row, return row. else, return empty {}
roofRowCount = Roof.query.filter(Roof.id == householdId).count()
if roofRowCount > 0:
roof = Roof.query.filter(Roof.id == householdId).one()
return roof.as_dict()
# Return roof as json
return ""
@app.route("/api/roof", methods=["PATCH", "OPTIONS"])
def updateRoof():
print(request.json)
householdId = request.json.get("id")
# Get roof by id
roof = Roof.query.filter(Roof.id == householdId).one()
# Update roof class
roof.householdID = request.json.get("householdId")
roof.installationDate = request.json.get("installationDate")
roof.invoicePhoto = request.json.get("invoicePhoto")
roof.hasBeenReplaced = request.json.get("hasBeenReplaced")
roof.hadExistingMaterialRemoved = request.json.get("hadExistingMaterialRemoved")
roof.hasPreexistingLeaks = request.json.get("hasPreexistingLeaks")
roof.hasRainwaterProblems = request.json.get("hasRainwaterProblems")
roof.notes = request.json.get("notes")
# Update roof in DB
db.session.commit()
# Return roof as json
return roof.as_dict()
@app.route("/api/roof", methods=["POST", "OPTIONS"])
def createRoof():
print(request.json)
# Create new roof class
newRoof = Roof()
newRoof.householdID = request.json.get("householdId")
newRoof.installationDate = request.json.get("installationDate")
newRoof.invoicePhoto = request.json.get("invoicePhoto")
newRoof.hasBeenReplaced = request.json.get("hasBeenReplaced")
newRoof.hadExistingMaterialRemoved = request.json.get("hadExistingMaterialRemoved")
newRoof.hasPreexistingLeaks = request.json.get("hasPreexistingLeaks")
newRoof.hasRainwaterProblems = request.json.get("hasRainwaterProblems")
newRoof.notes = request.json.get("notes")
# Add roof to DB
db.session.add(newRoof)
db.session.commit()
# Return roof as json
return newRoof.as_dict()
############################## BASEMENT ROUTES ##############################
@app.route("/api/basement", methods=["GET", "OPTIONS"])
def getBasement():
print("ARGS: " +str(request.args))
householdId = request.args.get("householdId")
# Get basement by id
# query by house id, if result set has a row, return row. else, return empty {}
basementRowCount = Roof.query.filter(Basement.id == householdId).count()
if basementRowCount > 0:
basement = Basement.query.filter(Basement.id == householdId).one()
return basement.as_dict()
# Return basement as json
return ""
@app.route("/api/basement", methods=["PATCH", "OPTIONS"])
def updateBasement():
print(request.json)
householdId = request.json.get("id")
# Get basement by id
basement = Basement.query.filter(Basement.id == householdId).one()
# Update roof class
basement.householdID = request.json.get("householdId")
basement.hasSumpPump = request.json.get("hasSumpPump")
basement.pumpCount = request.json.get("pumpCount")
basement.hasBeenUsed = request.json.get("hasBeenUsed")
basement.hasWaterDamage = request.json.get("hasWaterDamage")
basement.hasRepairs = request.json.get("hasRepairs")
basement.hasDownspoutConnection = request.json.get("hasDownspoutConnection")
basement.notes = request.json.get("notes")
# Update basement in DB
db.session.commit()
# Return basement as json
return basement.as_dict()
@app.route("/api/basement", methods=["POST", "OPTIONS"])
def createBsement():
print(request.json)
# Create new basement class
newBasement = Basement()
newBasement.householdID = request.json.get("householdId")
newBasement.hasSumpPump = request.json.get("hasSumpPump")
newBasement.pumpCount = request.json.get("pumpCount")
newBasement.hasBeenUsed = request.json.get("hasBeenUsed")
newBasement.hasWaterDamage = request.json.get("hasWaterDamage")
newBasement.hasRepairs = request.json.get("hasRepairs")
newBasement.hasDownspoutConnection = request.json.get("hasDownspoutConnection")
newBasement.notes = request.json.get("notes")
# Add bsement to DB
db.session.add(newBasement)
db.session.commit()
# Return basement as json
return newBasement.as_dict()
############################## LOOKUP ROUTES ##############################
@app.route("/api/roleType", methods=["GET", "OPTIONS"])
def getRoleType():
print("ARGS: " +str(request.args))
roleTypeID = request.args.get("roleTypeId")
# Get roleType by id
roleType = RoleType.query.filter(RoleType.id == roleTypeID).one()
return roleType.as_dict()
@app.route("/api/frequencyType", methods=["GET", "OPTIONS"])
def getFrequencyType():
print("ARGS: " +str(request.args))
frequencyTypeID = request.args.get("frequencyTypeId")
# Get frequencyType by id
frequencyType = FrequencyType.query.filter(FrequencyType.id == frequencyTypeID).one()
return frequencyType.as_dict()
@app.route("/api/associationType", methods=["GET", "OPTIONS"])
def getAssociationType():
print("ARGS: " +str(request.args))
associationTypeID = request.args.get("associationTypeId")
# Get associationType by id
associationType = AssociationType.query.filter(AssociationType.id == associationTypeID).one()
return associationType.as_dict()