Total Complexity | 344 |
Total Lines | 1559 |
Duplicated Lines | 24.12 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like core.energystoragepowerstation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import uuid |
||
2 | from datetime import datetime, timedelta |
||
3 | import falcon |
||
4 | import mysql.connector |
||
5 | import simplejson as json |
||
6 | from core.useractivity import user_logger, admin_control, access_control |
||
7 | import config |
||
8 | |||
9 | |||
10 | class EnergyStoragePowerStationCollection: |
||
11 | def __init__(self): |
||
12 | """"Initializes Class""" |
||
13 | pass |
||
14 | |||
15 | @staticmethod |
||
16 | def on_options(req, resp): |
||
17 | _ = req |
||
18 | resp.status = falcon.HTTP_200 |
||
19 | |||
20 | @staticmethod |
||
21 | def on_get(req, resp): |
||
22 | access_control(req) |
||
23 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
24 | cursor = cnx.cursor() |
||
25 | |||
26 | # query contact dict |
||
27 | contact_dict = dict() |
||
28 | query = (" SELECT id, name, uuid " |
||
29 | " FROM tbl_contacts ") |
||
30 | cursor.execute(query) |
||
31 | rows_contacts = cursor.fetchall() |
||
32 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
33 | for row in rows_contacts: |
||
34 | contact_dict[row[0]] = {"id": row[0], |
||
35 | "name": row[1], |
||
36 | "uuid": row[2]} |
||
37 | # query cost center dict |
||
38 | cost_center_dict = dict() |
||
39 | query = (" SELECT id, name, uuid " |
||
40 | " FROM tbl_cost_centers ") |
||
41 | cursor.execute(query) |
||
42 | rows_cost_centers = cursor.fetchall() |
||
43 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
44 | for row in rows_cost_centers: |
||
45 | cost_center_dict[row[0]] = {"id": row[0], |
||
46 | "name": row[1], |
||
47 | "uuid": row[2]} |
||
48 | |||
49 | # query svg dict |
||
50 | svg_dict = dict() |
||
51 | query = (" SELECT id, name, uuid " |
||
52 | " FROM tbl_svgs ") |
||
53 | cursor.execute(query) |
||
54 | rows_svgs = cursor.fetchall() |
||
55 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
56 | for row in rows_svgs: |
||
57 | svg_dict[row[0]] = {"id": row[0], |
||
58 | "name": row[1], |
||
59 | "uuid": row[2]} |
||
60 | |||
61 | # query point dict |
||
62 | point_dict = dict() |
||
63 | query = (" SELECT id, name " |
||
64 | " FROM tbl_points ") |
||
65 | cursor.execute(query) |
||
66 | rows_points = cursor.fetchall() |
||
67 | if rows_points is not None and len(rows_points) > 0: |
||
68 | for row in rows_points: |
||
69 | point_dict[row[0]] = {"id": row[0], |
||
70 | "name": row[1]} |
||
71 | |||
72 | query = (" SELECT id, name, uuid, " |
||
73 | " address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
74 | " rated_capacity, rated_power, contact_id, cost_center_id, " |
||
75 | " svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
76 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
||
77 | " FROM tbl_energy_storage_power_stations " |
||
78 | " ORDER BY id ") |
||
79 | cursor.execute(query) |
||
80 | rows_energy_storage_power_stations = cursor.fetchall() |
||
81 | |||
82 | result = list() |
||
83 | if rows_energy_storage_power_stations is not None and len(rows_energy_storage_power_stations) > 0: |
||
84 | for row in rows_energy_storage_power_stations: |
||
85 | meta_result = {"id": row[0], |
||
86 | "name": row[1], |
||
87 | "uuid": row[2], |
||
88 | "address": row[3], |
||
89 | "latitude": row[4], |
||
90 | "longitude": row[5], |
||
91 | "latitude_point": point_dict.get(row[6], None), |
||
92 | "longitude_point": point_dict.get(row[7], None), |
||
93 | "rated_capacity": row[8], |
||
94 | "rated_power": row[9], |
||
95 | "contact": contact_dict.get(row[10], None), |
||
96 | "cost_center": cost_center_dict.get(row[11], None), |
||
97 | "svg": svg_dict.get(row[12], None), |
||
98 | "svg2": svg_dict.get(row[13], None), |
||
99 | "svg3": svg_dict.get(row[14], None), |
||
100 | "svg4": svg_dict.get(row[15], None), |
||
101 | "svg5": svg_dict.get(row[16], None), |
||
102 | "is_cost_data_displayed": bool(row[17]), |
||
103 | "phase_of_lifecycle": row[18], |
||
104 | "commissioning_date": row[19].isoformat() if row[19] else None, |
||
105 | "description": row[20], |
||
106 | "qrcode": 'energystoragepowerstation:' + row[2]} |
||
107 | result.append(meta_result) |
||
108 | |||
109 | cursor.close() |
||
110 | cnx.close() |
||
111 | resp.text = json.dumps(result) |
||
112 | |||
113 | @staticmethod |
||
114 | @user_logger |
||
115 | def on_post(req, resp): |
||
116 | """Handles POST requests""" |
||
117 | admin_control(req) |
||
118 | try: |
||
119 | raw_json = req.stream.read().decode('utf-8') |
||
120 | except Exception as ex: |
||
121 | print(str(ex)) |
||
122 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
123 | title='API.BAD_REQUEST', |
||
124 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
125 | |||
126 | new_values = json.loads(raw_json) |
||
127 | |||
128 | if 'name' not in new_values['data'].keys() or \ |
||
129 | not isinstance(new_values['data']['name'], str) or \ |
||
130 | len(str.strip(new_values['data']['name'])) == 0: |
||
131 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
132 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
||
133 | name = str.strip(new_values['data']['name']) |
||
134 | |||
135 | if 'address' not in new_values['data'].keys() or \ |
||
136 | not isinstance(new_values['data']['address'], str) or \ |
||
137 | len(str.strip(new_values['data']['address'])) == 0: |
||
138 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
139 | description='API.INVALID_ADDRESS_VALUE') |
||
140 | address = str.strip(new_values['data']['address']) |
||
141 | |||
142 | if 'latitude' not in new_values['data'].keys() or \ |
||
143 | not (isinstance(new_values['data']['latitude'], float) or |
||
144 | isinstance(new_values['data']['latitude'], int)) or \ |
||
145 | new_values['data']['latitude'] < -90.0 or \ |
||
146 | new_values['data']['latitude'] > 90.0: |
||
147 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
148 | description='API.INVALID_LATITUDE_VALUE') |
||
149 | latitude = new_values['data']['latitude'] |
||
150 | |||
151 | if 'longitude' not in new_values['data'].keys() or \ |
||
152 | not (isinstance(new_values['data']['longitude'], float) or |
||
153 | isinstance(new_values['data']['longitude'], int)) or \ |
||
154 | new_values['data']['longitude'] < -180.0 or \ |
||
155 | new_values['data']['longitude'] > 180.0: |
||
156 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
157 | description='API.INVALID_LONGITUDE_VALUE') |
||
158 | longitude = new_values['data']['longitude'] |
||
159 | |||
160 | if 'latitude_point_id' in new_values['data'].keys() and \ |
||
161 | isinstance(new_values['data']['latitude_point_id'], int) and \ |
||
162 | new_values['data']['latitude_point_id'] > 0: |
||
163 | latitude_point_id = new_values['data']['latitude_point_id'] |
||
164 | else: |
||
165 | latitude_point_id = None |
||
166 | |||
167 | if 'longitude_point_id' in new_values['data'].keys() and \ |
||
168 | isinstance(new_values['data']['longitude_point_id'], int) and \ |
||
169 | new_values['data']['longitude_point_id'] > 0: |
||
170 | longitude_point_id = new_values['data']['longitude_point_id'] |
||
171 | else: |
||
172 | longitude_point_id = None |
||
173 | |||
174 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
175 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
176 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
177 | new_values['data']['rated_capacity'] < 0.0: |
||
178 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
179 | description='API.INVALID_RATED_CAPACITY') |
||
180 | rated_capacity = new_values['data']['rated_capacity'] |
||
181 | |||
182 | if 'rated_power' not in new_values['data'].keys() or \ |
||
183 | not (isinstance(new_values['data']['rated_power'], float) or |
||
184 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
185 | new_values['data']['rated_power'] < 0.0: |
||
186 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
187 | description='API.INVALID_RATED_POWER') |
||
188 | rated_power = new_values['data']['rated_power'] |
||
189 | |||
190 | if 'contact_id' not in new_values['data'].keys() or \ |
||
191 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
192 | new_values['data']['contact_id'] <= 0: |
||
193 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
194 | description='API.INVALID_CONTACT_ID') |
||
195 | contact_id = new_values['data']['contact_id'] |
||
196 | |||
197 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
198 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
199 | new_values['data']['cost_center_id'] <= 0: |
||
200 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
201 | description='API.INVALID_COST_CENTER_ID') |
||
202 | cost_center_id = new_values['data']['cost_center_id'] |
||
203 | |||
204 | if 'svg_id' not in new_values['data'].keys() or \ |
||
205 | not isinstance(new_values['data']['svg_id'], int) or \ |
||
206 | new_values['data']['svg_id'] <= 0: |
||
207 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
208 | description='API.INVALID_SVG_ID') |
||
209 | svg_id = new_values['data']['svg_id'] |
||
210 | |||
211 | if 'svg2_id' in new_values['data'].keys() and \ |
||
212 | isinstance(new_values['data']['svg2_id'], int) and \ |
||
213 | new_values['data']['svg2_id'] > 0: |
||
214 | svg2_id = new_values['data']['svg2_id'] |
||
215 | else: |
||
216 | svg2_id = None |
||
217 | |||
218 | if 'svg3_id' in new_values['data'].keys() and \ |
||
219 | isinstance(new_values['data']['svg3_id'], int) and \ |
||
220 | new_values['data']['svg3_id'] > 0: |
||
221 | svg3_id = new_values['data']['svg3_id'] |
||
222 | else: |
||
223 | svg3_id = None |
||
224 | |||
225 | if 'svg4_id' in new_values['data'].keys() and \ |
||
226 | isinstance(new_values['data']['svg4_id'], int) and \ |
||
227 | new_values['data']['svg4_id'] > 0: |
||
228 | svg4_id = new_values['data']['svg4_id'] |
||
229 | else: |
||
230 | svg4_id = None |
||
231 | |||
232 | if 'svg5_id' in new_values['data'].keys() and \ |
||
233 | isinstance(new_values['data']['svg5_id'], int) and \ |
||
234 | new_values['data']['svg5_id'] > 0: |
||
235 | svg5_id = new_values['data']['svg5_id'] |
||
236 | else: |
||
237 | svg5_id = None |
||
238 | |||
239 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
||
240 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
||
241 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
242 | description='API.INVALID_IS_COST_DATA_DISPLAYED') |
||
243 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
||
244 | |||
245 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
||
246 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
||
247 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
||
248 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
249 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
||
250 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
||
251 | |||
252 | if 'commissioning_date' not in new_values['data'].keys() or \ |
||
253 | not isinstance(new_values['data']['commissioning_date'], str) or \ |
||
254 | len(str.strip(new_values['data']['commissioning_date'])) == 0: |
||
255 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
256 | description='API.INVALID_COMMISSIONING_DATE') |
||
257 | commissioning_date = datetime.strptime(new_values['data']['commissioning_date'], '%Y-%m-%d') |
||
258 | |||
259 | if 'description' in new_values['data'].keys() and \ |
||
260 | new_values['data']['description'] is not None and \ |
||
261 | len(str(new_values['data']['description'])) > 0: |
||
262 | description = str.strip(new_values['data']['description']) |
||
263 | else: |
||
264 | description = None |
||
265 | |||
266 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
267 | cursor = cnx.cursor() |
||
268 | |||
269 | cursor.execute(" SELECT name " |
||
270 | " FROM tbl_energy_storage_power_stations " |
||
271 | " WHERE name = %s ", (name,)) |
||
272 | if cursor.fetchone() is not None: |
||
273 | cursor.close() |
||
274 | cnx.close() |
||
275 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
276 | description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
||
277 | |||
278 | cursor.execute(" SELECT name " |
||
279 | " FROM tbl_contacts " |
||
280 | " WHERE id = %s ", |
||
281 | (contact_id,)) |
||
282 | row = cursor.fetchone() |
||
283 | if row is None: |
||
284 | cursor.close() |
||
285 | cnx.close() |
||
286 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
287 | description='API.CONTACT_NOT_FOUND') |
||
288 | |||
289 | cursor.execute(" SELECT name " |
||
290 | " FROM tbl_cost_centers " |
||
291 | " WHERE id = %s ", |
||
292 | (cost_center_id,)) |
||
293 | row = cursor.fetchone() |
||
294 | if row is None: |
||
295 | cursor.close() |
||
296 | cnx.close() |
||
297 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
298 | description='API.COST_CENTER_NOT_FOUND') |
||
299 | |||
300 | cursor.execute(" SELECT name " |
||
301 | " FROM tbl_svgs " |
||
302 | " WHERE id = %s ", |
||
303 | (svg_id,)) |
||
304 | row = cursor.fetchone() |
||
305 | if row is None: |
||
306 | cursor.close() |
||
307 | cnx.close() |
||
308 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
309 | description='API.SVG_NOT_FOUND') |
||
310 | |||
311 | add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
||
312 | " (name, uuid, address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
313 | " rated_capacity, rated_power, contact_id, cost_center_id, " |
||
314 | " svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
315 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description) " |
||
316 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
317 | cursor.execute(add_values, (name, |
||
318 | str(uuid.uuid4()), |
||
319 | address, |
||
320 | latitude, |
||
321 | longitude, |
||
322 | latitude_point_id, |
||
323 | longitude_point_id, |
||
324 | rated_capacity, |
||
325 | rated_power, |
||
326 | contact_id, |
||
327 | cost_center_id, |
||
328 | svg_id, |
||
329 | svg2_id, |
||
330 | svg3_id, |
||
331 | svg4_id, |
||
332 | svg5_id, |
||
333 | is_cost_data_displayed, |
||
334 | phase_of_lifecycle, |
||
335 | commissioning_date, |
||
336 | description)) |
||
337 | new_id = cursor.lastrowid |
||
338 | cnx.commit() |
||
339 | cursor.close() |
||
340 | cnx.close() |
||
341 | |||
342 | resp.status = falcon.HTTP_201 |
||
343 | resp.location = '/energystoragepowerstations/' + str(new_id) |
||
344 | |||
345 | |||
346 | class EnergyStoragePowerStationItem: |
||
347 | def __init__(self): |
||
348 | """"Initializes Class""" |
||
349 | pass |
||
350 | |||
351 | @staticmethod |
||
352 | def on_options(req, resp, id_): |
||
353 | _ = req |
||
354 | resp.status = falcon.HTTP_200 |
||
355 | _ = id_ |
||
356 | |||
357 | @staticmethod |
||
358 | def on_get(req, resp, id_): |
||
359 | access_control(req) |
||
360 | if not id_.isdigit() or int(id_) <= 0: |
||
361 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
362 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
363 | |||
364 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
365 | cursor = cnx.cursor() |
||
366 | |||
367 | contact_dict = dict() |
||
368 | query = (" SELECT id, name, uuid " |
||
369 | " FROM tbl_contacts ") |
||
370 | cursor.execute(query) |
||
371 | rows_contacts = cursor.fetchall() |
||
372 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
373 | for row in rows_contacts: |
||
374 | contact_dict[row[0]] = {"id": row[0], |
||
375 | "name": row[1], |
||
376 | "uuid": row[2]} |
||
377 | |||
378 | cost_center_dict = dict() |
||
379 | query = (" SELECT id, name, uuid " |
||
380 | " FROM tbl_cost_centers ") |
||
381 | cursor.execute(query) |
||
382 | rows_cost_centers = cursor.fetchall() |
||
383 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
384 | for row in rows_cost_centers: |
||
385 | cost_center_dict[row[0]] = {"id": row[0], |
||
386 | "name": row[1], |
||
387 | "uuid": row[2]} |
||
388 | |||
389 | svg_dict = dict() |
||
390 | query = (" SELECT id, name, uuid " |
||
391 | " FROM tbl_svgs ") |
||
392 | cursor.execute(query) |
||
393 | rows_svgs = cursor.fetchall() |
||
394 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
395 | for row in rows_svgs: |
||
396 | svg_dict[row[0]] = {"id": row[0], |
||
397 | "name": row[1], |
||
398 | "uuid": row[2]} |
||
399 | |||
400 | # query point dict |
||
401 | point_dict = dict() |
||
402 | query = (" SELECT id, name " |
||
403 | " FROM tbl_points ") |
||
404 | cursor.execute(query) |
||
405 | rows_points = cursor.fetchall() |
||
406 | if rows_points is not None and len(rows_points) > 0: |
||
407 | for row in rows_points: |
||
408 | point_dict[row[0]] = {"id": row[0], |
||
409 | "name": row[1]} |
||
410 | |||
411 | query = (" SELECT id, name, uuid, " |
||
412 | " address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
413 | " rated_capacity, rated_power, contact_id, cost_center_id, " |
||
414 | " svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
415 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
||
416 | " FROM tbl_energy_storage_power_stations " |
||
417 | " WHERE id = %s ") |
||
418 | cursor.execute(query, (id_,)) |
||
419 | row = cursor.fetchone() |
||
420 | cursor.close() |
||
421 | cnx.close() |
||
422 | |||
423 | if row is None: |
||
424 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
425 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
426 | else: |
||
427 | meta_result = {"id": row[0], |
||
428 | "name": row[1], |
||
429 | "uuid": row[2], |
||
430 | "address": row[3], |
||
431 | "latitude": row[4], |
||
432 | "longitude": row[5], |
||
433 | "latitude_point": point_dict.get(row[6], None), |
||
434 | "longitude_point": point_dict.get(row[7], None), |
||
435 | "rated_capacity": row[8], |
||
436 | "rated_power": row[9], |
||
437 | "contact": contact_dict.get(row[10], None), |
||
438 | "cost_center": cost_center_dict.get(row[11], None), |
||
439 | "svg": svg_dict.get(row[12], None), |
||
440 | "svg2": svg_dict.get(row[13], None), |
||
441 | "svg3": svg_dict.get(row[14], None), |
||
442 | "svg4": svg_dict.get(row[15], None), |
||
443 | "svg5": svg_dict.get(row[16], None), |
||
444 | "is_cost_data_displayed": bool(row[17]), |
||
445 | "phase_of_lifecycle": row[18], |
||
446 | "commissioning_date": row[19].isoformat() if row[19] else None, |
||
447 | "description": row[20], |
||
448 | "qrcode": 'energystoragepowerstation:' + row[2]} |
||
449 | |||
450 | resp.text = json.dumps(meta_result) |
||
451 | |||
452 | View Code Duplication | @staticmethod |
|
|
|||
453 | @user_logger |
||
454 | def on_delete(req, resp, id_): |
||
455 | admin_control(req) |
||
456 | if not id_.isdigit() or int(id_) <= 0: |
||
457 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
458 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
459 | |||
460 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
461 | cursor = cnx.cursor() |
||
462 | |||
463 | cursor.execute(" SELECT name " |
||
464 | " FROM tbl_energy_storage_power_stations " |
||
465 | " WHERE id = %s ", (id_,)) |
||
466 | if cursor.fetchone() is None: |
||
467 | cursor.close() |
||
468 | cnx.close() |
||
469 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
470 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
471 | |||
472 | # check relation with spaces |
||
473 | cursor.execute(" SELECT id " |
||
474 | " FROM tbl_spaces_energy_storage_power_stations " |
||
475 | " WHERE energy_storage_power_station_id = %s ", (id_,)) |
||
476 | rows_spaces = cursor.fetchall() |
||
477 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
478 | cursor.close() |
||
479 | cnx.close() |
||
480 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
481 | title='API.BAD_REQUEST', |
||
482 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
483 | |||
484 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers " |
||
485 | " WHERE energy_storage_power_station_id = %s ", (id_,)) |
||
486 | |||
487 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations " |
||
488 | " WHERE id = %s ", (id_,)) |
||
489 | cnx.commit() |
||
490 | |||
491 | cursor.close() |
||
492 | cnx.close() |
||
493 | |||
494 | resp.status = falcon.HTTP_204 |
||
495 | |||
496 | @staticmethod |
||
497 | @user_logger |
||
498 | def on_put(req, resp, id_): |
||
499 | """Handles PUT requests""" |
||
500 | admin_control(req) |
||
501 | try: |
||
502 | raw_json = req.stream.read().decode('utf-8') |
||
503 | except Exception as ex: |
||
504 | print(str(ex)) |
||
505 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
506 | title='API.BAD_REQUEST', |
||
507 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
508 | |||
509 | if not id_.isdigit() or int(id_) <= 0: |
||
510 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
511 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
512 | |||
513 | new_values = json.loads(raw_json) |
||
514 | |||
515 | if 'name' not in new_values['data'].keys() or \ |
||
516 | not isinstance(new_values['data']['name'], str) or \ |
||
517 | len(str.strip(new_values['data']['name'])) == 0: |
||
518 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
519 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
||
520 | name = str.strip(new_values['data']['name']) |
||
521 | |||
522 | if 'address' not in new_values['data'].keys() or \ |
||
523 | not isinstance(new_values['data']['address'], str) or \ |
||
524 | len(str.strip(new_values['data']['address'])) == 0: |
||
525 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
526 | description='API.INVALID_ADDRESS_VALUE') |
||
527 | address = str.strip(new_values['data']['address']) |
||
528 | |||
529 | if 'latitude' not in new_values['data'].keys() or \ |
||
530 | not (isinstance(new_values['data']['latitude'], float) or |
||
531 | isinstance(new_values['data']['latitude'], int)) or \ |
||
532 | new_values['data']['latitude'] < -90.0 or \ |
||
533 | new_values['data']['latitude'] > 90.0: |
||
534 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
535 | description='API.INVALID_LATITUDE_VALUE') |
||
536 | latitude = new_values['data']['latitude'] |
||
537 | |||
538 | if 'longitude' not in new_values['data'].keys() or \ |
||
539 | not (isinstance(new_values['data']['longitude'], float) or |
||
540 | isinstance(new_values['data']['longitude'], int)) or \ |
||
541 | new_values['data']['longitude'] < -180.0 or \ |
||
542 | new_values['data']['longitude'] > 180.0: |
||
543 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
544 | description='API.INVALID_LONGITUDE_VALUE') |
||
545 | longitude = new_values['data']['longitude'] |
||
546 | |||
547 | if 'latitude_point_id' in new_values['data'].keys() and \ |
||
548 | isinstance(new_values['data']['latitude_point_id'], int) and \ |
||
549 | new_values['data']['latitude_point_id'] > 0: |
||
550 | latitude_point_id = new_values['data']['latitude_point_id'] |
||
551 | else: |
||
552 | latitude_point_id = None |
||
553 | |||
554 | if 'longitude_point_id' in new_values['data'].keys() and \ |
||
555 | isinstance(new_values['data']['longitude_point_id'], int) and \ |
||
556 | new_values['data']['longitude_point_id'] > 0: |
||
557 | longitude_point_id = new_values['data']['longitude_point_id'] |
||
558 | else: |
||
559 | longitude_point_id = None |
||
560 | |||
561 | if 'rated_capacity' not in new_values['data'].keys() or \ |
||
562 | not (isinstance(new_values['data']['rated_capacity'], float) or |
||
563 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
||
564 | new_values['data']['rated_capacity'] < 0.0: |
||
565 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
566 | description='API.INVALID_RATED_CAPACITY') |
||
567 | rated_capacity = new_values['data']['rated_capacity'] |
||
568 | |||
569 | if 'rated_power' not in new_values['data'].keys() or \ |
||
570 | not (isinstance(new_values['data']['rated_power'], float) or |
||
571 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
572 | new_values['data']['rated_power'] < 0.0: |
||
573 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
574 | description='API.INVALID_RATED_POWER') |
||
575 | rated_power = new_values['data']['rated_power'] |
||
576 | |||
577 | if 'contact_id' not in new_values['data'].keys() or \ |
||
578 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
579 | new_values['data']['contact_id'] <= 0: |
||
580 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
581 | description='API.INVALID_CONTACT_ID') |
||
582 | contact_id = new_values['data']['contact_id'] |
||
583 | |||
584 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
585 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
586 | new_values['data']['cost_center_id'] <= 0: |
||
587 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
588 | description='API.INVALID_COST_CENTER_ID') |
||
589 | cost_center_id = new_values['data']['cost_center_id'] |
||
590 | |||
591 | if 'svg_id' not in new_values['data'].keys() or \ |
||
592 | not isinstance(new_values['data']['svg_id'], int) or \ |
||
593 | new_values['data']['svg_id'] <= 0: |
||
594 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
595 | description='API.INVALID_SVG_ID') |
||
596 | svg_id = new_values['data']['svg_id'] |
||
597 | |||
598 | if 'svg2_id' in new_values['data'].keys() and \ |
||
599 | isinstance(new_values['data']['svg2_id'], int) and \ |
||
600 | new_values['data']['svg2_id'] > 0: |
||
601 | svg2_id = new_values['data']['svg2_id'] |
||
602 | else: |
||
603 | svg2_id = None |
||
604 | |||
605 | if 'svg3_id' in new_values['data'].keys() and \ |
||
606 | isinstance(new_values['data']['svg3_id'], int) and \ |
||
607 | new_values['data']['svg3_id'] > 0: |
||
608 | svg3_id = new_values['data']['svg3_id'] |
||
609 | else: |
||
610 | svg3_id = None |
||
611 | |||
612 | if 'svg4_id' in new_values['data'].keys() and \ |
||
613 | isinstance(new_values['data']['svg4_id'], int) and \ |
||
614 | new_values['data']['svg4_id'] > 0: |
||
615 | svg4_id = new_values['data']['svg4_id'] |
||
616 | else: |
||
617 | svg4_id = None |
||
618 | |||
619 | if 'svg5_id' in new_values['data'].keys() and \ |
||
620 | isinstance(new_values['data']['svg5_id'], int) and \ |
||
621 | new_values['data']['svg5_id'] > 0: |
||
622 | svg5_id = new_values['data']['svg5_id'] |
||
623 | else: |
||
624 | svg5_id = None |
||
625 | |||
626 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
||
627 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
||
628 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
629 | description='API.INVALID_IS_COST_DATA_DISPLAYED_VALUE') |
||
630 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
||
631 | |||
632 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
||
633 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
||
634 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
||
635 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
636 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
||
637 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
||
638 | |||
639 | if 'commissioning_date' not in new_values['data'].keys() or \ |
||
640 | not isinstance(new_values['data']['commissioning_date'], str) or \ |
||
641 | len(str.strip(new_values['data']['commissioning_date'])) == 0: |
||
642 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
643 | description='API.INVALID_COMMISSIONING_DATE') |
||
644 | commissioning_date = datetime.strptime(new_values['data']['commissioning_date'], '%Y-%m-%d') |
||
645 | |||
646 | if 'description' in new_values['data'].keys() and \ |
||
647 | new_values['data']['description'] is not None and \ |
||
648 | len(str(new_values['data']['description'])) > 0: |
||
649 | description = str.strip(new_values['data']['description']) |
||
650 | else: |
||
651 | description = None |
||
652 | |||
653 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
654 | cursor = cnx.cursor() |
||
655 | |||
656 | cursor.execute(" SELECT name " |
||
657 | " FROM tbl_energy_storage_power_stations " |
||
658 | " WHERE id = %s ", (id_,)) |
||
659 | if cursor.fetchone() is None: |
||
660 | cursor.close() |
||
661 | cnx.close() |
||
662 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
663 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
664 | |||
665 | cursor.execute(" SELECT name " |
||
666 | " FROM tbl_energy_storage_power_stations " |
||
667 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
668 | if cursor.fetchone() is not None: |
||
669 | cursor.close() |
||
670 | cnx.close() |
||
671 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
672 | description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
||
673 | |||
674 | cursor.execute(" SELECT name " |
||
675 | " FROM tbl_contacts " |
||
676 | " WHERE id = %s ", |
||
677 | (new_values['data']['contact_id'],)) |
||
678 | row = cursor.fetchone() |
||
679 | if row is None: |
||
680 | cursor.close() |
||
681 | cnx.close() |
||
682 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
683 | description='API.CONTACT_NOT_FOUND') |
||
684 | |||
685 | cursor.execute(" SELECT name " |
||
686 | " FROM tbl_cost_centers " |
||
687 | " WHERE id = %s ", |
||
688 | (new_values['data']['cost_center_id'],)) |
||
689 | row = cursor.fetchone() |
||
690 | if row is None: |
||
691 | cursor.close() |
||
692 | cnx.close() |
||
693 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
694 | description='API.COST_CENTER_NOT_FOUND') |
||
695 | |||
696 | cursor.execute(" SELECT name " |
||
697 | " FROM tbl_svgs " |
||
698 | " WHERE id = %s ", |
||
699 | (new_values['data']['svg_id'],)) |
||
700 | row = cursor.fetchone() |
||
701 | if row is None: |
||
702 | cursor.close() |
||
703 | cnx.close() |
||
704 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
705 | description='API.SVG_NOT_FOUND') |
||
706 | |||
707 | update_row = (" UPDATE tbl_energy_storage_power_stations " |
||
708 | " SET name = %s, address = %s, latitude = %s, longitude = %s, " |
||
709 | " latitude_point_id = %s, longitude_point_id = %s, " |
||
710 | " rated_capacity = %s, rated_power = %s, " |
||
711 | " contact_id = %s, cost_center_id = %s, " |
||
712 | " svg_id = %s, svg2_id = %s, svg3_id = %s, svg4_id = %s, svg5_id = %s, " |
||
713 | " is_cost_data_displayed = %s, phase_of_lifecycle = %s, commissioning_date = %s, " |
||
714 | " description = %s " |
||
715 | " WHERE id = %s ") |
||
716 | cursor.execute(update_row, (name, |
||
717 | address, |
||
718 | latitude, |
||
719 | longitude, |
||
720 | latitude_point_id, |
||
721 | longitude_point_id, |
||
722 | rated_capacity, |
||
723 | rated_power, |
||
724 | contact_id, |
||
725 | cost_center_id, |
||
726 | svg_id, |
||
727 | svg2_id, |
||
728 | svg3_id, |
||
729 | svg4_id, |
||
730 | svg5_id, |
||
731 | is_cost_data_displayed, |
||
732 | phase_of_lifecycle, |
||
733 | commissioning_date, |
||
734 | description, |
||
735 | id_)) |
||
736 | cnx.commit() |
||
737 | |||
738 | cursor.close() |
||
739 | cnx.close() |
||
740 | |||
741 | resp.status = falcon.HTTP_200 |
||
742 | |||
743 | |||
744 | View Code Duplication | class EnergyStoragePowerStationContainerCollection: |
|
745 | def __init__(self): |
||
746 | """Initializes Class""" |
||
747 | pass |
||
748 | |||
749 | @staticmethod |
||
750 | def on_options(req, resp, id_): |
||
751 | _ = req |
||
752 | resp.status = falcon.HTTP_200 |
||
753 | _ = id_ |
||
754 | |||
755 | @staticmethod |
||
756 | def on_get(req, resp, id_): |
||
757 | access_control(req) |
||
758 | if not id_.isdigit() or int(id_) <= 0: |
||
759 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
760 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
761 | |||
762 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
763 | cursor = cnx.cursor() |
||
764 | |||
765 | cursor.execute(" SELECT name " |
||
766 | " FROM tbl_energy_storage_power_stations " |
||
767 | " WHERE id = %s ", (id_,)) |
||
768 | if cursor.fetchone() is None: |
||
769 | cursor.close() |
||
770 | cnx.close() |
||
771 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
772 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
773 | |||
774 | query = (" SELECT s.id, s.name, s.uuid " |
||
775 | " FROM tbl_energy_storage_power_stations e, " |
||
776 | " tbl_energy_storage_power_stations_containers es, tbl_energy_storage_containers s " |
||
777 | " WHERE es.energy_storage_power_station_id = e.id " |
||
778 | " AND s.id = es.energy_storage_container_id " |
||
779 | " AND e.id = %s " |
||
780 | " ORDER BY s.id ") |
||
781 | cursor.execute(query, (id_,)) |
||
782 | rows = cursor.fetchall() |
||
783 | |||
784 | result = list() |
||
785 | if rows is not None and len(rows) > 0: |
||
786 | for row in rows: |
||
787 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
788 | result.append(meta_result) |
||
789 | |||
790 | resp.text = json.dumps(result) |
||
791 | |||
792 | @staticmethod |
||
793 | @user_logger |
||
794 | def on_post(req, resp, id_): |
||
795 | """Handles POST requests""" |
||
796 | admin_control(req) |
||
797 | try: |
||
798 | raw_json = req.stream.read().decode('utf-8') |
||
799 | except Exception as ex: |
||
800 | print(str(ex)) |
||
801 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
802 | title='API.BAD_REQUEST', |
||
803 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
804 | |||
805 | if not id_.isdigit() or int(id_) <= 0: |
||
806 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
807 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
808 | |||
809 | new_values = json.loads(raw_json) |
||
810 | |||
811 | if 'energy_storage_container_id' not in new_values['data'].keys() or \ |
||
812 | not isinstance(new_values['data']['energy_storage_container_id'], int) or \ |
||
813 | new_values['data']['energy_storage_container_id'] <= 0: |
||
814 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
815 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
816 | energy_storage_container_id = new_values['data']['energy_storage_container_id'] |
||
817 | |||
818 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
819 | cursor = cnx.cursor() |
||
820 | |||
821 | cursor.execute(" SELECT name " |
||
822 | " FROM tbl_energy_storage_power_stations " |
||
823 | " WHERE id = %s ", (id_,)) |
||
824 | if cursor.fetchone() is None: |
||
825 | cursor.close() |
||
826 | cnx.close() |
||
827 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
828 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
829 | |||
830 | cursor.execute(" SELECT name " |
||
831 | " FROM tbl_energy_storage_containers " |
||
832 | " WHERE id = %s ", (energy_storage_container_id,)) |
||
833 | if cursor.fetchone() is None: |
||
834 | cursor.close() |
||
835 | cnx.close() |
||
836 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
837 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
838 | |||
839 | query = (" SELECT id " |
||
840 | " FROM tbl_energy_storage_power_stations_containers " |
||
841 | " WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s") |
||
842 | cursor.execute(query, (id_, energy_storage_container_id,)) |
||
843 | if cursor.fetchone() is not None: |
||
844 | cursor.close() |
||
845 | cnx.close() |
||
846 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
847 | description='API.ENERGY_STORAGE_CONTAINER_SENSOR_RELATION_EXISTS') |
||
848 | |||
849 | add_row = (" INSERT INTO tbl_energy_storage_power_stations_containers " |
||
850 | " (energy_storage_power_station_id, energy_storage_container_id) " |
||
851 | " VALUES (%s, %s) ") |
||
852 | cursor.execute(add_row, (id_, energy_storage_container_id,)) |
||
853 | cnx.commit() |
||
854 | cursor.close() |
||
855 | cnx.close() |
||
856 | |||
857 | resp.status = falcon.HTTP_201 |
||
858 | resp.location = '/energystoragepowerstationss/' + str(id_) + '/containers/' + str(energy_storage_container_id) |
||
859 | |||
860 | |||
861 | class EnergyStoragePowerStationContainerItem: |
||
862 | def __init__(self): |
||
863 | """Initializes Class""" |
||
864 | pass |
||
865 | |||
866 | @staticmethod |
||
867 | def on_options(req, resp, id_, sid): |
||
868 | _ = req |
||
869 | resp.status = falcon.HTTP_200 |
||
870 | _ = id_ |
||
871 | |||
872 | @staticmethod |
||
873 | @user_logger |
||
874 | def on_delete(req, resp, id_, sid): |
||
875 | admin_control(req) |
||
876 | if not id_.isdigit() or int(id_) <= 0: |
||
877 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
878 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
879 | |||
880 | if not sid.isdigit() or int(sid) <= 0: |
||
881 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
882 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID') |
||
883 | |||
884 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
885 | cursor = cnx.cursor() |
||
886 | |||
887 | cursor.execute(" SELECT name " |
||
888 | " FROM tbl_energy_storage_power_stations " |
||
889 | " WHERE id = %s ", (id_,)) |
||
890 | if cursor.fetchone() is None: |
||
891 | cursor.close() |
||
892 | cnx.close() |
||
893 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
894 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
895 | |||
896 | cursor.execute(" SELECT name " |
||
897 | " FROM tbl_energy_storage_containers " |
||
898 | " WHERE id = %s ", (sid,)) |
||
899 | if cursor.fetchone() is None: |
||
900 | cursor.close() |
||
901 | cnx.close() |
||
902 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
903 | description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND') |
||
904 | |||
905 | cursor.execute(" SELECT id " |
||
906 | " FROM tbl_energy_storage_power_stations_containers " |
||
907 | " WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s ", (id_, sid)) |
||
908 | if cursor.fetchone() is None: |
||
909 | cursor.close() |
||
910 | cnx.close() |
||
911 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
912 | description='API.ENERGY_STORAGE_POWER_STATION_CONTAINER_RELATION_NOT_FOUND') |
||
913 | |||
914 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers " |
||
915 | " WHERE energy_storage_power_station_id = %s AND energy_storage_container_id = %s ", (id_, sid)) |
||
916 | cnx.commit() |
||
917 | |||
918 | cursor.close() |
||
919 | cnx.close() |
||
920 | |||
921 | resp.status = falcon.HTTP_204 |
||
922 | |||
923 | |||
924 | View Code Duplication | class EnergyStoragePowerStationUserCollection: |
|
925 | def __init__(self): |
||
926 | """Initializes Class""" |
||
927 | pass |
||
928 | |||
929 | @staticmethod |
||
930 | def on_options(req, resp, id_): |
||
931 | _ = req |
||
932 | resp.status = falcon.HTTP_200 |
||
933 | _ = id_ |
||
934 | |||
935 | @staticmethod |
||
936 | def on_get(req, resp, id_): |
||
937 | access_control(req) |
||
938 | if not id_.isdigit() or int(id_) <= 0: |
||
939 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
940 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
941 | |||
942 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
943 | cursor = cnx.cursor() |
||
944 | cursor.execute(" SELECT name " |
||
945 | " FROM tbl_energy_storage_power_stations " |
||
946 | " WHERE id = %s ", (id_,)) |
||
947 | if cursor.fetchone() is None: |
||
948 | cursor.close() |
||
949 | cnx.close() |
||
950 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
951 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
952 | |||
953 | query = (" SELECT u.id, u.name, u.uuid " |
||
954 | " FROM tbl_energy_storage_power_stations m, tbl_energy_storage_power_stations_users mu, " |
||
955 | + config.myems_user_db['database'] + ".tbl_users u " |
||
956 | " WHERE mu.energy_storage_power_station_id = m.id AND u.id = mu.user_id AND m.id = %s " |
||
957 | " ORDER BY u.id ") |
||
958 | cursor.execute(query, (id_,)) |
||
959 | rows = cursor.fetchall() |
||
960 | result = list() |
||
961 | if rows is not None and len(rows) > 0: |
||
962 | for row in rows: |
||
963 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
||
964 | result.append(meta_result) |
||
965 | |||
966 | cursor.close() |
||
967 | cnx.close() |
||
968 | resp.text = json.dumps(result) |
||
969 | |||
970 | @staticmethod |
||
971 | @user_logger |
||
972 | def on_post(req, resp, id_): |
||
973 | """Handles POST requests""" |
||
974 | admin_control(req) |
||
975 | try: |
||
976 | raw_json = req.stream.read().decode('utf-8') |
||
977 | except Exception as ex: |
||
978 | print(str(ex)) |
||
979 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
980 | title='API.BAD_REQUEST', |
||
981 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
982 | |||
983 | if not id_.isdigit() or int(id_) <= 0: |
||
984 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
985 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
986 | |||
987 | new_values = json.loads(raw_json) |
||
988 | if 'user_id' not in new_values['data'].keys() or \ |
||
989 | not isinstance(new_values['data']['user_id'], int) or \ |
||
990 | new_values['data']['user_id'] <= 0: |
||
991 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
992 | description='API.INVALID_USER_ID') |
||
993 | user_id = new_values['data']['user_id'] |
||
994 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
995 | cursor = cnx.cursor() |
||
996 | cursor.execute(" SELECT name " |
||
997 | " FROM tbl_energy_storage_power_stations " |
||
998 | " WHERE id = %s ", (id_,)) |
||
999 | if cursor.fetchone() is None: |
||
1000 | cursor.close() |
||
1001 | cnx.close() |
||
1002 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1003 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
1004 | |||
1005 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
||
1006 | cursor_user = cnx_user.cursor() |
||
1007 | cursor_user.execute(" SELECT name" |
||
1008 | " FROM tbl_users " |
||
1009 | " WHERE id = %s ", (user_id,)) |
||
1010 | if cursor_user.fetchone() is None: |
||
1011 | cursor_user.close() |
||
1012 | cnx_user.close() |
||
1013 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1014 | description='API.USER_NOT_FOUND') |
||
1015 | query = (" SELECT id " |
||
1016 | " FROM tbl_energy_storage_power_stations_users " |
||
1017 | " WHERE energy_storage_power_station_id = %s AND user_id = %s") |
||
1018 | cursor.execute(query, (id_, user_id,)) |
||
1019 | if cursor.fetchone() is not None: |
||
1020 | cursor.close() |
||
1021 | cnx.close() |
||
1022 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
1023 | description='API.ENERGY_STORAGE_POWER_STATION_USER_RELATION_EXISTS') |
||
1024 | add_row = (" INSERT INTO tbl_energy_storage_power_stations_users (energy_storage_power_station_id, user_id) " |
||
1025 | " VALUES (%s, %s) ") |
||
1026 | cursor.execute(add_row, (id_, user_id,)) |
||
1027 | cnx.commit() |
||
1028 | cursor.close() |
||
1029 | cnx.close() |
||
1030 | cursor_user.close() |
||
1031 | cnx_user.close() |
||
1032 | |||
1033 | resp.status = falcon.HTTP_201 |
||
1034 | resp.location = '/energystoragepowerstations/' + str(id_) + '/users/' + str(user_id) |
||
1035 | |||
1036 | |||
1037 | View Code Duplication | class EnergyStoragePowerStationUserItem: |
|
1038 | def __init__(self): |
||
1039 | """Initializes Class""" |
||
1040 | pass |
||
1041 | |||
1042 | @staticmethod |
||
1043 | def on_options(req, resp, id_, uid): |
||
1044 | _ = req |
||
1045 | resp.status = falcon.HTTP_200 |
||
1046 | _ = id_ |
||
1047 | |||
1048 | @staticmethod |
||
1049 | @user_logger |
||
1050 | def on_delete(req, resp, id_, uid): |
||
1051 | # todo Verify if the user is bound when deleting it |
||
1052 | admin_control(req) |
||
1053 | if not id_.isdigit() or int(id_) <= 0: |
||
1054 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1055 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
1056 | |||
1057 | if not uid.isdigit() or int(uid) <= 0: |
||
1058 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1059 | description='API.INVALID_USER_ID') |
||
1060 | |||
1061 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1062 | cursor = cnx.cursor() |
||
1063 | cursor.execute(" SELECT name " |
||
1064 | " FROM tbl_energy_storage_power_stations " |
||
1065 | " WHERE id = %s ", (id_,)) |
||
1066 | if cursor.fetchone() is None: |
||
1067 | cursor.close() |
||
1068 | cnx.close() |
||
1069 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1070 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
1071 | |||
1072 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
||
1073 | cursor_user = cnx_user.cursor() |
||
1074 | cursor_user.execute(" SELECT name FROM tbl_users WHERE id = %s ", (uid,)) |
||
1075 | if cursor_user.fetchone() is None: |
||
1076 | cursor_user.close() |
||
1077 | cnx_user.close() |
||
1078 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1079 | description='API.USER_NOT_FOUND') |
||
1080 | |||
1081 | cursor.execute(" SELECT id " |
||
1082 | " FROM tbl_energy_storage_power_stations_users " |
||
1083 | " WHERE energy_storage_power_station_id = %s AND user_id = %s ", (id_, uid)) |
||
1084 | if cursor.fetchone() is None: |
||
1085 | cursor.close() |
||
1086 | cnx.close() |
||
1087 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1088 | description='API.ENERGY_STORAGE_POWER_STATION_USER_RELATION_NOT_FOUND') |
||
1089 | |||
1090 | cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_users " |
||
1091 | " WHERE energy_storage_power_station_id = %s AND user_id = %s ", (id_, uid)) |
||
1092 | cnx.commit() |
||
1093 | |||
1094 | cursor.close() |
||
1095 | cnx.close() |
||
1096 | cursor_user.close() |
||
1097 | cnx_user.close() |
||
1098 | |||
1099 | resp.status = falcon.HTTP_204 |
||
1100 | |||
1101 | |||
1102 | class EnergyStoragePowerStationExport: |
||
1103 | def __init__(self): |
||
1104 | """"Initializes Class""" |
||
1105 | pass |
||
1106 | |||
1107 | @staticmethod |
||
1108 | def on_options(req, resp, id_): |
||
1109 | _ = req |
||
1110 | resp.status = falcon.HTTP_200 |
||
1111 | _ = id_ |
||
1112 | |||
1113 | @staticmethod |
||
1114 | def on_get(req, resp, id_): |
||
1115 | access_control(req) |
||
1116 | if not id_.isdigit() or int(id_) <= 0: |
||
1117 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1118 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
1119 | |||
1120 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1121 | cursor = cnx.cursor() |
||
1122 | |||
1123 | query = (" SELECT id, name, uuid " |
||
1124 | " FROM tbl_contacts ") |
||
1125 | cursor.execute(query) |
||
1126 | rows_contacts = cursor.fetchall() |
||
1127 | |||
1128 | contact_dict = dict() |
||
1129 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
1130 | for row in rows_contacts: |
||
1131 | contact_dict[row[0]] = {"id": row[0], |
||
1132 | "name": row[1], |
||
1133 | "uuid": row[2]} |
||
1134 | |||
1135 | query = (" SELECT id, name, uuid " |
||
1136 | " FROM tbl_cost_centers ") |
||
1137 | cursor.execute(query) |
||
1138 | rows_cost_centers = cursor.fetchall() |
||
1139 | |||
1140 | cost_center_dict = dict() |
||
1141 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
1142 | for row in rows_cost_centers: |
||
1143 | cost_center_dict[row[0]] = {"id": row[0], |
||
1144 | "name": row[1], |
||
1145 | "uuid": row[2]} |
||
1146 | |||
1147 | query = (" SELECT id, name, uuid " |
||
1148 | " FROM tbl_svgs ") |
||
1149 | cursor.execute(query) |
||
1150 | rows_svgs = cursor.fetchall() |
||
1151 | |||
1152 | svg_dict = dict() |
||
1153 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
1154 | for row in rows_svgs: |
||
1155 | svg_dict[row[0]] = {"id": row[0], |
||
1156 | "name": row[1], |
||
1157 | "uuid": row[2]} |
||
1158 | |||
1159 | query = (" SELECT id, name, uuid, " |
||
1160 | " address, latitude, longitude, latitude_point_id, longitude_point_id, rated_capacity, " |
||
1161 | " rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
1162 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
||
1163 | " FROM tbl_energy_storage_power_stations " |
||
1164 | " WHERE id = %s ") |
||
1165 | cursor.execute(query, (id_,)) |
||
1166 | row = cursor.fetchone() |
||
1167 | |||
1168 | if row is None: |
||
1169 | cursor.close() |
||
1170 | cnx.close() |
||
1171 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1172 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
1173 | else: |
||
1174 | meta_result = {"id": row[0], |
||
1175 | "name": row[1], |
||
1176 | "uuid": row[2], |
||
1177 | "address": row[3], |
||
1178 | "latitude": row[4], |
||
1179 | "longitude": row[5], |
||
1180 | "latitude_point_id": row[6], |
||
1181 | "longitude_point_id": row[7], |
||
1182 | "rated_capacity": row[8], |
||
1183 | "rated_power": row[9], |
||
1184 | "contact": contact_dict.get(row[10], None), |
||
1185 | "cost_center": cost_center_dict.get(row[11], None), |
||
1186 | "svg": svg_dict.get(row[12], None), |
||
1187 | "svg2": svg_dict.get(row[13], None), |
||
1188 | "svg3": svg_dict.get(row[14], None), |
||
1189 | "svg4": svg_dict.get(row[15], None), |
||
1190 | "svg5": svg_dict.get(row[16], None), |
||
1191 | "is_cost_data_displayed": bool(row[17]), |
||
1192 | "phase_of_lifecycle": row[18], |
||
1193 | "commissioning_date": row[19].isoformat() if row[19] else None, |
||
1194 | "description": row[20]} |
||
1195 | |||
1196 | resp.text = json.dumps(meta_result) |
||
1197 | |||
1198 | |||
1199 | class EnergyStoragePowerStationImport: |
||
1200 | def __init__(self): |
||
1201 | """"Initializes Class""" |
||
1202 | pass |
||
1203 | |||
1204 | @staticmethod |
||
1205 | def on_options(req, resp): |
||
1206 | _ = req |
||
1207 | resp.status = falcon.HTTP_200 |
||
1208 | |||
1209 | @staticmethod |
||
1210 | @user_logger |
||
1211 | def on_post(req, resp): |
||
1212 | """Handles POST requests""" |
||
1213 | admin_control(req) |
||
1214 | try: |
||
1215 | raw_json = req.stream.read().decode('utf-8') |
||
1216 | except Exception as ex: |
||
1217 | print(str(ex)) |
||
1218 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
1219 | title='API.BAD_REQUEST', |
||
1220 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
1221 | |||
1222 | new_values = json.loads(raw_json) |
||
1223 | |||
1224 | print(new_values) |
||
1225 | if 'name' not in new_values.keys() or \ |
||
1226 | not isinstance(new_values['name'], str) or \ |
||
1227 | len(str.strip(new_values['name'])) == 0: |
||
1228 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1229 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
||
1230 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
1231 | if config.utc_offset[0] == '-': |
||
1232 | timezone_offset = -timezone_offset |
||
1233 | name = str.strip(new_values['name']) + \ |
||
1234 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
||
1235 | |||
1236 | if 'address' not in new_values.keys() or \ |
||
1237 | not isinstance(new_values['address'], str) or \ |
||
1238 | len(str.strip(new_values['address'])) == 0: |
||
1239 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1240 | description='API.INVALID_ADDRESS_VALUE') |
||
1241 | address = str.strip(new_values['address']) |
||
1242 | |||
1243 | if 'latitude' not in new_values.keys() or \ |
||
1244 | not (isinstance(new_values['latitude'], float) or |
||
1245 | isinstance(new_values['latitude'], int)) or \ |
||
1246 | new_values['latitude'] < -90.0 or \ |
||
1247 | new_values['latitude'] > 90.0: |
||
1248 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1249 | description='API.INVALID_LATITUDE_VALUE') |
||
1250 | latitude = new_values['latitude'] |
||
1251 | |||
1252 | if 'longitude' not in new_values.keys() or \ |
||
1253 | not (isinstance(new_values['longitude'], float) or |
||
1254 | isinstance(new_values['longitude'], int)) or \ |
||
1255 | new_values['longitude'] < -180.0 or \ |
||
1256 | new_values['longitude'] > 180.0: |
||
1257 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1258 | description='API.INVALID_LONGITUDE_VALUE') |
||
1259 | longitude = new_values['longitude'] |
||
1260 | |||
1261 | latitude_point_id = None |
||
1262 | longitude_point_id = None |
||
1263 | |||
1264 | if 'rated_capacity' not in new_values.keys() or \ |
||
1265 | not (isinstance(new_values['rated_capacity'], float) or |
||
1266 | isinstance(new_values['rated_capacity'], int)) or \ |
||
1267 | new_values['rated_capacity'] < 0.0: |
||
1268 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1269 | description='API.INVALID_RATED_CAPACITY') |
||
1270 | rated_capacity = new_values['rated_capacity'] |
||
1271 | |||
1272 | if 'rated_power' not in new_values.keys() or \ |
||
1273 | not (isinstance(new_values['rated_power'], float) or |
||
1274 | isinstance(new_values['rated_power'], int)) or \ |
||
1275 | new_values['rated_power'] < 0.0: |
||
1276 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1277 | description='API.INVALID_RATED_POWER') |
||
1278 | rated_power = new_values['rated_power'] |
||
1279 | |||
1280 | if 'contact' not in new_values.keys() or \ |
||
1281 | 'id' not in new_values['contact'].keys() or \ |
||
1282 | not isinstance(new_values['contact']['id'], int) or \ |
||
1283 | new_values['contact']['id'] <= 0: |
||
1284 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1285 | description='API.INVALID_CONTACT_ID') |
||
1286 | contact_id = new_values['contact']['id'] |
||
1287 | |||
1288 | if 'cost_center' not in new_values.keys() or \ |
||
1289 | 'id' not in new_values['cost_center'].keys() or \ |
||
1290 | not isinstance(new_values['cost_center']['id'], int) or \ |
||
1291 | new_values['cost_center']['id'] <= 0: |
||
1292 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1293 | description='API.INVALID_COST_CENTER_ID') |
||
1294 | cost_center_id = new_values['cost_center']['id'] |
||
1295 | |||
1296 | if 'svg' not in new_values.keys() or \ |
||
1297 | 'id' not in new_values['svg'].keys() or \ |
||
1298 | not isinstance(new_values['svg']['id'], int) or \ |
||
1299 | new_values['svg']['id'] <= 0: |
||
1300 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1301 | description='API.INVALID_SVG_ID') |
||
1302 | svg_id = new_values['svg']['id'] |
||
1303 | |||
1304 | View Code Duplication | if 'svg2' in new_values.keys() and \ |
|
1305 | new_values['svg2'] is not None and \ |
||
1306 | 'id' in new_values['svg2'].keys() and \ |
||
1307 | new_values['svg2']['id'] is not None: |
||
1308 | if not isinstance(new_values['svg2']['id'], int) or \ |
||
1309 | new_values['svg2']['id'] <= 0: |
||
1310 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1311 | description='API.INVALID_SVG2_ID') |
||
1312 | svg2_id = new_values['svg2']['id'] |
||
1313 | else: |
||
1314 | svg2_id = None |
||
1315 | |||
1316 | View Code Duplication | if 'svg3' in new_values.keys() and \ |
|
1317 | new_values['svg3'] is not None and \ |
||
1318 | 'id' in new_values['svg3'].keys() and \ |
||
1319 | new_values['svg3']['id'] is not None: |
||
1320 | if not isinstance(new_values['svg3']['id'], int) or \ |
||
1321 | new_values['svg3']['id'] <= 0: |
||
1322 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1323 | description='API.INVALID_SVG3_ID') |
||
1324 | svg3_id = new_values['svg3']['id'] |
||
1325 | else: |
||
1326 | svg3_id = None |
||
1327 | |||
1328 | View Code Duplication | if 'svg4' in new_values.keys() and \ |
|
1329 | new_values['svg4'] is not None and \ |
||
1330 | 'id' in new_values['svg4'].keys() and \ |
||
1331 | new_values['svg4']['id'] is not None: |
||
1332 | if not isinstance(new_values['svg4']['id'], int) or \ |
||
1333 | new_values['svg4']['id'] <= 0: |
||
1334 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1335 | description='API.INVALID_SVG4_ID') |
||
1336 | svg4_id = new_values['svg4']['id'] |
||
1337 | else: |
||
1338 | svg4_id = None |
||
1339 | |||
1340 | View Code Duplication | if 'svg5' in new_values.keys() and \ |
|
1341 | new_values['svg5'] is not None and \ |
||
1342 | 'id' in new_values['svg5'].keys() and \ |
||
1343 | new_values['svg5']['id'] is not None: |
||
1344 | if not isinstance(new_values['svg5']['id'], int) or \ |
||
1345 | new_values['svg5']['id'] <= 0: |
||
1346 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1347 | description='API.INVALID_SVG5_ID') |
||
1348 | svg5_id = new_values['svg5']['id'] |
||
1349 | else: |
||
1350 | svg5_id = None |
||
1351 | |||
1352 | if 'is_cost_data_displayed' not in new_values.keys() or \ |
||
1353 | not isinstance(new_values['is_cost_data_displayed'], bool): |
||
1354 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1355 | description='API.INVALID_IS_COST_DATA_DISPLAYED') |
||
1356 | is_cost_data_displayed = new_values['is_cost_data_displayed'] |
||
1357 | |||
1358 | if 'phase_of_lifecycle' not in new_values.keys() or \ |
||
1359 | not isinstance(new_values['phase_of_lifecycle'], str) or \ |
||
1360 | len(str.strip(new_values['phase_of_lifecycle'])) == 0: |
||
1361 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1362 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
||
1363 | phase_of_lifecycle = str.strip(new_values['phase_of_lifecycle']) |
||
1364 | |||
1365 | if 'commissioning_date' not in new_values.keys() or \ |
||
1366 | not isinstance(new_values['commissioning_date'], str) or \ |
||
1367 | len(str.strip(new_values['commissioning_date'])) == 0: |
||
1368 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1369 | description='API.INVALID_COMMISSIONING_DATE') |
||
1370 | commissioning_date = datetime.strptime(new_values['commissioning_date'], '%Y-%m-%d') |
||
1371 | |||
1372 | if 'description' in new_values.keys() and \ |
||
1373 | new_values['description'] is not None and \ |
||
1374 | len(str(new_values['description'])) > 0: |
||
1375 | description = str.strip(new_values['description']) |
||
1376 | else: |
||
1377 | description = None |
||
1378 | |||
1379 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1380 | cursor = cnx.cursor() |
||
1381 | |||
1382 | cursor.execute(" SELECT name " |
||
1383 | " FROM tbl_energy_storage_power_stations " |
||
1384 | " WHERE name = %s ", (name,)) |
||
1385 | if cursor.fetchone() is not None: |
||
1386 | cursor.close() |
||
1387 | cnx.close() |
||
1388 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1389 | description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
||
1390 | |||
1391 | cursor.execute(" SELECT name " |
||
1392 | " FROM tbl_contacts " |
||
1393 | " WHERE id = %s ", |
||
1394 | (contact_id,)) |
||
1395 | row = cursor.fetchone() |
||
1396 | if row is None: |
||
1397 | cursor.close() |
||
1398 | cnx.close() |
||
1399 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1400 | description='API.CONTACT_NOT_FOUND') |
||
1401 | |||
1402 | cursor.execute(" SELECT name " |
||
1403 | " FROM tbl_cost_centers " |
||
1404 | " WHERE id = %s ", |
||
1405 | (cost_center_id,)) |
||
1406 | row = cursor.fetchone() |
||
1407 | if row is None: |
||
1408 | cursor.close() |
||
1409 | cnx.close() |
||
1410 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1411 | description='API.COST_CENTER_NOT_FOUND') |
||
1412 | |||
1413 | cursor.execute(" SELECT name " |
||
1414 | " FROM tbl_svgs " |
||
1415 | " WHERE id = %s ", |
||
1416 | (svg_id,)) |
||
1417 | row = cursor.fetchone() |
||
1418 | if row is None: |
||
1419 | cursor.close() |
||
1420 | cnx.close() |
||
1421 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1422 | description='API.SVG_NOT_FOUND') |
||
1423 | |||
1424 | add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
||
1425 | " (name, uuid, address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
1426 | " rated_capacity, rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, " |
||
1427 | " svg4_id, svg5_id, is_cost_data_displayed, phase_of_lifecycle, commissioning_date, " |
||
1428 | " description) " |
||
1429 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
1430 | |||
1431 | cursor.execute(add_values, (name, |
||
1432 | str(uuid.uuid4()), |
||
1433 | address, |
||
1434 | latitude, |
||
1435 | longitude, |
||
1436 | latitude_point_id, |
||
1437 | longitude_point_id, |
||
1438 | rated_capacity, |
||
1439 | rated_power, |
||
1440 | contact_id, |
||
1441 | cost_center_id, |
||
1442 | svg_id, |
||
1443 | svg2_id, |
||
1444 | svg3_id, |
||
1445 | svg4_id, |
||
1446 | svg5_id, |
||
1447 | is_cost_data_displayed, |
||
1448 | phase_of_lifecycle, |
||
1449 | commissioning_date, |
||
1450 | description)) |
||
1451 | |||
1452 | new_id = cursor.lastrowid |
||
1453 | cnx.commit() |
||
1454 | cursor.close() |
||
1455 | cnx.close() |
||
1456 | |||
1457 | resp.status = falcon.HTTP_201 |
||
1458 | resp.location = '/energystoragepowerstations/' + str(new_id) |
||
1459 | |||
1460 | |||
1461 | class EnergyStoragePowerStationClone: |
||
1462 | def __init__(self): |
||
1463 | """Initializes Class""" |
||
1464 | pass |
||
1465 | |||
1466 | @staticmethod |
||
1467 | def on_options(req, resp, id_): |
||
1468 | _ = req |
||
1469 | resp.status = falcon.HTTP_200 |
||
1470 | _ = id_ |
||
1471 | |||
1472 | @staticmethod |
||
1473 | @user_logger |
||
1474 | def on_post(req, resp, id_): |
||
1475 | """Handles POST requests""" |
||
1476 | admin_control(req) |
||
1477 | if not id_.isdigit() or int(id_) <= 0: |
||
1478 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
1479 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
||
1480 | |||
1481 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
1482 | cursor = cnx.cursor() |
||
1483 | |||
1484 | query = (" SELECT id, name, uuid, " |
||
1485 | " address, latitude, longitude, latitude_point_id, longitude_point_id, rated_capacity, " |
||
1486 | " rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, svg4_id, svg5_id, " |
||
1487 | " is_cost_data_displayed, phase_of_lifecycle, commissioning_date, description " |
||
1488 | " FROM tbl_energy_storage_power_stations " |
||
1489 | " WHERE id = %s ") |
||
1490 | cursor.execute(query, (id_,)) |
||
1491 | row = cursor.fetchone() |
||
1492 | |||
1493 | if row is None: |
||
1494 | cursor.close() |
||
1495 | cnx.close() |
||
1496 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
1497 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
||
1498 | else: |
||
1499 | meta_result = {"name": row[1], |
||
1500 | "uuid": row[2], |
||
1501 | "address": row[3], |
||
1502 | "latitude": row[4], |
||
1503 | "longitude": row[5], |
||
1504 | "latitude_point_id": row[6], |
||
1505 | "longitude_point_id": row[7], |
||
1506 | "rated_capacity": row[8], |
||
1507 | "rated_power": row[9], |
||
1508 | "contact_id": row[10], |
||
1509 | "cost_center_id": row[11], |
||
1510 | "svg_id": row[12], |
||
1511 | "svg2_id": row[13], |
||
1512 | "svg3_id": row[14], |
||
1513 | "svg4_id": row[15], |
||
1514 | "svg5_id": row[16], |
||
1515 | "is_cost_data_displayed": row[17], |
||
1516 | "phase_of_lifecycle": row[18], |
||
1517 | "commissioning_date": row[19], |
||
1518 | "description": row[20]} |
||
1519 | |||
1520 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
1521 | if config.utc_offset[0] == '-': |
||
1522 | timezone_offset = -timezone_offset |
||
1523 | new_name = str.strip(meta_result['name']) + \ |
||
1524 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
||
1525 | |||
1526 | add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
||
1527 | " (name, uuid, address, latitude, longitude, latitude_point_id, longitude_point_id, " |
||
1528 | " rated_capacity, rated_power, contact_id, cost_center_id, svg_id, svg2_id, svg3_id, " |
||
1529 | " svg4_id, svg5_id, is_cost_data_displayed, phase_of_lifecycle, commissioning_date, " |
||
1530 | " description) " |
||
1531 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
1532 | cursor.execute(add_values, (new_name, |
||
1533 | str(uuid.uuid4()), |
||
1534 | meta_result['address'], |
||
1535 | meta_result['latitude'], |
||
1536 | meta_result['longitude'], |
||
1537 | meta_result['latitude_point_id'], |
||
1538 | meta_result['longitude_point_id'], |
||
1539 | meta_result['rated_capacity'], |
||
1540 | meta_result['rated_power'], |
||
1541 | meta_result['contact_id'], |
||
1542 | meta_result['cost_center_id'], |
||
1543 | meta_result['svg_id'], |
||
1544 | meta_result['svg2_id'], |
||
1545 | meta_result['svg3_id'], |
||
1546 | meta_result['svg4_id'], |
||
1547 | meta_result['svg5_id'], |
||
1548 | meta_result['is_cost_data_displayed'], |
||
1549 | meta_result['phase_of_lifecycle'], |
||
1550 | meta_result['commissioning_date'], |
||
1551 | meta_result['description'])) |
||
1552 | new_id = cursor.lastrowid |
||
1553 | cnx.commit() |
||
1554 | cursor.close() |
||
1555 | cnx.close() |
||
1556 | |||
1557 | resp.status = falcon.HTTP_201 |
||
1558 | resp.location = '/energystoragepowerstations/' + str(new_id) |
||
1559 |