| Total Complexity | 122 |
| Total Lines | 621 |
| Duplicated Lines | 10.79 % |
| 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.offlinemeter 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 falcon |
||
| 2 | import simplejson as json |
||
| 3 | import mysql.connector |
||
| 4 | import config |
||
| 5 | import uuid |
||
| 6 | |||
| 7 | |||
| 8 | class OfflineMeterCollection: |
||
| 9 | @staticmethod |
||
| 10 | def __init__(): |
||
| 11 | pass |
||
| 12 | |||
| 13 | @staticmethod |
||
| 14 | def on_options(req, resp): |
||
| 15 | resp.status = falcon.HTTP_200 |
||
| 16 | |||
| 17 | @staticmethod |
||
| 18 | def on_get(req, resp): |
||
| 19 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 20 | cursor = cnx.cursor(dictionary=True) |
||
| 21 | |||
| 22 | query = (" SELECT id, name, uuid " |
||
| 23 | " FROM tbl_energy_categories ") |
||
| 24 | cursor.execute(query) |
||
| 25 | rows_energy_categories = cursor.fetchall() |
||
| 26 | |||
| 27 | energy_category_dict = dict() |
||
| 28 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 29 | for row in rows_energy_categories: |
||
| 30 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 31 | "name": row['name'], |
||
| 32 | "uuid": row['uuid']} |
||
| 33 | |||
| 34 | query = (" SELECT id, name, uuid " |
||
| 35 | " FROM tbl_energy_items ") |
||
| 36 | cursor.execute(query) |
||
| 37 | rows_energy_items = cursor.fetchall() |
||
| 38 | |||
| 39 | energy_item_dict = dict() |
||
| 40 | if rows_energy_items is not None and len(rows_energy_items) > 0: |
||
| 41 | for row in rows_energy_items: |
||
| 42 | energy_item_dict[row['id']] = {"id": row['id'], |
||
| 43 | "name": row['name'], |
||
| 44 | "uuid": row['uuid']} |
||
| 45 | |||
| 46 | query = (" SELECT id, name, uuid " |
||
| 47 | " FROM tbl_cost_centers ") |
||
| 48 | cursor.execute(query) |
||
| 49 | rows_cost_centers = cursor.fetchall() |
||
| 50 | |||
| 51 | cost_center_dict = dict() |
||
| 52 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 53 | for row in rows_cost_centers: |
||
| 54 | cost_center_dict[row['id']] = {"id": row['id'], |
||
| 55 | "name": row['name'], |
||
| 56 | "uuid": row['uuid']} |
||
| 57 | |||
| 58 | query = (" SELECT id, name, uuid, energy_category_id, " |
||
| 59 | " is_counted, hourly_low_limit, hourly_high_limit, " |
||
| 60 | " energy_item_id, cost_center_id, description " |
||
| 61 | " FROM tbl_offline_meters " |
||
| 62 | " ORDER BY id ") |
||
| 63 | cursor.execute(query) |
||
| 64 | rows_meters = cursor.fetchall() |
||
| 65 | |||
| 66 | result = list() |
||
| 67 | View Code Duplication | if rows_meters is not None and len(rows_meters) > 0: |
|
|
|
|||
| 68 | for row in rows_meters: |
||
| 69 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 70 | energy_item = energy_item_dict.get(row['energy_item_id'], None) |
||
| 71 | cost_center = cost_center_dict.get(row['cost_center_id'], None) |
||
| 72 | meta_result = {"id": row['id'], |
||
| 73 | "name": row['name'], |
||
| 74 | "uuid": row['uuid'], |
||
| 75 | "energy_category": energy_category, |
||
| 76 | "is_counted": True if row['is_counted'] else False, |
||
| 77 | "hourly_low_limit": row['hourly_low_limit'], |
||
| 78 | "hourly_high_limit": row['hourly_high_limit'], |
||
| 79 | "energy_item": energy_item, |
||
| 80 | "cost_center": cost_center, |
||
| 81 | "description": row['description']} |
||
| 82 | result.append(meta_result) |
||
| 83 | |||
| 84 | cursor.close() |
||
| 85 | cnx.disconnect() |
||
| 86 | resp.body = json.dumps(result) |
||
| 87 | |||
| 88 | @staticmethod |
||
| 89 | def on_post(req, resp): |
||
| 90 | """Handles POST requests""" |
||
| 91 | try: |
||
| 92 | raw_json = req.stream.read().decode('utf-8') |
||
| 93 | except Exception as ex: |
||
| 94 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 95 | |||
| 96 | new_values = json.loads(raw_json) |
||
| 97 | |||
| 98 | if 'name' not in new_values['data'].keys() or \ |
||
| 99 | not isinstance(new_values['data']['name'], str) or \ |
||
| 100 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 101 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 102 | description='API.INVALID_OFFLINE_METER_NAME') |
||
| 103 | name = str.strip(new_values['data']['name']) |
||
| 104 | |||
| 105 | if 'energy_category_id' not in new_values['data'].keys() or \ |
||
| 106 | not isinstance(new_values['data']['energy_category_id'], int) or \ |
||
| 107 | new_values['data']['energy_category_id'] <= 0: |
||
| 108 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 109 | description='API.INVALID_ENERGY_CATEGORY_ID') |
||
| 110 | energy_category_id = new_values['data']['energy_category_id'] |
||
| 111 | |||
| 112 | if 'is_counted' not in new_values['data'].keys() or \ |
||
| 113 | not isinstance(new_values['data']['is_counted'], bool): |
||
| 114 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 115 | description='API.INVALID_IS_COUNTED_VALUE') |
||
| 116 | is_counted = new_values['data']['is_counted'] |
||
| 117 | |||
| 118 | if 'hourly_low_limit' not in new_values['data'].keys() or \ |
||
| 119 | not (isinstance(new_values['data']['hourly_low_limit'], float) or |
||
| 120 | isinstance(new_values['data']['hourly_low_limit'], int)): |
||
| 121 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 122 | description='API.INVALID_HOURLY_LOW_LIMIT_VALUE') |
||
| 123 | hourly_low_limit = new_values['data']['hourly_low_limit'] |
||
| 124 | |||
| 125 | if 'hourly_high_limit' not in new_values['data'].keys() or \ |
||
| 126 | not (isinstance(new_values['data']['hourly_high_limit'], float) or |
||
| 127 | isinstance(new_values['data']['hourly_high_limit'], int)): |
||
| 128 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 129 | description='API.INVALID_HOURLY_HIGH_LIMIT_VALUE') |
||
| 130 | hourly_high_limit = new_values['data']['hourly_high_limit'] |
||
| 131 | |||
| 132 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 133 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 134 | new_values['data']['cost_center_id'] <= 0: |
||
| 135 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 136 | description='API.INVALID_COST_CENTER_ID') |
||
| 137 | |||
| 138 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 139 | |||
| 140 | if 'energy_item_id' in new_values['data'].keys() and \ |
||
| 141 | new_values['data']['energy_item_id'] is not None: |
||
| 142 | if not isinstance(new_values['data']['energy_item_id'], int) or \ |
||
| 143 | new_values['data']['energy_item_id'] <= 0: |
||
| 144 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 145 | description='API.INVALID_ENERGY_ITEM_ID') |
||
| 146 | energy_item_id = new_values['data']['energy_item_id'] |
||
| 147 | else: |
||
| 148 | energy_item_id = None |
||
| 149 | |||
| 150 | if 'description' in new_values['data'].keys() and \ |
||
| 151 | new_values['data']['description'] is not None and \ |
||
| 152 | len(str(new_values['data']['description'])) > 0: |
||
| 153 | description = str.strip(new_values['data']['description']) |
||
| 154 | else: |
||
| 155 | description = None |
||
| 156 | |||
| 157 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 158 | cursor = cnx.cursor() |
||
| 159 | |||
| 160 | cursor.execute(" SELECT name " |
||
| 161 | " FROM tbl_offline_meters " |
||
| 162 | " WHERE name = %s ", (name,)) |
||
| 163 | if cursor.fetchone() is not None: |
||
| 164 | cursor.close() |
||
| 165 | cnx.disconnect() |
||
| 166 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 167 | description='API.OFFLINE_METER_NAME_IS_ALREADY_IN_USE') |
||
| 168 | |||
| 169 | cursor.execute(" SELECT name " |
||
| 170 | " FROM tbl_energy_categories " |
||
| 171 | " WHERE id = %s ", |
||
| 172 | (new_values['data']['energy_category_id'],)) |
||
| 173 | if cursor.fetchone() is None: |
||
| 174 | cursor.close() |
||
| 175 | cnx.disconnect() |
||
| 176 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 177 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
||
| 178 | View Code Duplication | if energy_item_id is not None: |
|
| 179 | cursor.execute(" SELECT name, energy_category_id " |
||
| 180 | " FROM tbl_energy_items " |
||
| 181 | " WHERE id = %s ", |
||
| 182 | (new_values['data']['energy_item_id'],)) |
||
| 183 | row = cursor.fetchone() |
||
| 184 | if row is None: |
||
| 185 | cursor.close() |
||
| 186 | cnx.disconnect() |
||
| 187 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 188 | description='API.ENERGY_ITEM_NOT_FOUND') |
||
| 189 | else: |
||
| 190 | if row[1] != energy_category_id: |
||
| 191 | cursor.close() |
||
| 192 | cnx.disconnect() |
||
| 193 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 194 | description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY') |
||
| 195 | |||
| 196 | cursor.execute(" SELECT name " |
||
| 197 | " FROM tbl_cost_centers " |
||
| 198 | " WHERE id = %s ", |
||
| 199 | (new_values['data']['cost_center_id'],)) |
||
| 200 | row = cursor.fetchone() |
||
| 201 | if row is None: |
||
| 202 | cursor.close() |
||
| 203 | cnx.disconnect() |
||
| 204 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 205 | description='API.COST_CENTER_NOT_FOUND') |
||
| 206 | |||
| 207 | add_values = (" INSERT INTO tbl_offline_meters " |
||
| 208 | " (name, uuid, energy_category_id, " |
||
| 209 | " is_counted, hourly_low_limit, hourly_high_limit, " |
||
| 210 | " cost_center_id, energy_item_id, description) " |
||
| 211 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 212 | cursor.execute(add_values, (name, |
||
| 213 | str(uuid.uuid4()), |
||
| 214 | energy_category_id, |
||
| 215 | is_counted, |
||
| 216 | hourly_low_limit, |
||
| 217 | hourly_high_limit, |
||
| 218 | cost_center_id, |
||
| 219 | energy_item_id, |
||
| 220 | description)) |
||
| 221 | new_id = cursor.lastrowid |
||
| 222 | cnx.commit() |
||
| 223 | cursor.close() |
||
| 224 | cnx.disconnect() |
||
| 225 | |||
| 226 | resp.status = falcon.HTTP_201 |
||
| 227 | resp.location = '/offlinemeters/' + str(new_id) |
||
| 228 | |||
| 229 | |||
| 230 | class OfflineMeterItem: |
||
| 231 | @staticmethod |
||
| 232 | def __init__(): |
||
| 233 | pass |
||
| 234 | |||
| 235 | @staticmethod |
||
| 236 | def on_options(req, resp, id_): |
||
| 237 | resp.status = falcon.HTTP_200 |
||
| 238 | |||
| 239 | @staticmethod |
||
| 240 | def on_get(req, resp, id_): |
||
| 241 | if not id_.isdigit() or int(id_) <= 0: |
||
| 242 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 243 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 244 | |||
| 245 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 246 | cursor = cnx.cursor(dictionary=True) |
||
| 247 | |||
| 248 | query = (" SELECT id, name, uuid " |
||
| 249 | " FROM tbl_energy_categories ") |
||
| 250 | cursor.execute(query) |
||
| 251 | rows_energy_categories = cursor.fetchall() |
||
| 252 | |||
| 253 | energy_category_dict = dict() |
||
| 254 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 255 | for row in rows_energy_categories: |
||
| 256 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 257 | "name": row['name'], |
||
| 258 | "uuid": row['uuid']} |
||
| 259 | |||
| 260 | query = (" SELECT id, name, uuid " |
||
| 261 | " FROM tbl_energy_items ") |
||
| 262 | cursor.execute(query) |
||
| 263 | rows_energy_items = cursor.fetchall() |
||
| 264 | |||
| 265 | energy_item_dict = dict() |
||
| 266 | if rows_energy_items is not None and len(rows_energy_items) > 0: |
||
| 267 | for row in rows_energy_items: |
||
| 268 | energy_item_dict[row['id']] = {"id": row['id'], |
||
| 269 | "name": row['name'], |
||
| 270 | "uuid": row['uuid']} |
||
| 271 | |||
| 272 | query = (" SELECT id, name, uuid " |
||
| 273 | " FROM tbl_cost_centers ") |
||
| 274 | cursor.execute(query) |
||
| 275 | rows_cost_centers = cursor.fetchall() |
||
| 276 | |||
| 277 | cost_center_dict = dict() |
||
| 278 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
||
| 279 | for row in rows_cost_centers: |
||
| 280 | cost_center_dict[row['id']] = {"id": row['id'], |
||
| 281 | "name": row['name'], |
||
| 282 | "uuid": row['uuid']} |
||
| 283 | |||
| 284 | query = (" SELECT id, name, uuid, energy_category_id, " |
||
| 285 | " is_counted, hourly_low_limit, hourly_high_limit, " |
||
| 286 | " energy_item_id, cost_center_id, description " |
||
| 287 | " FROM tbl_offline_meters " |
||
| 288 | " WHERE id = %s ") |
||
| 289 | cursor.execute(query, (id_,)) |
||
| 290 | row = cursor.fetchone() |
||
| 291 | cursor.close() |
||
| 292 | cnx.disconnect() |
||
| 293 | |||
| 294 | View Code Duplication | if row is None: |
|
| 295 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 296 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 297 | else: |
||
| 298 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 299 | energy_item = energy_item_dict.get(row['energy_item_id'], None) |
||
| 300 | cost_center = cost_center_dict.get(row['cost_center_id'], None) |
||
| 301 | meta_result = {"id": row['id'], |
||
| 302 | "name": row['name'], |
||
| 303 | "uuid": row['uuid'], |
||
| 304 | "energy_category": energy_category, |
||
| 305 | "is_counted": True if row['is_counted'] else False, |
||
| 306 | "hourly_low_limit": row['hourly_low_limit'], |
||
| 307 | "hourly_high_limit": row['hourly_high_limit'], |
||
| 308 | "energy_item": energy_item, |
||
| 309 | "cost_center": cost_center, |
||
| 310 | "description": row['description']} |
||
| 311 | |||
| 312 | resp.body = json.dumps(meta_result) |
||
| 313 | |||
| 314 | @staticmethod |
||
| 315 | def on_delete(req, resp, id_): |
||
| 316 | if not id_.isdigit() or int(id_) <= 0: |
||
| 317 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 318 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 319 | |||
| 320 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 321 | cursor = cnx.cursor() |
||
| 322 | |||
| 323 | cursor.execute(" SELECT uuid " |
||
| 324 | " FROM tbl_offline_meters " |
||
| 325 | " WHERE id = %s ", (id_,)) |
||
| 326 | row = cursor.fetchone() |
||
| 327 | if row is None: |
||
| 328 | cursor.close() |
||
| 329 | cnx.disconnect() |
||
| 330 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 331 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 332 | else: |
||
| 333 | offline_meter_uuid = row[0] |
||
| 334 | |||
| 335 | # check if this offline meter is being used by virtual meters |
||
| 336 | cursor.execute(" SELECT vm.name " |
||
| 337 | " FROM tbl_variables va, tbl_expressions ex, tbl_virtual_meters vm " |
||
| 338 | " WHERE va.meter_id = %s AND va.meter_type = 'offline_meter' AND va.expression_id = ex.id " |
||
| 339 | " AND ex.virtual_meter_id = vm.id ", |
||
| 340 | (id_,)) |
||
| 341 | row_virtual_meter = cursor.fetchone() |
||
| 342 | if row_virtual_meter is not None: |
||
| 343 | cursor.close() |
||
| 344 | cnx.disconnect() |
||
| 345 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 346 | title='API.BAD_REQUEST', |
||
| 347 | description='API.THIS_OFFLINE_METER_IS_BEING_USED_BY_A_VIRTUAL_METER') |
||
| 348 | |||
| 349 | # check relationship with spaces |
||
| 350 | cursor.execute(" SELECT id " |
||
| 351 | " FROM tbl_spaces_offline_meters " |
||
| 352 | " WHERE offline_meter_id = %s ", (id_,)) |
||
| 353 | rows_companies = cursor.fetchall() |
||
| 354 | if rows_companies is not None and len(rows_companies) > 0: |
||
| 355 | cursor.close() |
||
| 356 | cnx.disconnect() |
||
| 357 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 358 | title='API.BAD_REQUEST', |
||
| 359 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
| 360 | |||
| 361 | # check relation with combined equipments |
||
| 362 | cursor.execute(" SELECT combined_equipment_id " |
||
| 363 | " FROM tbl_combined_equipments_offline_meters " |
||
| 364 | " WHERE offline_meter_id = %s ", |
||
| 365 | (id_,)) |
||
| 366 | rows_combined_equipments = cursor.fetchall() |
||
| 367 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
| 368 | cursor.close() |
||
| 369 | cnx.disconnect() |
||
| 370 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 371 | title='API.BAD_REQUEST', |
||
| 372 | description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS') |
||
| 373 | |||
| 374 | # check relation with combined equipment parameters |
||
| 375 | cursor.execute(" SELECT combined_equipment_id " |
||
| 376 | " FROM tbl_combined_equipments_parameters " |
||
| 377 | " WHERE numerator_meter_uuid = %s OR denominator_meter_uuid = %s", |
||
| 378 | (offline_meter_uuid, offline_meter_uuid,)) |
||
| 379 | rows_combined_equipments = cursor.fetchall() |
||
| 380 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
| 381 | cursor.close() |
||
| 382 | cnx.disconnect() |
||
| 383 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 384 | title='API.BAD_REQUEST', |
||
| 385 | description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENT_PARAMETERS') |
||
| 386 | |||
| 387 | # check relations with tenants |
||
| 388 | cursor.execute(" SELECT tenant_id " |
||
| 389 | " FROM tbl_tenants_offline_meters " |
||
| 390 | " WHERE offline_meter_id = %s ", (id_,)) |
||
| 391 | rows_tenants = cursor.fetchall() |
||
| 392 | if rows_tenants is not None and len(rows_tenants) > 0: |
||
| 393 | cursor.close() |
||
| 394 | cnx.disconnect() |
||
| 395 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 396 | title='API.BAD_REQUEST', |
||
| 397 | description='API.THERE_IS_RELATION_WITH_TENANTS') |
||
| 398 | |||
| 399 | # check relations with stores |
||
| 400 | cursor.execute(" SELECT store_id " |
||
| 401 | " FROM tbl_stores_offline_meters " |
||
| 402 | " WHERE offline_meter_id = %s ", (id_,)) |
||
| 403 | rows_stores = cursor.fetchall() |
||
| 404 | if rows_stores is not None and len(rows_stores) > 0: |
||
| 405 | cursor.close() |
||
| 406 | cnx.disconnect() |
||
| 407 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 408 | title='API.BAD_REQUEST', |
||
| 409 | description='API.THERE_IS_RELATION_WITH_STORES') |
||
| 410 | |||
| 411 | # check relations with shopfloors |
||
| 412 | cursor.execute(" SELECT shopfloor_id " |
||
| 413 | " FROM tbl_shopfloors_offline_meters " |
||
| 414 | " WHERE offline_meter_id = %s ", (id_,)) |
||
| 415 | rows_shopfloors = cursor.fetchall() |
||
| 416 | if rows_shopfloors is not None and len(rows_shopfloors) > 0: |
||
| 417 | cursor.close() |
||
| 418 | cnx.disconnect() |
||
| 419 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 420 | title='API.BAD_REQUEST', |
||
| 421 | description='API.THERE_IS_RELATION_WITH_SHOPFLOORS') |
||
| 422 | |||
| 423 | # check relations with equipments |
||
| 424 | cursor.execute(" SELECT equipment_id " |
||
| 425 | " FROM tbl_equipments_offline_meters " |
||
| 426 | " WHERE offline_meter_id = %s ", (id_,)) |
||
| 427 | rows_equipments = cursor.fetchall() |
||
| 428 | if rows_equipments is not None and len(rows_equipments) > 0: |
||
| 429 | cursor.close() |
||
| 430 | cnx.disconnect() |
||
| 431 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 432 | title='API.BAD_REQUEST', |
||
| 433 | description='API.THERE_IS_RELATIONSHIP_WITH_EQUIPMENTS') |
||
| 434 | |||
| 435 | # check relation with equipment parameters |
||
| 436 | cursor.execute(" SELECT equipment_id " |
||
| 437 | " FROM tbl_equipments_parameters " |
||
| 438 | " WHERE numerator_meter_uuid = %s OR denominator_meter_uuid = %s", |
||
| 439 | (offline_meter_uuid, offline_meter_uuid,)) |
||
| 440 | rows_equipments = cursor.fetchall() |
||
| 441 | if rows_equipments is not None and len(rows_equipments) > 0: |
||
| 442 | cursor.close() |
||
| 443 | cnx.disconnect() |
||
| 444 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 445 | title='API.BAD_REQUEST', |
||
| 446 | description='API.THERE_IS_RELATION_WITH_EQUIPMENT_PARAMETERS') |
||
| 447 | |||
| 448 | # check relation with energy flow diagram links |
||
| 449 | cursor.execute(" SELECT id " |
||
| 450 | " FROM tbl_energy_flow_diagrams_links " |
||
| 451 | " WHERE meter_uuid = %s ", (offline_meter_uuid,)) |
||
| 452 | rows_links = cursor.fetchall() |
||
| 453 | if rows_links is not None and len(rows_links) > 0: |
||
| 454 | cursor.close() |
||
| 455 | cnx.disconnect() |
||
| 456 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 457 | title='API.BAD_REQUEST', |
||
| 458 | description='API.THERE_IS_RELATION_WITH_ENERGY_FLOW_DIAGRAM_LINKS') |
||
| 459 | |||
| 460 | cursor.execute(" DELETE FROM tbl_offline_meters WHERE id = %s ", (id_,)) |
||
| 461 | cnx.commit() |
||
| 462 | |||
| 463 | cursor.close() |
||
| 464 | cnx.disconnect() |
||
| 465 | |||
| 466 | resp.status = falcon.HTTP_204 |
||
| 467 | |||
| 468 | @staticmethod |
||
| 469 | def on_put(req, resp, id_): |
||
| 470 | """Handles PUT requests""" |
||
| 471 | try: |
||
| 472 | raw_json = req.stream.read().decode('utf-8') |
||
| 473 | except Exception as ex: |
||
| 474 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 475 | |||
| 476 | if not id_.isdigit() or int(id_) <= 0: |
||
| 477 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 478 | description='API.INVALID_OFFLINE_METER_ID') |
||
| 479 | |||
| 480 | new_values = json.loads(raw_json) |
||
| 481 | |||
| 482 | if 'name' not in new_values['data'].keys() or \ |
||
| 483 | not isinstance(new_values['data']['name'], str) or \ |
||
| 484 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 485 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 486 | description='API.INVALID_OFFLINE_METER_NAME') |
||
| 487 | name = str.strip(new_values['data']['name']) |
||
| 488 | |||
| 489 | if 'energy_category_id' not in new_values['data'].keys() or \ |
||
| 490 | not isinstance(new_values['data']['energy_category_id'], int) or \ |
||
| 491 | new_values['data']['energy_category_id'] <= 0: |
||
| 492 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 493 | description='API.INVALID_ENERGY_CATEGORY_ID') |
||
| 494 | energy_category_id = new_values['data']['energy_category_id'] |
||
| 495 | |||
| 496 | if 'is_counted' not in new_values['data'].keys() or \ |
||
| 497 | not isinstance(new_values['data']['is_counted'], bool): |
||
| 498 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 499 | description='API.INVALID_IS_COUNTED_VALUE') |
||
| 500 | is_counted = new_values['data']['is_counted'] |
||
| 501 | |||
| 502 | if 'hourly_low_limit' not in new_values['data'].keys() or \ |
||
| 503 | not (isinstance(new_values['data']['hourly_low_limit'], float) or |
||
| 504 | isinstance(new_values['data']['hourly_low_limit'], int)): |
||
| 505 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 506 | description='API.INVALID_HOURLY_LOW_LIMIT_VALUE') |
||
| 507 | hourly_low_limit = new_values['data']['hourly_low_limit'] |
||
| 508 | |||
| 509 | if 'hourly_high_limit' not in new_values['data'].keys() or \ |
||
| 510 | not (isinstance(new_values['data']['hourly_high_limit'], float) or |
||
| 511 | isinstance(new_values['data']['hourly_high_limit'], int)): |
||
| 512 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 513 | description='API.INVALID_HOURLY_HIGH_LIMIT_VALUE') |
||
| 514 | hourly_high_limit = new_values['data']['hourly_high_limit'] |
||
| 515 | |||
| 516 | if 'cost_center_id' not in new_values['data'].keys() or \ |
||
| 517 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
||
| 518 | new_values['data']['cost_center_id'] <= 0: |
||
| 519 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 520 | description='API.INVALID_COST_CENTER_ID') |
||
| 521 | |||
| 522 | cost_center_id = new_values['data']['cost_center_id'] |
||
| 523 | |||
| 524 | if 'energy_item_id' in new_values['data'].keys() and \ |
||
| 525 | new_values['data']['energy_item_id'] is not None: |
||
| 526 | if not isinstance(new_values['data']['energy_item_id'], int) or \ |
||
| 527 | new_values['data']['energy_item_id'] <= 0: |
||
| 528 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 529 | description='API.INVALID_ENERGY_ITEM_ID') |
||
| 530 | energy_item_id = new_values['data']['energy_item_id'] |
||
| 531 | else: |
||
| 532 | energy_item_id = None |
||
| 533 | |||
| 534 | if 'description' in new_values['data'].keys() and \ |
||
| 535 | new_values['data']['description'] is not None and \ |
||
| 536 | len(str(new_values['data']['description'])) > 0: |
||
| 537 | description = str.strip(new_values['data']['description']) |
||
| 538 | else: |
||
| 539 | description = None |
||
| 540 | |||
| 541 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 542 | cursor = cnx.cursor() |
||
| 543 | |||
| 544 | cursor.execute(" SELECT name " |
||
| 545 | " FROM tbl_offline_meters " |
||
| 546 | " WHERE id = %s ", (id_,)) |
||
| 547 | if cursor.fetchone() is None: |
||
| 548 | cursor.close() |
||
| 549 | cnx.disconnect() |
||
| 550 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 551 | description='API.OFFLINE_METER_NOT_FOUND') |
||
| 552 | |||
| 553 | cursor.execute(" SELECT name " |
||
| 554 | " FROM tbl_offline_meters " |
||
| 555 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 556 | if cursor.fetchone() is not None: |
||
| 557 | cursor.close() |
||
| 558 | cnx.disconnect() |
||
| 559 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 560 | description='API.OFFLINE_METER_NAME_IS_ALREADY_IN_USE') |
||
| 561 | |||
| 562 | cursor.execute(" SELECT name " |
||
| 563 | " FROM tbl_energy_categories " |
||
| 564 | " WHERE id = %s ", |
||
| 565 | (new_values['data']['energy_category_id'],)) |
||
| 566 | if cursor.fetchone() is None: |
||
| 567 | cursor.close() |
||
| 568 | cnx.disconnect() |
||
| 569 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 570 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
||
| 571 | |||
| 572 | cursor.execute(" SELECT name " |
||
| 573 | " FROM tbl_cost_centers " |
||
| 574 | " WHERE id = %s ", |
||
| 575 | (new_values['data']['cost_center_id'],)) |
||
| 576 | row = cursor.fetchone() |
||
| 577 | if row is None: |
||
| 578 | cursor.close() |
||
| 579 | cnx.disconnect() |
||
| 580 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 581 | description='API.COST_CENTER_NOT_FOUND') |
||
| 582 | |||
| 583 | View Code Duplication | if energy_item_id is not None: |
|
| 584 | cursor.execute(" SELECT name, energy_category_id " |
||
| 585 | " FROM tbl_energy_items " |
||
| 586 | " WHERE id = %s ", |
||
| 587 | (new_values['data']['energy_item_id'],)) |
||
| 588 | row = cursor.fetchone() |
||
| 589 | if row is None: |
||
| 590 | cursor.close() |
||
| 591 | cnx.disconnect() |
||
| 592 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 593 | description='API.ENERGY_ITEM_NOT_FOUND') |
||
| 594 | else: |
||
| 595 | if row[1] != energy_category_id: |
||
| 596 | cursor.close() |
||
| 597 | cnx.disconnect() |
||
| 598 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 599 | description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY') |
||
| 600 | |||
| 601 | update_row = (" UPDATE tbl_offline_meters " |
||
| 602 | " SET name = %s, energy_category_id = %s," |
||
| 603 | " is_counted = %s, hourly_low_limit = %s, hourly_high_limit = %s, " |
||
| 604 | " cost_center_id = %s, energy_item_id = %s, description = %s " |
||
| 605 | " WHERE id = %s ") |
||
| 606 | cursor.execute(update_row, (name, |
||
| 607 | energy_category_id, |
||
| 608 | is_counted, |
||
| 609 | hourly_low_limit, |
||
| 610 | hourly_high_limit, |
||
| 611 | cost_center_id, |
||
| 612 | energy_item_id, |
||
| 613 | description, |
||
| 614 | id_,)) |
||
| 615 | cnx.commit() |
||
| 616 | |||
| 617 | cursor.close() |
||
| 618 | cnx.disconnect() |
||
| 619 | |||
| 620 | resp.status = falcon.HTTP_200 |
||
| 621 | |||
| 622 |