| Total Complexity | 179 |
| Total Lines | 849 |
| Duplicated Lines | 19.08 % |
| 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.windfarm 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 WindFarmCollection: |
||
| 11 | """ |
||
| 12 | Wind Farm Collection Resource |
||
| 13 | |||
| 14 | This class handles CRUD operations for wind farm collection. |
||
| 15 | It provides endpoints for listing all wind farms and creating new wind farms. |
||
| 16 | Wind farms represent renewable energy generation facilities in the energy management system. |
||
| 17 | """ |
||
| 18 | def __init__(self): |
||
| 19 | pass |
||
| 20 | |||
| 21 | @staticmethod |
||
| 22 | def on_options(req, resp): |
||
| 23 | _ = req |
||
| 24 | resp.status = falcon.HTTP_200 |
||
| 25 | |||
| 26 | @staticmethod |
||
| 27 | def on_get(req, resp): |
||
| 28 | access_control(req) |
||
| 29 | search_query = req.get_param('q', default=None) |
||
| 30 | if search_query is not None and len(search_query.strip()) > 0: |
||
| 31 | search_query = search_query.strip() |
||
| 32 | else: |
||
| 33 | search_query = '' |
||
| 34 | |||
| 35 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 36 | cursor = cnx.cursor() |
||
| 37 | |||
| 38 | query = (" SELECT id, name, uuid " |
||
| 39 | " FROM tbl_contacts ") |
||
| 40 | cursor.execute(query) |
||
| 41 | rows_contacts = cursor.fetchall() |
||
| 42 | |||
| 43 | contact_dict = dict() |
||
| 44 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 45 | for row in rows_contacts: |
||
| 46 | contact_dict[row[0]] = {"id": row[0], |
||
| 47 | "name": row[1], |
||
| 48 | "uuid": row[2]} |
||
| 49 | |||
| 50 | query = (" SELECT id, name, uuid " |
||
| 51 | " FROM tbl_cost_centers ") |
||
| 52 | cursor.execute(query) |
||
| 53 | rows_cost_centers = cursor.fetchall() |
||
| 54 | |||
| 55 | cost_center_dict = dict() |
||
| 56 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 57 | for row in rows_cost_centers: |
||
| 58 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 59 | "name": row[1], |
||
| 60 | "uuid": row[2]} |
||
| 61 | |||
| 62 | svg_dict = dict() |
||
| 63 | query = (" SELECT id, name, uuid " |
||
| 64 | " FROM tbl_svgs ") |
||
| 65 | cursor.execute(query) |
||
| 66 | rows_svgs = cursor.fetchall() |
||
| 67 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 68 | for row in rows_svgs: |
||
| 69 | svg_dict[row[0]] = {"id": row[0], |
||
| 70 | "name": row[1], |
||
| 71 | "uuid": row[2]} |
||
| 72 | |||
| 73 | query = (" SELECT id, name, uuid, " |
||
| 74 | " address, latitude, longitude, rated_power, " |
||
| 75 | " contact_id, cost_center_id, svg_id, description " |
||
| 76 | " FROM tbl_wind_farms ") |
||
| 77 | params = [] |
||
| 78 | if search_query: |
||
| 79 | query += " WHERE name LIKE %s OR address LIKE %s OR description LIKE %s " |
||
| 80 | params = [f'%{search_query}%', f'%{search_query}%', f'%{search_query}%'] |
||
| 81 | query += " ORDER BY id " |
||
| 82 | cursor.execute(query, params) |
||
| 83 | rows_spaces = cursor.fetchall() |
||
| 84 | |||
| 85 | result = list() |
||
| 86 | View Code Duplication | if rows_spaces is not None and len(rows_spaces) > 0: |
|
|
|
|||
| 87 | for row in rows_spaces: |
||
| 88 | |||
| 89 | meta_result = {"id": row[0], |
||
| 90 | "name": row[1], |
||
| 91 | "uuid": row[2], |
||
| 92 | "address": row[3], |
||
| 93 | "latitude": row[4], |
||
| 94 | "longitude": row[5], |
||
| 95 | "rated_power": row[6], |
||
| 96 | "contact": contact_dict.get(row[7], None), |
||
| 97 | "cost_center": cost_center_dict.get(row[8], None), |
||
| 98 | "svg": svg_dict.get(row[9], None), |
||
| 99 | "description": row[10], |
||
| 100 | "qrcode": 'windfarm:' + row[2]} |
||
| 101 | result.append(meta_result) |
||
| 102 | |||
| 103 | cursor.close() |
||
| 104 | cnx.close() |
||
| 105 | resp.text = json.dumps(result) |
||
| 106 | |||
| 107 | @staticmethod |
||
| 108 | @user_logger |
||
| 109 | def on_post(req, resp): |
||
| 110 | """Handles POST requests""" |
||
| 111 | admin_control(req) |
||
| 112 | try: |
||
| 113 | raw_json = req.stream.read().decode('utf-8') |
||
| 114 | except Exception as ex: |
||
| 115 | print(str(ex)) |
||
| 116 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 117 | title='API.BAD_REQUEST', |
||
| 118 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 119 | |||
| 120 | new_values = json.loads(raw_json) |
||
| 121 | |||
| 122 | if 'name' not in new_values['data'].keys() or \ |
||
| 123 | not isinstance(new_values['data']['name'], str) or \ |
||
| 124 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 125 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 126 | description='API.INVALID_WIND_FARM_NAME') |
||
| 127 | name = str.strip(new_values['data']['name']) |
||
| 128 | |||
| 129 | if 'address' not in new_values['data'].keys() or \ |
||
| 130 | not isinstance(new_values['data']['address'], str) or \ |
||
| 131 | len(str.strip(new_values['data']['address'])) == 0: |
||
| 132 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 133 | description='API.INVALID_ADDRESS_VALUE') |
||
| 134 | address = str.strip(new_values['data']['address']) |
||
| 135 | |||
| 136 | if 'latitude' not in new_values['data'].keys() or \ |
||
| 137 | not (isinstance(new_values['data']['latitude'], float) or |
||
| 138 | isinstance(new_values['data']['latitude'], int)) or \ |
||
| 139 | new_values['data']['latitude'] < -90.0 or \ |
||
| 140 | new_values['data']['latitude'] > 90.0: |
||
| 141 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 142 | description='API.INVALID_LATITUDE_VALUE') |
||
| 143 | latitude = new_values['data']['latitude'] |
||
| 144 | |||
| 145 | if 'longitude' not in new_values['data'].keys() or \ |
||
| 146 | not (isinstance(new_values['data']['longitude'], float) or |
||
| 147 | isinstance(new_values['data']['longitude'], int)) or \ |
||
| 148 | new_values['data']['longitude'] < -180.0 or \ |
||
| 149 | new_values['data']['longitude'] > 180.0: |
||
| 150 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 151 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 152 | longitude = new_values['data']['longitude'] |
||
| 153 | |||
| 154 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 155 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 156 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 157 | new_values['data']['rated_power'] <= 0.0: |
||
| 158 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 159 | description='API.INVALID_RATED_POWER') |
||
| 160 | rated_power = new_values['data']['rated_power'] |
||
| 161 | |||
| 162 | if 'contact_id' not in new_values['data'].keys() or \ |
||
| 163 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
| 164 | new_values['data']['contact_id'] <= 0: |
||
| 165 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 166 | description='API.INVALID_CONTACT_ID') |
||
| 167 | contact_id = new_values['data']['contact_id'] |
||
| 168 | |||
| 169 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 170 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 171 | new_values['data']['cost_center_id'] <= 0: |
||
| 172 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 173 | description='API.INVALID_COST_CENTER_ID') |
||
| 174 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 175 | |||
| 176 | if 'svg_id' not in new_values['data'].keys() or \ |
||
| 177 | not isinstance(new_values['data']['svg_id'], int) or \ |
||
| 178 | new_values['data']['svg_id'] <= 0: |
||
| 179 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 180 | description='API.INVALID_SVG_ID') |
||
| 181 | svg_id = new_values['data']['svg_id'] |
||
| 182 | |||
| 183 | if 'description' in new_values['data'].keys() and \ |
||
| 184 | new_values['data']['description'] is not None and \ |
||
| 185 | len(str(new_values['data']['description'])) > 0: |
||
| 186 | description = str.strip(new_values['data']['description']) |
||
| 187 | else: |
||
| 188 | description = None |
||
| 189 | |||
| 190 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 191 | cursor = cnx.cursor() |
||
| 192 | |||
| 193 | cursor.execute(" SELECT name " |
||
| 194 | " FROM tbl_wind_farms " |
||
| 195 | " WHERE name = %s ", (name,)) |
||
| 196 | if cursor.fetchone() is not None: |
||
| 197 | cursor.close() |
||
| 198 | cnx.close() |
||
| 199 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 200 | description='API.WIND_FARM_NAME_IS_ALREADY_IN_USE') |
||
| 201 | |||
| 202 | cursor.execute(" SELECT name " |
||
| 203 | " FROM tbl_contacts " |
||
| 204 | " WHERE id = %s ", |
||
| 205 | (new_values['data']['contact_id'],)) |
||
| 206 | row = cursor.fetchone() |
||
| 207 | if row is None: |
||
| 208 | cursor.close() |
||
| 209 | cnx.close() |
||
| 210 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 211 | description='API.CONTACT_NOT_FOUND') |
||
| 212 | |||
| 213 | cursor.execute(" SELECT name " |
||
| 214 | " FROM tbl_cost_centers " |
||
| 215 | " WHERE id = %s ", |
||
| 216 | (new_values['data']['cost_center_id'],)) |
||
| 217 | row = cursor.fetchone() |
||
| 218 | if row is None: |
||
| 219 | cursor.close() |
||
| 220 | cnx.close() |
||
| 221 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 222 | description='API.COST_CENTER_NOT_FOUND') |
||
| 223 | |||
| 224 | cursor.execute(" SELECT name " |
||
| 225 | " FROM tbl_svgs " |
||
| 226 | " WHERE id = %s ", |
||
| 227 | (svg_id,)) |
||
| 228 | row = cursor.fetchone() |
||
| 229 | if row is None: |
||
| 230 | cursor.close() |
||
| 231 | cnx.close() |
||
| 232 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 233 | description='API.SVG_NOT_FOUND') |
||
| 234 | |||
| 235 | add_values = (" INSERT INTO tbl_wind_farms " |
||
| 236 | " (name, uuid, address, latitude, longitude, rated_power, " |
||
| 237 | " contact_id, cost_center_id, svg_id, description) " |
||
| 238 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 239 | cursor.execute(add_values, (name, |
||
| 240 | str(uuid.uuid4()), |
||
| 241 | address, |
||
| 242 | latitude, |
||
| 243 | longitude, |
||
| 244 | rated_power, |
||
| 245 | contact_id, |
||
| 246 | cost_center_id, |
||
| 247 | svg_id, |
||
| 248 | description)) |
||
| 249 | new_id = cursor.lastrowid |
||
| 250 | cnx.commit() |
||
| 251 | cursor.close() |
||
| 252 | cnx.close() |
||
| 253 | |||
| 254 | resp.status = falcon.HTTP_201 |
||
| 255 | resp.location = '/windfarms/' + str(new_id) |
||
| 256 | |||
| 257 | |||
| 258 | class WindFarmItem: |
||
| 259 | def __init__(self): |
||
| 260 | pass |
||
| 261 | |||
| 262 | @staticmethod |
||
| 263 | def on_options(req, resp, id_): |
||
| 264 | _ = req |
||
| 265 | resp.status = falcon.HTTP_200 |
||
| 266 | _ = id_ |
||
| 267 | |||
| 268 | View Code Duplication | @staticmethod |
|
| 269 | def on_get(req, resp, id_): |
||
| 270 | access_control(req) |
||
| 271 | if not id_.isdigit() or int(id_) <= 0: |
||
| 272 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 273 | description='API.INVALID_WIND_FARM_ID') |
||
| 274 | |||
| 275 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 276 | cursor = cnx.cursor() |
||
| 277 | |||
| 278 | query = (" SELECT id, name, uuid " |
||
| 279 | " FROM tbl_contacts ") |
||
| 280 | cursor.execute(query) |
||
| 281 | rows_contacts = cursor.fetchall() |
||
| 282 | |||
| 283 | contact_dict = dict() |
||
| 284 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 285 | for row in rows_contacts: |
||
| 286 | contact_dict[row[0]] = {"id": row[0], |
||
| 287 | "name": row[1], |
||
| 288 | "uuid": row[2]} |
||
| 289 | |||
| 290 | query = (" SELECT id, name, uuid " |
||
| 291 | " FROM tbl_cost_centers ") |
||
| 292 | cursor.execute(query) |
||
| 293 | rows_cost_centers = cursor.fetchall() |
||
| 294 | |||
| 295 | cost_center_dict = dict() |
||
| 296 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 297 | for row in rows_cost_centers: |
||
| 298 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 299 | "name": row[1], |
||
| 300 | "uuid": row[2]} |
||
| 301 | |||
| 302 | svg_dict = dict() |
||
| 303 | query = (" SELECT id, name, uuid " |
||
| 304 | " FROM tbl_svgs ") |
||
| 305 | cursor.execute(query) |
||
| 306 | rows_svgs = cursor.fetchall() |
||
| 307 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 308 | for row in rows_svgs: |
||
| 309 | svg_dict[row[0]] = {"id": row[0], |
||
| 310 | "name": row[1], |
||
| 311 | "uuid": row[2]} |
||
| 312 | |||
| 313 | query = (" SELECT id, name, uuid, " |
||
| 314 | " address, latitude, longitude, rated_power, " |
||
| 315 | " contact_id, cost_center_id, svg_id, description " |
||
| 316 | " FROM tbl_wind_farms " |
||
| 317 | " WHERE id = %s ") |
||
| 318 | cursor.execute(query, (id_,)) |
||
| 319 | row = cursor.fetchone() |
||
| 320 | cursor.close() |
||
| 321 | cnx.close() |
||
| 322 | |||
| 323 | if row is None: |
||
| 324 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 325 | description='API.WIND_FARM_NOT_FOUND') |
||
| 326 | else: |
||
| 327 | meta_result = {"id": row[0], |
||
| 328 | "name": row[1], |
||
| 329 | "uuid": row[2], |
||
| 330 | "address": row[3], |
||
| 331 | "latitude": row[4], |
||
| 332 | "longitude": row[5], |
||
| 333 | "rated_power": row[6], |
||
| 334 | "contact": contact_dict.get(row[7], None), |
||
| 335 | "cost_center": cost_center_dict.get(row[8], None), |
||
| 336 | "svg": svg_dict.get(row[9], None), |
||
| 337 | "description": row[10], |
||
| 338 | "qrcode": 'windfarm:' + row[2]} |
||
| 339 | |||
| 340 | resp.text = json.dumps(meta_result) |
||
| 341 | |||
| 342 | @staticmethod |
||
| 343 | @user_logger |
||
| 344 | def on_delete(req, resp, id_): |
||
| 345 | admin_control(req) |
||
| 346 | if not id_.isdigit() or int(id_) <= 0: |
||
| 347 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 348 | description='API.INVALID_WIND_FARM_ID') |
||
| 349 | |||
| 350 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 351 | cursor = cnx.cursor() |
||
| 352 | |||
| 353 | cursor.execute(" SELECT name " |
||
| 354 | " FROM tbl_wind_farms " |
||
| 355 | " WHERE id = %s ", (id_,)) |
||
| 356 | if cursor.fetchone() is None: |
||
| 357 | cursor.close() |
||
| 358 | cnx.close() |
||
| 359 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 360 | description='API.WIND_FARM_NOT_FOUND') |
||
| 361 | |||
| 362 | cursor.execute(" DELETE FROM tbl_wind_farms WHERE id = %s ", (id_,)) |
||
| 363 | cnx.commit() |
||
| 364 | |||
| 365 | cursor.close() |
||
| 366 | cnx.close() |
||
| 367 | |||
| 368 | resp.status = falcon.HTTP_204 |
||
| 369 | |||
| 370 | @staticmethod |
||
| 371 | @user_logger |
||
| 372 | def on_put(req, resp, id_): |
||
| 373 | """Handles PUT requests""" |
||
| 374 | admin_control(req) |
||
| 375 | try: |
||
| 376 | raw_json = req.stream.read().decode('utf-8') |
||
| 377 | except Exception as ex: |
||
| 378 | print(str(ex)) |
||
| 379 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 380 | title='API.BAD_REQUEST', |
||
| 381 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 382 | |||
| 383 | if not id_.isdigit() or int(id_) <= 0: |
||
| 384 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 385 | description='API.INVALID_WIND_FARM_ID') |
||
| 386 | |||
| 387 | new_values = json.loads(raw_json) |
||
| 388 | |||
| 389 | if 'name' not in new_values['data'].keys() or \ |
||
| 390 | not isinstance(new_values['data']['name'], str) or \ |
||
| 391 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 392 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 393 | description='API.INVALID_WIND_FARM_NAME') |
||
| 394 | name = str.strip(new_values['data']['name']) |
||
| 395 | |||
| 396 | if 'address' not in new_values['data'].keys() or \ |
||
| 397 | not isinstance(new_values['data']['address'], str) or \ |
||
| 398 | len(str.strip(new_values['data']['address'])) == 0: |
||
| 399 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 400 | description='API.INVALID_ADDRESS_VALUE') |
||
| 401 | address = str.strip(new_values['data']['address']) |
||
| 402 | |||
| 403 | if 'latitude' not in new_values['data'].keys() or \ |
||
| 404 | not (isinstance(new_values['data']['latitude'], float) or |
||
| 405 | isinstance(new_values['data']['latitude'], int)) or \ |
||
| 406 | new_values['data']['latitude'] < -90.0 or \ |
||
| 407 | new_values['data']['latitude'] > 90.0: |
||
| 408 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 409 | description='API.INVALID_LATITUDE_VALUE') |
||
| 410 | latitude = new_values['data']['latitude'] |
||
| 411 | |||
| 412 | if 'longitude' not in new_values['data'].keys() or \ |
||
| 413 | not (isinstance(new_values['data']['longitude'], float) or |
||
| 414 | isinstance(new_values['data']['longitude'], int)) or \ |
||
| 415 | new_values['data']['longitude'] < -180.0 or \ |
||
| 416 | new_values['data']['longitude'] > 180.0: |
||
| 417 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 418 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 419 | longitude = new_values['data']['longitude'] |
||
| 420 | |||
| 421 | if 'rated_power' not in new_values['data'].keys() or \ |
||
| 422 | not (isinstance(new_values['data']['rated_power'], float) or |
||
| 423 | isinstance(new_values['data']['rated_power'], int)) or \ |
||
| 424 | new_values['data']['rated_power'] <= 0.0: |
||
| 425 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 426 | description='API.INVALID_RATED_POWER') |
||
| 427 | rated_power = new_values['data']['rated_power'] |
||
| 428 | |||
| 429 | if 'contact_id' not in new_values['data'].keys() or \ |
||
| 430 | not isinstance(new_values['data']['contact_id'], int) or \ |
||
| 431 | new_values['data']['contact_id'] <= 0: |
||
| 432 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 433 | description='API.INVALID_CONTACT_ID') |
||
| 434 | contact_id = new_values['data']['contact_id'] |
||
| 435 | |||
| 436 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 437 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 438 | new_values['data']['cost_center_id'] <= 0: |
||
| 439 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 440 | description='API.INVALID_COST_CENTER_ID') |
||
| 441 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 442 | |||
| 443 | if 'svg_id' not in new_values['data'].keys() or \ |
||
| 444 | not isinstance(new_values['data']['svg_id'], int) or \ |
||
| 445 | new_values['data']['svg_id'] <= 0: |
||
| 446 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 447 | description='API.INVALID_SVG_ID') |
||
| 448 | svg_id = new_values['data']['svg_id'] |
||
| 449 | |||
| 450 | if 'description' in new_values['data'].keys() and \ |
||
| 451 | new_values['data']['description'] is not None and \ |
||
| 452 | len(str(new_values['data']['description'])) > 0: |
||
| 453 | description = str.strip(new_values['data']['description']) |
||
| 454 | else: |
||
| 455 | description = None |
||
| 456 | |||
| 457 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 458 | cursor = cnx.cursor() |
||
| 459 | |||
| 460 | cursor.execute(" SELECT name " |
||
| 461 | " FROM tbl_wind_farms " |
||
| 462 | " WHERE id = %s ", (id_,)) |
||
| 463 | if cursor.fetchone() is None: |
||
| 464 | cursor.close() |
||
| 465 | cnx.close() |
||
| 466 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 467 | description='API.WIND_FARM_NOT_FOUND') |
||
| 468 | |||
| 469 | cursor.execute(" SELECT name " |
||
| 470 | " FROM tbl_wind_farms " |
||
| 471 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 472 | if cursor.fetchone() is not None: |
||
| 473 | cursor.close() |
||
| 474 | cnx.close() |
||
| 475 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 476 | description='API.WIND_FARM_NAME_IS_ALREADY_IN_USE') |
||
| 477 | |||
| 478 | cursor.execute(" SELECT name " |
||
| 479 | " FROM tbl_contacts " |
||
| 480 | " WHERE id = %s ", |
||
| 481 | (new_values['data']['contact_id'],)) |
||
| 482 | row = cursor.fetchone() |
||
| 483 | if row is None: |
||
| 484 | cursor.close() |
||
| 485 | cnx.close() |
||
| 486 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 487 | description='API.CONTACT_NOT_FOUND') |
||
| 488 | |||
| 489 | cursor.execute(" SELECT name " |
||
| 490 | " FROM tbl_cost_centers " |
||
| 491 | " WHERE id = %s ", |
||
| 492 | (new_values['data']['cost_center_id'],)) |
||
| 493 | row = cursor.fetchone() |
||
| 494 | if row is None: |
||
| 495 | cursor.close() |
||
| 496 | cnx.close() |
||
| 497 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 498 | description='API.COST_CENTER_NOT_FOUND') |
||
| 499 | |||
| 500 | cursor.execute(" SELECT name " |
||
| 501 | " FROM tbl_svgs " |
||
| 502 | " WHERE id = %s ", |
||
| 503 | (new_values['data']['svg_id'],)) |
||
| 504 | row = cursor.fetchone() |
||
| 505 | if row is None: |
||
| 506 | cursor.close() |
||
| 507 | cnx.close() |
||
| 508 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 509 | description='API.SVG_NOT_FOUND') |
||
| 510 | |||
| 511 | update_row = (" UPDATE tbl_wind_farms " |
||
| 512 | " SET name = %s, address = %s, latitude = %s, longitude = %s, rated_power = %s," |
||
| 513 | " contact_id = %s, cost_center_id = %s, svg_id = %s, description = %s " |
||
| 514 | " WHERE id = %s ") |
||
| 515 | cursor.execute(update_row, (name, |
||
| 516 | address, |
||
| 517 | latitude, |
||
| 518 | longitude, |
||
| 519 | rated_power, |
||
| 520 | contact_id, |
||
| 521 | cost_center_id, |
||
| 522 | svg_id, |
||
| 523 | description, |
||
| 524 | id_)) |
||
| 525 | cnx.commit() |
||
| 526 | |||
| 527 | cursor.close() |
||
| 528 | cnx.close() |
||
| 529 | |||
| 530 | resp.status = falcon.HTTP_200 |
||
| 531 | |||
| 532 | |||
| 533 | class WindFarmExport: |
||
| 534 | def __init__(self): |
||
| 535 | pass |
||
| 536 | |||
| 537 | @staticmethod |
||
| 538 | def on_options(req, resp, id_): |
||
| 539 | _ = req |
||
| 540 | resp.status = falcon.HTTP_200 |
||
| 541 | _ = id_ |
||
| 542 | |||
| 543 | View Code Duplication | @staticmethod |
|
| 544 | def on_get(req, resp, id_): |
||
| 545 | access_control(req) |
||
| 546 | if not id_.isdigit() or int(id_) <= 0: |
||
| 547 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 548 | description='API.INVALID_WIND_FARM_ID') |
||
| 549 | |||
| 550 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 551 | cursor = cnx.cursor() |
||
| 552 | |||
| 553 | query = (" SELECT id, name, uuid " |
||
| 554 | " FROM tbl_contacts ") |
||
| 555 | cursor.execute(query) |
||
| 556 | rows_contacts = cursor.fetchall() |
||
| 557 | |||
| 558 | contact_dict = dict() |
||
| 559 | if rows_contacts is not None and len(rows_contacts) > 0: |
||
| 560 | for row in rows_contacts: |
||
| 561 | contact_dict[row[0]] = {"id": row[0], |
||
| 562 | "name": row[1], |
||
| 563 | "uuid": row[2]} |
||
| 564 | |||
| 565 | query = (" SELECT id, name, uuid " |
||
| 566 | " FROM tbl_cost_centers ") |
||
| 567 | cursor.execute(query) |
||
| 568 | rows_cost_centers = cursor.fetchall() |
||
| 569 | |||
| 570 | cost_center_dict = dict() |
||
| 571 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 572 | for row in rows_cost_centers: |
||
| 573 | cost_center_dict[row[0]] = {"id": row[0], |
||
| 574 | "name": row[1], |
||
| 575 | "uuid": row[2]} |
||
| 576 | |||
| 577 | query = (" SELECT id, name, uuid " |
||
| 578 | " FROM tbl_svgs ") |
||
| 579 | cursor.execute(query) |
||
| 580 | rows_svgs = cursor.fetchall() |
||
| 581 | |||
| 582 | svg_dict = dict() |
||
| 583 | if rows_svgs is not None and len(rows_svgs) > 0: |
||
| 584 | for row in rows_svgs: |
||
| 585 | svg_dict[row[0]] = {"id": row[0], |
||
| 586 | "name": row[1], |
||
| 587 | "uuid": row[2]} |
||
| 588 | |||
| 589 | query = (" SELECT id, name, uuid, " |
||
| 590 | " address, latitude, longitude, rated_power, " |
||
| 591 | " contact_id, cost_center_id, svg_id, description " |
||
| 592 | " FROM tbl_wind_farms " |
||
| 593 | " WHERE id = %s ") |
||
| 594 | cursor.execute(query, (id_,)) |
||
| 595 | row = cursor.fetchone() |
||
| 596 | cursor.close() |
||
| 597 | cnx.close() |
||
| 598 | |||
| 599 | if row is None: |
||
| 600 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 601 | description='API.WIND_FARM_NOT_FOUND') |
||
| 602 | else: |
||
| 603 | meta_result = {"id": row[0], |
||
| 604 | "name": row[1], |
||
| 605 | "uuid": row[2], |
||
| 606 | "address": row[3], |
||
| 607 | "latitude": row[4], |
||
| 608 | "longitude": row[5], |
||
| 609 | "rated_power": row[6], |
||
| 610 | "contact": contact_dict.get(row[7], None), |
||
| 611 | "cost_center": cost_center_dict.get(row[8], None), |
||
| 612 | "svg": svg_dict.get(row[9], None), |
||
| 613 | "description": row[10]} |
||
| 614 | |||
| 615 | resp.text = json.dumps(meta_result) |
||
| 616 | |||
| 617 | |||
| 618 | class WindFarmImport: |
||
| 619 | def __init__(self): |
||
| 620 | pass |
||
| 621 | |||
| 622 | @staticmethod |
||
| 623 | def on_options(req, resp): |
||
| 624 | _ = req |
||
| 625 | resp.status = falcon.HTTP_200 |
||
| 626 | |||
| 627 | @staticmethod |
||
| 628 | @user_logger |
||
| 629 | def on_post(req, resp): |
||
| 630 | """Handles POST requests""" |
||
| 631 | admin_control(req) |
||
| 632 | try: |
||
| 633 | raw_json = req.stream.read().decode('utf-8') |
||
| 634 | except Exception as ex: |
||
| 635 | print(str(ex)) |
||
| 636 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 637 | title='API.BAD_REQUEST', |
||
| 638 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 639 | |||
| 640 | new_values = json.loads(raw_json) |
||
| 641 | |||
| 642 | if 'name' not in new_values.keys() or \ |
||
| 643 | not isinstance(new_values['name'], str) or \ |
||
| 644 | len(str.strip(new_values['name'])) == 0: |
||
| 645 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 646 | description='API.INVALID_WIND_FARM_NAME') |
||
| 647 | name = str.strip(new_values['name']) |
||
| 648 | |||
| 649 | if 'address' not in new_values.keys() or \ |
||
| 650 | not isinstance(new_values['address'], str) or \ |
||
| 651 | len(str.strip(new_values['address'])) == 0: |
||
| 652 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 653 | description='API.INVALID_ADDRESS_VALUE') |
||
| 654 | address = str.strip(new_values['address']) |
||
| 655 | |||
| 656 | if 'latitude' not in new_values.keys() or \ |
||
| 657 | not (isinstance(new_values['latitude'], float) or |
||
| 658 | isinstance(new_values['latitude'], int)) or \ |
||
| 659 | new_values['latitude'] < -90.0 or \ |
||
| 660 | new_values['latitude'] > 90.0: |
||
| 661 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 662 | description='API.INVALID_LATITUDE_VALUE') |
||
| 663 | latitude = new_values['latitude'] |
||
| 664 | |||
| 665 | if 'longitude' not in new_values.keys() or \ |
||
| 666 | not (isinstance(new_values['longitude'], float) or |
||
| 667 | isinstance(new_values['longitude'], int)) or \ |
||
| 668 | new_values['longitude'] < -180.0 or \ |
||
| 669 | new_values['longitude'] > 180.0: |
||
| 670 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 671 | description='API.INVALID_LONGITUDE_VALUE') |
||
| 672 | longitude = new_values['longitude'] |
||
| 673 | |||
| 674 | if 'rated_power' not in new_values.keys() or \ |
||
| 675 | not (isinstance(new_values['rated_power'], float) or |
||
| 676 | isinstance(new_values['rated_power'], int)) or \ |
||
| 677 | new_values['rated_power'] <= 0.0: |
||
| 678 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 679 | description='API.INVALID_RATED_POWER') |
||
| 680 | rated_power = new_values['rated_power'] |
||
| 681 | |||
| 682 | if 'id' not in new_values['contact'].keys() or \ |
||
| 683 | not isinstance(new_values['contact']['id'], int) or \ |
||
| 684 | new_values['contact']['id'] <= 0: |
||
| 685 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 686 | description='API.INVALID_CONTACT_ID') |
||
| 687 | contact_id = new_values['contact']['id'] |
||
| 688 | |||
| 689 | if 'id' not in new_values['cost_center'].keys() or \ |
||
| 690 | not isinstance(new_values['cost_center']['id'], int) or \ |
||
| 691 | new_values['cost_center']['id'] <= 0: |
||
| 692 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 693 | description='API.INVALID_COST_CENTER_ID') |
||
| 694 | cost_center_id = new_values['cost_center']['id'] |
||
| 695 | |||
| 696 | if 'svg' not in new_values.keys() or \ |
||
| 697 | 'id' not in new_values['svg'].keys() or \ |
||
| 698 | not isinstance(new_values['svg']['id'], int) or \ |
||
| 699 | new_values['svg']['id'] <= 0: |
||
| 700 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 701 | description='API.INVALID_SVG_ID') |
||
| 702 | svg_id = new_values['svg']['id'] |
||
| 703 | |||
| 704 | if 'description' in new_values.keys() and \ |
||
| 705 | new_values['description'] is not None and \ |
||
| 706 | len(str(new_values['description'])) > 0: |
||
| 707 | description = str.strip(new_values['description']) |
||
| 708 | else: |
||
| 709 | description = None |
||
| 710 | |||
| 711 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 712 | cursor = cnx.cursor() |
||
| 713 | |||
| 714 | cursor.execute(" SELECT name " |
||
| 715 | " FROM tbl_wind_farms " |
||
| 716 | " WHERE name = %s ", (name,)) |
||
| 717 | if cursor.fetchone() is not None: |
||
| 718 | cursor.close() |
||
| 719 | cnx.close() |
||
| 720 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 721 | description='API.WIND_FARM_NAME_IS_ALREADY_IN_USE') |
||
| 722 | |||
| 723 | cursor.execute(" SELECT name " |
||
| 724 | " FROM tbl_contacts " |
||
| 725 | " WHERE id = %s ", |
||
| 726 | (new_values['contact']['id'],)) |
||
| 727 | row = cursor.fetchone() |
||
| 728 | if row is None: |
||
| 729 | cursor.close() |
||
| 730 | cnx.close() |
||
| 731 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 732 | description='API.CONTACT_NOT_FOUND') |
||
| 733 | |||
| 734 | cursor.execute(" SELECT name " |
||
| 735 | " FROM tbl_cost_centers " |
||
| 736 | " WHERE id = %s ", |
||
| 737 | (new_values['cost_center']['id'],)) |
||
| 738 | row = cursor.fetchone() |
||
| 739 | if row is None: |
||
| 740 | cursor.close() |
||
| 741 | cnx.close() |
||
| 742 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 743 | description='API.COST_CENTER_NOT_FOUND') |
||
| 744 | |||
| 745 | cursor.execute(" SELECT name " |
||
| 746 | " FROM tbl_svgs " |
||
| 747 | " WHERE id = %s ", |
||
| 748 | (svg_id,)) |
||
| 749 | row = cursor.fetchone() |
||
| 750 | if row is None: |
||
| 751 | cursor.close() |
||
| 752 | cnx.close() |
||
| 753 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 754 | description='API.SVG_NOT_FOUND') |
||
| 755 | |||
| 756 | add_values = (" INSERT INTO tbl_wind_farms " |
||
| 757 | " (name, uuid, address, latitude, longitude, rated_power, " |
||
| 758 | " contact_id, cost_center_id, svg_id, description) " |
||
| 759 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 760 | cursor.execute(add_values, (name, |
||
| 761 | str(uuid.uuid4()), |
||
| 762 | address, |
||
| 763 | latitude, |
||
| 764 | longitude, |
||
| 765 | rated_power, |
||
| 766 | contact_id, |
||
| 767 | cost_center_id, |
||
| 768 | svg_id, |
||
| 769 | description)) |
||
| 770 | new_id = cursor.lastrowid |
||
| 771 | cnx.commit() |
||
| 772 | cursor.close() |
||
| 773 | cnx.close() |
||
| 774 | |||
| 775 | resp.status = falcon.HTTP_201 |
||
| 776 | resp.location = '/windfarms/' + str(new_id) |
||
| 777 | |||
| 778 | |||
| 779 | class WindFarmClone: |
||
| 780 | def __init__(self): |
||
| 781 | pass |
||
| 782 | |||
| 783 | @staticmethod |
||
| 784 | def on_options(req, resp, id_): |
||
| 785 | _ = req |
||
| 786 | resp.status = falcon.HTTP_200 |
||
| 787 | _ = id_ |
||
| 788 | |||
| 789 | @staticmethod |
||
| 790 | @user_logger |
||
| 791 | def on_post(req, resp, id_): |
||
| 792 | admin_control(req) |
||
| 793 | if not id_.isdigit() or int(id_) <= 0: |
||
| 794 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 795 | description='API.INVALID_WIND_FARM_ID') |
||
| 796 | |||
| 797 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 798 | cursor = cnx.cursor() |
||
| 799 | |||
| 800 | query = (" SELECT id, name, uuid, " |
||
| 801 | " address, latitude, longitude, rated_power, " |
||
| 802 | " contact_id, cost_center_id, svg_id, description " |
||
| 803 | " FROM tbl_wind_farms " |
||
| 804 | " WHERE id = %s ") |
||
| 805 | cursor.execute(query, (id_,)) |
||
| 806 | row = cursor.fetchone() |
||
| 807 | |||
| 808 | if row is None: |
||
| 809 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 810 | description='API.WIND_FARM_NOT_FOUND') |
||
| 811 | else: |
||
| 812 | meta_result = {"id": row[0], |
||
| 813 | "name": row[1], |
||
| 814 | "uuid": row[2], |
||
| 815 | "address": row[3], |
||
| 816 | "latitude": row[4], |
||
| 817 | "longitude": row[5], |
||
| 818 | "rated_power": row[6], |
||
| 819 | "contact_id": row[7], |
||
| 820 | "cost_center_id": row[8], |
||
| 821 | "svg_id": row[9], |
||
| 822 | "description": row[10]} |
||
| 823 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 824 | if config.utc_offset[0] == '-': |
||
| 825 | timezone_offset = -timezone_offset |
||
| 826 | new_name = (str.strip(meta_result['name']) + |
||
| 827 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')) |
||
| 828 | add_values = (" INSERT INTO tbl_wind_farms " |
||
| 829 | " (name, uuid, address, latitude, longitude, rated_power, " |
||
| 830 | " contact_id, cost_center_id, svg_id, description) " |
||
| 831 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 832 | cursor.execute(add_values, (new_name, |
||
| 833 | str(uuid.uuid4()), |
||
| 834 | meta_result['address'], |
||
| 835 | meta_result['latitude'], |
||
| 836 | meta_result['longitude'], |
||
| 837 | meta_result['rated_power'], |
||
| 838 | meta_result['contact_id'], |
||
| 839 | meta_result['cost_center_id'], |
||
| 840 | meta_result['svg_id'], |
||
| 841 | meta_result['description'])) |
||
| 842 | new_id = cursor.lastrowid |
||
| 843 | cnx.commit() |
||
| 844 | cursor.close() |
||
| 845 | cnx.close() |
||
| 846 | |||
| 847 | resp.status = falcon.HTTP_201 |
||
| 848 | resp.location = '/windfarms/' + str(new_id) |
||
| 849 | |||
| 850 |