-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilities.py
161 lines (124 loc) · 6.5 KB
/
utilities.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
from flask import request
from flask_restful import Resource
from data_pm import connect
from werkzeug.exceptions import BadRequest
import json
class Utilities(Resource):
def get(self):
response = {}
where = request.args.to_dict()
with connect() as db:
response = db.select('property_utility', where )
return response
def post(self):
print("In Insert Property Utility")
response = {}
payload = request.form.to_dict()
print(payload)
if payload.get('property_uid') in {None, '', 'null'}:
print("No property_uid")
raise BadRequest("Request failed, no UID in payload.")
key = {'property_uid': payload.pop('property_uid')}
if payload.get('property_utility') in {None, '', 'null'}:
print("No property_utility")
raise BadRequest("No Utilities in payload.")
else:
keyUtility = {'property_utility': payload.pop('property_utility')}
print(type(key), key)
property_uid_value = key['property_uid']
print(property_uid_value)
print(type(keyUtility), keyUtility)
json_string = keyUtility['property_utility']
# Parse the JSON string into a Python dictionary
data = json.loads(json_string)
# Now, you can iterate over the items in the dictionary
for key, value in data.items():
print(f'Key: {key}, Value: {value}')
with connect() as db:
print("in connect")
utilityQuery = ("""
INSERT INTO space.property_utility
SET utility_property_id = \'""" + property_uid_value + """\',
utility_type_id = \'""" + key + """\',
utility_payer_id = \'""" + value + """\';
""")
response["Utility_query"] = db.execute(utilityQuery, [], 'post')
return response
def put(self):
print("\nIn Utility PUT")
response = {}
payload = request.form.to_dict()
# print("Utility 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.")
key = {'property_uid': payload.pop('property_uid')}
# print("Utility Property Key: ", key)
# print("Utility Property Key: ", key['property_uid'])
# Getting data from FrontEnd
if payload.get('property_utility') in {None, '', 'null'}:
print("No property_utility")
raise BadRequest("No Utilities in payload.")
else:
# Get Property Utility Settings
property_utility_dict = json.loads(payload['property_utility'])
# print("Utility Settings from FrontEnd: ", property_utility_dict)
# for property_utility, responsibility in property_utility_dict.items():
# print("Utility Settings: ", property_utility, responsibility)
# Get current property utilities from the database
with connect() as db:
# print("\nin connect")
response = db.execute("""
SELECT *
FROM space.property_utility
-- WHERE utility_property_id = '200-000001'
WHERE utility_property_id= \'""" + key['property_uid'] + """\'
""")
db_utilities = response['result']
# print("Current DB Values: ", db_utilities, type(db_utilities))
# for utility in db_utilities:
# print("DB Utility: ", utility)
# For each utility key, value pair
for property_utility, responsibility in property_utility_dict.items():
# print("\nUtility Setting: ", property_utility, responsibility)
# print("Utility to check: ", property_utility)
for utility in db_utilities:
# print("Current db utility", utility['utility_type_id'])
if utility['utility_type_id'] == property_utility:
# print("Property utility already exists in database")
# break
if utility['utility_payer_id'] == responsibility:
# Utility - Responsibility Match found
# print(f"Match found for utility_type_id {property_utility} with utility_payer_id {responsibility}")
break
else:
# Utility found but responsibility has changed => Do UPDATE
# print("In Update")
with connect() as db:
# print("in connect")
utilityQuery = ("""
UPDATE space.property_utility
SET utility_payer_id = \'""" + responsibility + """\'
WHERE utility_property_id = \'""" + key['property_uid'] + """\'
AND utility_type_id = \'""" + property_utility + """\';
""")
# print(utilityQuery)
response["Utility_query"] = db.execute(utilityQuery, [], 'post')
# print(response)
break
else:
# Utility is NOT in database => Do INSERT
# print("In Insert")
with connect() as db:
# print("in connect")
utilityQuery = ("""
INSERT INTO space.property_utility
SET utility_property_id = \'""" + key['property_uid'] + """\',
utility_type_id = \'""" + property_utility + """\',
utility_payer_id = \'""" + responsibility + """\';
""")
# print(utilityQuery)
response["Utility_query"] = db.execute(utilityQuery, [], 'post')
# print(response)
return response