-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.py
685 lines (599 loc) · 35 KB
/
users.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# from myspace_api import get_new_propertyUID
from flask import request
from flask_restful import Resource
from flask_jwt_extended import jwt_required, get_jwt_identity
from data import connect, uploadImage, deleteImage, s3, processImage
import boto3
import json
from datetime import date, datetime, timedelta
from dateutil.relativedelta import relativedelta
from werkzeug.exceptions import BadRequest
import ast
def updateImages(imageFiles, property_uid):
content = []
for filename in imageFiles:
if type(imageFiles[filename]) == str:
bucket = 'io-mmu'
key = imageFiles[filename].split('/io-mmu/')[1]
data = s3.get_object(
Bucket=bucket,
Key=key
)
imageFiles[filename] = data['Body']
content.append(data['ContentType'])
else:
content.append('')
s3Resource = boto3.resource('s3')
bucket = s3Resource.Bucket('io-mmu')
bucket.objects.filter(Prefix=f'properties/{property_uid}/').delete()
images = []
for i in range(len(imageFiles.keys())):
filename = f'img_{i-1}'
if i == 0:
filename = 'img_cover'
key = f'properties/{property_uid}/{filename}'
image = uploadImage(
imageFiles[filename], key, content[i])
# imageFiles[filename], key, '')
images.append(image)
return images
class Properties(Resource):
def get(self, uid):
print('in Properties')
response = {}
# conn = connect()
print("Property User UID: ", uid)
if uid[:3] == '110':
print("In Owner ID")
with connect() as db:
# print("in connect loop")
print("In Find Applications")
applicationQuery = db.execute("""
-- FIND APPLICATIONS CURRENTLY IN PROGRESS
SELECT property_uid
, leases.*
, lease_fees
, t_details.*
FROM space.properties
LEFT JOIN space.leases ON property_uid = lease_property_id
LEFT JOIN (SELECT fees_lease_id, JSON_ARRAYAGG(JSON_OBJECT
('leaseFees_uid', leaseFees_uid,
'fee_name', fee_name,
'fee_type', fee_type,
'charge', charge,
'due_by', due_by,
'late_by', late_by,
'late_fee',late_fee,
'perDay_late_fee', perDay_late_fee,
'frequency', frequency,
'available_topay', available_topay,
'due_by_date', due_by_date
)) AS lease_fees
FROM space.leaseFees
GROUP BY fees_lease_id) AS lf ON fees_lease_id = lease_uid
LEFT JOIN space.t_details ON lease_uid = lt_lease_id
LEFT JOIN space.property_owner ON property_id = property_uid
WHERE (leases.lease_status = "NEW" OR leases.lease_status = "SENT" OR leases.lease_status = "REJECTED" OR leases.lease_status = "REFUSED" OR leases.lease_status = "PROCESSING" OR leases.lease_status = "TENANT APPROVED")
AND property_owner_id = \'""" + uid + """\'
""")
response["Applications"] = applicationQuery
# print("Query: ", applicationQuery)
with connect() as db:
propertiesQuery = db.execute("""
-- PROPERTY RENT STATUS FOR PROPERTIES
SELECT p.*
, pur_property_id, purchase_type, pur_due_date, pur_amount_due
, if(ISNULL(pur_status_value), "0", pur_status_value) AS pur_status_value
, if(ISNULL(purchase_status), "UNPAID", purchase_status) AS purchase_status
, pur_description, cf_month, cf_year
, CASE
WHEN ISNULL(contract_uid) THEN "NO MANAGER"
WHEN ISNULL(lease_status) THEN "VACANT"
WHEN ISNULL(purchase_status) THEN "UNPAID"
ELSE purchase_status
END AS rent_status
FROM (
-- Find properties
SELECT * FROM space.p_details
-- WHERE business_uid = "600-000032"
-- WHERE owner_uid = "110-000003"
WHERE owner_uid = \'""" + uid + """\'
-- WHERE business_uid = \'""" + uid + """\'
-- WHERE tenant_uid = \'""" + uid + """\'
) AS p
-- Link to rent status
LEFT JOIN (
SELECT -- *
pur_property_id
, purchase_type
, pur_due_date
, SUM(pur_amount_due) AS pur_amount_due
, MIN(pur_status_value) AS pur_status_value
, purchase_status
, pur_description
, MONTH(STR_TO_DATE(pur_due_date, '%m-%d-%Y')) AS cf_month
, YEAR(STR_TO_DATE(pur_due_date, '%m-%d-%Y')) AS cf_year
FROM space.purchases
WHERE purchase_type = "Rent"
AND LEFT(pur_payer, 3) = '350'
AND MONTH(STR_TO_DATE(pur_due_date, '%m-%d-%Y')) = MONTH(CURRENT_DATE)
AND YEAR(STR_TO_DATE(pur_due_date, '%m-%d-%Y')) = YEAR(CURRENT_DATE)
GROUP BY pur_property_id -- , pur_due_date
) AS pp
ON property_uid = pur_property_id
""")
# print("Query: ", propertiesQuery)
response["Property"] = propertiesQuery
print("In Maintenance Requests")
with connect() as db:
maintenanceQuery = db.execute("""
SELECT *
-- quote_business_id, quote_status, maintenance_request_status, quote_total_estimate
, CASE
WHEN maintenance_request_status = 'NEW' OR maintenance_request_status = 'INFO' THEN "NEW REQUEST"
WHEN maintenance_request_status = "SCHEDULED" THEN "SCHEDULED"
WHEN maintenance_request_status = 'CANCELLED' or quote_status = "FINISHED" THEN "COMPLETED"
WHEN quote_status = "SENT" OR quote_status = "REFUSED" OR quote_status = "REQUESTED"
OR quote_status = "REJECTED" OR quote_status = "WITHDRAWN" THEN "QUOTES REQUESTED"
WHEN quote_status = "ACCEPTED" OR quote_status = "SCHEDULE" THEN "QUOTES ACCEPTED"
WHEN quote_status = "COMPLETED" THEN "PAID"
ELSE quote_status
END AS maintenance_status
FROM (
SELECT *
FROM space.maintenanceRequests
LEFT JOIN (
SELECT *,
CASE
WHEN max_quote_rank = "10" THEN "REQUESTED"
WHEN max_quote_rank = "11" THEN "REFUSED"
WHEN max_quote_rank = "20" THEN "SENT"
WHEN max_quote_rank = "21" THEN "REJECTED"
WHEN max_quote_rank = "22" THEN "WITHDRAWN"
WHEN max_quote_rank = "30" THEN "ACCEPTED"
WHEN max_quote_rank = "40" THEN "SCHEDULE"
WHEN max_quote_rank = "50" THEN "SCHEDULED"
WHEN max_quote_rank = "60" THEN "RESCHEDULED"
WHEN max_quote_rank = "70" THEN "FINISHED"
WHEN max_quote_rank = "80" THEN "COMPLETED"
ELSE "0"
END AS quote_status
FROM
(
SELECT -- maintenance_quote_uid,
quote_maintenance_request_id AS qmr_id
-- , quote_status
, MAX(quote_rank) AS max_quote_rank
FROM (
SELECT -- *,
maintenance_quote_uid, quote_maintenance_request_id, quote_status,
-- , quote_pm_notes, quote_business_id, quote_services_expenses, quote_earliest_available_date,quote_earliest_available_time, quote_event_type, quote_event_duration, quote_notes, quote_created_date, quote_total_estimate, quote_maintenance_images, quote_adjustment_date
CASE
WHEN quote_status = "REQUESTED" THEN "10"
WHEN quote_status = "REFUSED" THEN "11"
WHEN quote_status = "SENT" THEN "20"
WHEN quote_status = "REJECTED" THEN "21"
WHEN quote_status = "WITHDRAWN" THEN "22"
WHEN quote_status = "ACCEPTED" THEN "30"
WHEN quote_status = "SCHEDULE" THEN "40"
WHEN quote_status = "SCHEDULED" THEN "50"
WHEN quote_status = "RESCHEDULED" THEN "60"
WHEN quote_status = "FINISHED" THEN "70"
WHEN quote_status = "COMPLETED" THEN "80"
ELSE 0
END AS quote_rank
FROM space.maintenanceQuotes
) AS qr
GROUP BY quote_maintenance_request_id
) AS qr_quoterank
) AS quote_summary ON maintenance_request_uid = qmr_id
) AS quotes
LEFT JOIN ( SELECT * FROM space.contracts WHERE contract_status = "ACTIVE") AS c ON maintenance_property_id = contract_property_id
-- WHERE contract_business_id = \'""" + uid + """\'
-- WHERE contract_business_id = "600-000003"
-- WHERE owner_uid = "110-000003"
# WHERE owner_uid = \'""" + uid + """\'
""")
# print("Query: ", propertiesQuery)
response["MaintenanceRequests"] = maintenanceQuery
elif uid[:3] == '600':
print("In Business ID")
# APPLICATIONS
with connect() as db:
# print("in connect loop")
print("In Find Applications")
applicationQuery = db.execute("""
-- FIND APPLICATIONS CURRENTLY IN PROGRESS
SELECT property_uid
, leases.*
, lease_fees
, t_details.*
FROM space.properties
LEFT JOIN space.leases ON property_uid = lease_property_id
LEFT JOIN (SELECT fees_lease_id, JSON_ARRAYAGG(JSON_OBJECT
('leaseFees_uid', leaseFees_uid,
'fee_name', fee_name,
'fee_type', fee_type,
'charge', charge,
'due_by', due_by,
'late_by', late_by,
'late_fee',late_fee,
'perDay_late_fee', perDay_late_fee,
'frequency', frequency,
'available_topay', available_topay,
'due_by_date', due_by_date
)) AS lease_fees
FROM space.leaseFees
GROUP BY fees_lease_id) AS lf ON fees_lease_id = lease_uid
LEFT JOIN space.t_details ON lease_uid = lt_lease_id
LEFT JOIN space.contracts ON contract_property_id = property_uid
WHERE leases.lease_status NOT IN ('ACTIVE', 'ACTIVE M2M', 'ENDED', 'TERMINATED')
AND contract_status = "ACTIVE"
AND contract_business_id = \'""" + uid + """\'
""")
response["Applications"] = applicationQuery
#PROPERTIES
with connect() as db:
print("in connect loop")
propertiesQuery = db.execute("""
-- PROPERTY RENT STATUS FOR PROPERTIES
SELECT p.*
, pur_property_id, purchase_type, pur_due_date, pur_amount_due
, if(ISNULL(pur_status_value), "0", pur_status_value) AS pur_status_value
, if(ISNULL(purchase_status), "UNPAID", purchase_status) AS purchase_status
, pur_description, cf_month, cf_year
, CASE
WHEN ISNULL(contract_uid) THEN "NO MANAGER"
WHEN ISNULL(lease_status) THEN "VACANT"
WHEN ISNULL(purchase_status) THEN "UNPAID"
ELSE purchase_status
END AS rent_status
FROM (
-- Find properties
SELECT * FROM space.p_details
-- WHERE business_uid = "600-000032"
-- WHERE owner_uid = "110-000003"
-- WHERE owner_uid = \'""" + uid + """\'
WHERE business_uid = \'""" + uid + """\'
-- WHERE tenant_uid = \'""" + uid + """\'
) AS p
-- Link to rent status
LEFT JOIN (
SELECT -- *
pur_property_id
, purchase_type
, pur_due_date
, SUM(pur_amount_due) AS pur_amount_due
, MIN(pur_status_value) AS pur_status_value
, purchase_status
, pur_description
, MONTH(STR_TO_DATE(pur_due_date, '%m-%d-%Y')) AS cf_month
, YEAR(STR_TO_DATE(pur_due_date, '%m-%d-%Y')) AS cf_year
FROM space.purchases
WHERE purchase_type = "Rent"
AND LEFT(pur_payer, 3) = '350'
AND MONTH(STR_TO_DATE(pur_due_date, '%m-%d-%Y')) = MONTH(CURRENT_DATE)
AND YEAR(STR_TO_DATE(pur_due_date, '%m-%d-%Y')) = YEAR(CURRENT_DATE)
GROUP BY pur_property_id -- , pur_due_date
) AS pp
ON property_uid = pur_property_id
""")
# print("Query: ", propertiesQuery)
response["Property"] = propertiesQuery
# NEW PM REQUESTS
print("In New PM Requests")
with connect() as db:
# print("in New PM Requests connect loop")
contractsQuery = db.execute("""
-- NEW PROPERTIES FOR MANAGER
SELECT *, CASE WHEN announcements IS NULL THEN false ELSE true END AS announcements_boolean
FROM space.o_details
LEFT JOIN space.properties ON property_id = property_uid
LEFT JOIN space.b_details ON contract_property_id = property_uid
LEFT JOIN (
SELECT announcement_properties, JSON_ARRAYAGG(JSON_OBJECT
('announcement_uid', announcement_uid,
'announcement_title', announcement_title,
'announcement_msg', announcement_msg,
'announcement_mode', announcement_mode,
'announcement_date', announcement_date,
'announcement_receiver', announcement_receiver
)) AS announcements
FROM space.announcements
GROUP BY announcement_properties) as t ON announcement_properties = property_uid
WHERE contract_business_id = \'""" + uid + """\' AND (contract_status = "NEW" OR contract_status = "SENT" OR contract_status = "REJECTED");
""")
# print("Query: ", propertiesQuery)
response["NewPMRequests"] = contractsQuery
# MAINTENANCE REQUESTS
print("In Maintenance Requests")
with connect() as db:
# print("in Maintenance Requests connect loop")
maintenanceQuery = db.execute("""
SELECT *
-- quote_business_id, quote_status, maintenance_request_status, quote_total_estimate
, CASE
WHEN maintenance_request_status = 'NEW' OR maintenance_request_status = 'INFO' THEN "NEW REQUEST"
WHEN maintenance_request_status = "SCHEDULED" THEN "SCHEDULED"
WHEN maintenance_request_status = 'CANCELLED' or quote_status = "FINISHED" THEN "COMPLETED"
WHEN quote_status = "SENT" OR quote_status = "REFUSED" OR quote_status = "REQUESTED"
OR quote_status = "REJECTED" OR quote_status = "WITHDRAWN" THEN "QUOTES REQUESTED"
WHEN quote_status = "ACCEPTED" OR quote_status = "SCHEDULE" THEN "QUOTES ACCEPTED"
WHEN quote_status = "COMPLETED" THEN "PAID"
ELSE quote_status
END AS maintenance_status
FROM (
SELECT *
FROM space.maintenanceRequests
LEFT JOIN (
SELECT *,
CASE
WHEN max_quote_rank = "10" THEN "REQUESTED"
WHEN max_quote_rank = "11" THEN "REFUSED"
WHEN max_quote_rank = "20" THEN "SENT"
WHEN max_quote_rank = "21" THEN "REJECTED"
WHEN max_quote_rank = "22" THEN "WITHDRAWN"
WHEN max_quote_rank = "30" THEN "ACCEPTED"
WHEN max_quote_rank = "40" THEN "SCHEDULE"
WHEN max_quote_rank = "50" THEN "SCHEDULED"
WHEN max_quote_rank = "60" THEN "RESCHEDULED"
WHEN max_quote_rank = "70" THEN "FINISHED"
WHEN max_quote_rank = "80" THEN "COMPLETED"
ELSE "0"
END AS quote_status
FROM
(
SELECT -- maintenance_quote_uid,
quote_maintenance_request_id AS qmr_id
-- , quote_status
, MAX(quote_rank) AS max_quote_rank
FROM (
SELECT -- *,
maintenance_quote_uid, quote_maintenance_request_id, quote_status,
-- , quote_pm_notes, quote_business_id, quote_services_expenses, quote_earliest_available_date,quote_earliest_available_time, quote_event_type, quote_event_duration, quote_notes, quote_created_date, quote_total_estimate, quote_maintenance_images, quote_adjustment_date
CASE
WHEN quote_status = "REQUESTED" THEN "10"
WHEN quote_status = "REFUSED" THEN "11"
WHEN quote_status = "SENT" THEN "20"
WHEN quote_status = "REJECTED" THEN "21"
WHEN quote_status = "WITHDRAWN" THEN "22"
WHEN quote_status = "ACCEPTED" THEN "30"
WHEN quote_status = "SCHEDULE" THEN "40"
WHEN quote_status = "SCHEDULED" THEN "50"
WHEN quote_status = "RESCHEDULED" THEN "60"
WHEN quote_status = "FINISHED" THEN "70"
WHEN quote_status = "COMPLETED" THEN "80"
ELSE 0
END AS quote_rank
FROM space.maintenanceQuotes
) AS qr
GROUP BY quote_maintenance_request_id
) AS qr_quoterank
) AS quote_summary ON maintenance_request_uid = qmr_id
) AS quotes
LEFT JOIN ( SELECT * FROM space.contracts WHERE contract_status = "ACTIVE") AS c ON maintenance_property_id = contract_property_id
WHERE contract_business_id = \'""" + uid + """\'
-- WHERE contract_business_id = "600-000003"
-- WHERE owner_uid = "110-000003"
# -- WHERE owner_uid = \'""" + uid + """\'
""")
# print("Query: ", propertiesQuery)
response["MaintenanceRequests"] = maintenanceQuery
elif uid[:3] == '350':
print("In Tenant ID")
with connect() as db:
# print("in connect loop")
print("In Find Applications")
applicationQuery = db.execute("""
-- FIND APPLICATIONS CURRENTLY IN PROGRESS
SELECT property_uid
, leases.*
, lease_fees
, t_details.*
FROM space.properties
LEFT JOIN space.leases ON property_uid = lease_property_id
LEFT JOIN (SELECT fees_lease_id, JSON_ARRAYAGG(JSON_OBJECT
('leaseFees_uid', leaseFees_uid,
'fee_name', fee_name,
'fee_type', fee_type,
'charge', charge,
'due_by', due_by,
'late_by', late_by,
'late_fee',late_fee,
'perDay_late_fee', perDay_late_fee,
'frequency', frequency,
'available_topay', available_topay,
'due_by_date', due_by_date
)) AS lease_fees
FROM space.leaseFees
GROUP BY fees_lease_id) AS lf ON fees_lease_id = lease_uid
LEFT JOIN space.t_details ON lease_uid = lt_lease_id
WHERE (leases.lease_status = "NEW" OR leases.lease_status = "SENT" OR leases.lease_status = "REJECTED" OR leases.lease_status = "REFUSED" OR leases.lease_status = "PROCESSING" OR leases.lease_status = "TENANT APPROVED")
AND lt_tenant_id = \'""" + uid + """\';
""")
response["Applications"] = applicationQuery
with connect() as db:
# print("Query: ", propertiesQuery)
propertiesQuery = db.execute("""
-- PROPERTIES BY TENANT
SELECT p.*
, CASE
WHEN ISNULL(contract_uid) THEN "NO MANAGER"
WHEN (lease_status = 'ACTIVE' AND payment_status IS NOT NULL) THEN payment_status
WHEN (lease_status = 'ACTIVE' AND payment_status IS NULL) THEN 'UNPAID'
ELSE 'VACANT'
END AS rent_status
-- , contract_uid AS actual_contract_uid
-- , lease_status AS actual_lease_status
FROM (
SELECT * FROM space.p_details
-- WHERE business_uid = "600-000003"
-- WHERE owner_uid = "110-000003"
-- WHERE owner_uid = \'""" + uid + """\'
-- WHERE business_uid = \'""" + uid + """\'
WHERE tenant_uid = \'""" + uid + """\' AND contract_end_date < curdate()
-- WHERE tenant_uid = '350-000002' AND contract_end_date < curdate()
) as p
LEFT JOIN (
SELECT *
FROM space.pp_status
WHERE (purchase_type = "RENT" OR ISNULL(purchase_type))
AND LEFT(pur_payer, 3) = '350'
AND (cf_month = DATE_FORMAT(NOW(), '%M') OR ISNULL(cf_month))
AND (cf_year = DATE_FORMAT(NOW(), '%Y') OR ISNULL(cf_year))
) AS r ON p.property_uid = pur_property_id
LEFT JOIN (
SELECT -- *
property_owner_id
, maintenance_property_id
, COUNT(maintenance_property_id) AS num_open_maintenace_req
FROM space.maintenanceRequests
LEFT JOIN space.o_details ON maintenance_property_id = property_id
WHERE maintenance_request_status != "COMPLETED" AND maintenance_request_status != "CANCELLED"
GROUP BY maintenance_property_id
) AS m ON p.property_uid = maintenance_property_id
order by lease_status;
""")
# print("Query: ", propertiesQuery)
response["Property"] = propertiesQuery
# return response
elif uid[:3] == '200':
print("In Property ID")
with connect() as db:
# print("in connect loop")
print("In Find Property with Appliances")
propertiesQuery = db.execute("""
-- RETURN PROPERTIES WITH APPLIANCES
SELECT *
FROM space.p_details AS p
-- WHERE property_uid = '200-000001'
WHERE property_uid = \'""" + uid + """\'
GROUP BY p.property_uid;
""")
response["Property"] = propertiesQuery
else:
print("UID Not found")
response["Property"] = "UID Not Found"
return response
def post(self):
print("\nIn Property POST")
response = {}
appliances = {}
payload = request.form.to_dict()
print("Property Add Payload: ", payload)
# Verify uid has NOT been included in the data
if payload.get('property_uid'):
print("property_uid found. Please call PUT endpoint")
raise BadRequest("Request failed, UID found in payload.")
with connect() as db:
newPropertyUID = db.call('new_property_uid')['result'][0]['new_id']
key = {'property_uid': newPropertyUID}
print("Property Key: ", key)
# --------------- PROCESS IMAGES ------------------
processImage(key, payload)
print("Payload after function: ", payload)
# --------------- PROCESS IMAGES ------------------
# Add Appliances (if provided)
print("\nAppliances: ", payload.get('appliances'), type(payload.get('appliances')))
try:
appliances = json.loads(payload.pop('appliances'))
# appliances = "{\"appliances\":[\"050-000023\",\"050-000024\",\"050-000025\"]}"
print("Appliance Data: ", appliances, type(appliances))
if appliances:
newAppliance = {}
# appliances is now a list of strings
for appliance in appliances:
print(f"Appliance: {appliance}")
# newRequestID = db.call('new_property_uid')['result'][0]['new_id']
# print(newRequestID)
NewApplianceID = db.call('new_appliance_uid')['result'][0]['new_id']
newAppliance['appliance_uid'] = NewApplianceID
print(NewApplianceID)
newAppliance['appliance_property_id'] = newPropertyUID
newAppliance['appliance_type'] = appliance
response['Add Appliance'] = db.insert('appliances', newAppliance)
print(response['Add Appliance'])
else:
print("No appliances provided in the form.")
except:
print(f"Add Appliance failed")
response['add_appliance_error'] = f"No Appliance Data Provided"
# Add Property Owner Info
newPropertyOwner = {}
newPropertyOwner['property_id'] = newPropertyUID
newPropertyOwner['property_owner_id'] = payload.pop("property_owner_id")
newPropertyOwner['po_owner_percent'] = payload.pop("po_owner_percent", 1)
print("newPropertyOwner Payload: ", newPropertyOwner)
response['Add Owner'] = db.insert('property_owner', newPropertyOwner)
print("\nNew Property-Owner Relationship Added")
# Add Property Info
print("Add Property Payload: ", payload)
payload["property_listed_date"] = datetime.today().strftime('%m-%d-%Y') if payload.pop('property_listed') == '1' else ''
# payload.pop('property_listed')
payload["property_uid"] = newPropertyUID
response['Add Property'] = db.insert('properties', payload)
response['property_UID'] = newPropertyUID
response['Images Added'] = payload.get('property_images', "None")
print("\nNew Property Added")
return response
def put(self):
print("\nIn Property PUT")
response = {}
payload = request.form.to_dict()
print("Propoerty Update Payload: ", payload)
# Verify uid has been included in the data
if payload.get('property_uid') in {None, '', 'null'}:
print("No property_uid")
raise BadRequest("Request failed, no UID in payload.")
# property_uid = payload.get('property_uid')
key = {'property_uid': payload.pop('property_uid')}
print("Property Key: ", key)
# --------------- PROCESS IMAGES ------------------
processImage(key, payload)
print("Payload after function: ", payload)
# --------------- PROCESS IMAGES ------------------
# Write to Database
with connect() as db:
print("Checking Inputs: ", key, payload)
response['property_info'] = db.update('properties', key, payload)
# print("Response:" , response)
return response
def delete(self):
print("In delete Property")
response = {}
with connect() as db:
data = request.get_json(force=True)
print(data)
property_owner_id = data["property_owner_id"]
property_id = data["property_id"]
# Query
delPropertyQuery = ("""
-- DELETE PROPERTY FROM PROPERTY-OWNER TABLE
DELETE FROM space.property_owner
WHERE property_id = \'""" + property_id + """\'
AND property_owner_id = \'""" + property_owner_id + """\';
""")
# print("Query: ", delPropertyQuery)
response_po = db.delete(delPropertyQuery)
# print("Query out", response_po["code"])
# if response_po["code"] == 200:
# response["Deleted property_uid"] = property_id
# else:
# response["Deleted property_uid"] = "Not successful"
delPropertyQuery = ("""
-- DELETE PROPERTY FROM PROPERTIES TABLE
DELETE FROM space.properties
WHERE property_uid = \'""" + property_id + """\';
""")
# print("Query: ", delPropertyQuery)
response = db.delete(delPropertyQuery)
# print("Query out", response["code"])
if response["code"] == 200:
response["Deleted property_uid"] = property_id
else:
response["Deleted property_uid"] = "Not successful"
if response_po["code"] == 200:
response["Deleted po property_id"] = property_id
else:
response["Deleted po property_id"] = "Not successful"
return response