| Total Complexity | 86 |
| Total Lines | 517 |
| Duplicated Lines | 11.03 % |
| 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 costcenter 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 CostCenterCollection: |
||
| 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() |
||
| 21 | |||
| 22 | query = (" SELECT id, name, uuid, external_id " |
||
| 23 | " FROM tbl_cost_centers " |
||
| 24 | " ORDER BY id") |
||
| 25 | cursor.execute(query) |
||
| 26 | rows = cursor.fetchall() |
||
| 27 | cursor.close() |
||
| 28 | cnx.disconnect() |
||
| 29 | |||
| 30 | result = list() |
||
| 31 | if rows is not None and len(rows) > 0: |
||
| 32 | for row in rows: |
||
| 33 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2], "external_id": row[3]} |
||
| 34 | result.append(meta_result) |
||
| 35 | |||
| 36 | resp.body = json.dumps(result) |
||
| 37 | |||
| 38 | @staticmethod |
||
| 39 | def on_post(req, resp): |
||
| 40 | """Handles POST requests""" |
||
| 41 | try: |
||
| 42 | raw_json = req.stream.read().decode('utf-8') |
||
| 43 | |||
| 44 | except Exception as ex: |
||
| 45 | raise falcon.HTTPError(falcon.HTTP_400, 'API.ERROR', ex) |
||
| 46 | |||
| 47 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 48 | |||
| 49 | if 'name' not in new_values['data'].keys() or len(new_values['data']['name']) <= 0: |
||
| 50 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 51 | description='API.INVALID_NAME_VALUE') |
||
| 52 | name = str.strip(new_values['data']['name']) |
||
| 53 | |||
| 54 | if 'external_id' in new_values['data'].keys() and \ |
||
| 55 | new_values['data']['external_id'] is not None and \ |
||
| 56 | len(str(new_values['data']['external_id'])) > 0: |
||
| 57 | external_id = str.strip(new_values['data']['external_id']) |
||
| 58 | else: |
||
| 59 | external_id = None |
||
| 60 | |||
| 61 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 62 | cursor = cnx.cursor() |
||
| 63 | |||
| 64 | cursor.execute(" SELECT name " |
||
| 65 | " FROM tbl_cost_centers " |
||
| 66 | " WHERE name = %s ", (name, )) |
||
| 67 | if cursor.fetchone() is not None: |
||
| 68 | cursor.close() |
||
| 69 | cnx.disconnect() |
||
| 70 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 71 | title='API.BAD_REQUEST', |
||
| 72 | description='API.COST_CENTER_NAME_EXISTS') |
||
| 73 | if external_id is not None: |
||
| 74 | cursor.execute(" SELECT name " |
||
| 75 | " FROM tbl_cost_centers " |
||
| 76 | " WHERE external_id = %s ", (external_id, )) |
||
| 77 | if cursor.fetchone() is not None: |
||
| 78 | cursor.close() |
||
| 79 | cnx.disconnect() |
||
| 80 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 81 | description='API.COST_CENTER_EXTERNAL_ID_EXISTS') |
||
| 82 | |||
| 83 | add_row = (" INSERT INTO tbl_cost_centers " |
||
| 84 | " (name, uuid, external_id) " |
||
| 85 | " VALUES (%s, %s, %s) ") |
||
| 86 | cursor.execute(add_row, (name, |
||
| 87 | str(uuid.uuid4()), |
||
| 88 | external_id,)) |
||
| 89 | new_id = cursor.lastrowid |
||
| 90 | cnx.commit() |
||
| 91 | cursor.close() |
||
| 92 | cnx.disconnect() |
||
| 93 | |||
| 94 | resp.status = falcon.HTTP_201 |
||
| 95 | resp.location = '/costcenters/' + str(new_id) |
||
| 96 | |||
| 97 | |||
| 98 | class CostCenterItem: |
||
| 99 | @staticmethod |
||
| 100 | def __init__(): |
||
| 101 | pass |
||
| 102 | |||
| 103 | @staticmethod |
||
| 104 | def on_options(req, resp, id_): |
||
| 105 | resp.status = falcon.HTTP_200 |
||
| 106 | |||
| 107 | @staticmethod |
||
| 108 | def on_get(req, resp, id_): |
||
| 109 | if not id_.isdigit() or int(id_) <= 0: |
||
| 110 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 111 | description='API.INVALID_COST_CENTER_ID') |
||
| 112 | |||
| 113 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 114 | cursor = cnx.cursor() |
||
| 115 | |||
| 116 | query = (" SELECT id, name, uuid, external_id " |
||
| 117 | " FROM tbl_cost_centers " |
||
| 118 | " WHERE id = %s ") |
||
| 119 | cursor.execute(query, (id_,)) |
||
| 120 | row = cursor.fetchone() |
||
| 121 | cursor.close() |
||
| 122 | cnx.disconnect() |
||
| 123 | |||
| 124 | if row is None: |
||
| 125 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 126 | description='API.COST_CENTER_NOT_FOUND') |
||
| 127 | |||
| 128 | result = {"id": row[0], "name": row[1], "uuid": row[2], "external_id": row[3]} |
||
| 129 | resp.body = json.dumps(result) |
||
| 130 | |||
| 131 | @staticmethod |
||
| 132 | def on_delete(req, resp, id_): |
||
| 133 | if not id_.isdigit() or int(id_) <= 0: |
||
| 134 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 135 | description='API.INVALID_COST_CENTER_ID') |
||
| 136 | |||
| 137 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 138 | cursor = cnx.cursor() |
||
| 139 | |||
| 140 | cursor.execute(" SELECT name " |
||
| 141 | " FROM tbl_cost_centers " |
||
| 142 | " WHERE id = %s ", (id_,)) |
||
| 143 | if cursor.fetchone() is None: |
||
| 144 | cursor.close() |
||
| 145 | cnx.disconnect() |
||
| 146 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 147 | description='API.COST_CENTER_NOT_FOUND') |
||
| 148 | |||
| 149 | # check relation with equipments |
||
| 150 | cursor.execute(" SELECT id " |
||
| 151 | " FROM tbl_equipments " |
||
| 152 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 153 | rows_equipments = cursor.fetchall() |
||
| 154 | if rows_equipments is not None and len(rows_equipments) > 0: |
||
| 155 | cursor.close() |
||
| 156 | cnx.disconnect() |
||
| 157 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 158 | title='API.BAD_REQUEST', |
||
| 159 | description='API.THERE_IS_RELATION_WITH_EQUIPMENTS') |
||
| 160 | |||
| 161 | # check relation with combined equipments |
||
| 162 | cursor.execute(" SELECT id " |
||
| 163 | " FROM tbl_combined_equipments " |
||
| 164 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 165 | rows_combined_equipments = cursor.fetchall() |
||
| 166 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
| 167 | cursor.close() |
||
| 168 | cnx.disconnect() |
||
| 169 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 170 | title='API.BAD_REQUEST', |
||
| 171 | description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS') |
||
| 172 | |||
| 173 | # check relation with tariffs |
||
| 174 | cursor.execute(" SELECT id " |
||
| 175 | " FROM tbl_cost_centers_tariffs " |
||
| 176 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 177 | rows_tariffs = cursor.fetchall() |
||
| 178 | if rows_tariffs is not None and len(rows_tariffs) > 0: |
||
| 179 | cursor.close() |
||
| 180 | cnx.disconnect() |
||
| 181 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 182 | title='API.BAD_REQUEST', |
||
| 183 | description='API.THERE_IS_RELATION_WITH_TARIFFS') |
||
| 184 | |||
| 185 | # check relation with meters |
||
| 186 | cursor.execute(" SELECT id " |
||
| 187 | " FROM tbl_meters " |
||
| 188 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 189 | rows_meters = cursor.fetchall() |
||
| 190 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 191 | cursor.close() |
||
| 192 | cnx.disconnect() |
||
| 193 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 194 | title='API.BAD_REQUEST', |
||
| 195 | description='API.THERE_IS_RELATION_WITH_METERS') |
||
| 196 | |||
| 197 | # check relation with offline meters |
||
| 198 | cursor.execute(" SELECT id " |
||
| 199 | " FROM tbl_offline_meters " |
||
| 200 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 201 | rows_offline_meters = cursor.fetchall() |
||
| 202 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 203 | cursor.close() |
||
| 204 | cnx.disconnect() |
||
| 205 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 206 | title='API.BAD_REQUEST', |
||
| 207 | description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS') |
||
| 208 | |||
| 209 | # check relation with virtual meters |
||
| 210 | cursor.execute(" SELECT id " |
||
| 211 | " FROM tbl_virtual_meters " |
||
| 212 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 213 | rows_virtual_meters = cursor.fetchall() |
||
| 214 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 215 | cursor.close() |
||
| 216 | cnx.disconnect() |
||
| 217 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 218 | title='API.BAD_REQUEST', |
||
| 219 | description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS') |
||
| 220 | |||
| 221 | # check relation with tenants |
||
| 222 | cursor.execute(" SELECT id " |
||
| 223 | " FROM tbl_tenants " |
||
| 224 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 225 | rows_tenants = cursor.fetchall() |
||
| 226 | if rows_tenants is not None and len(rows_tenants) > 0: |
||
| 227 | cursor.close() |
||
| 228 | cnx.disconnect() |
||
| 229 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 230 | title='API.BAD_REQUEST', |
||
| 231 | description='API.THERE_IS_RELATION_WITH_TENANTS') |
||
| 232 | |||
| 233 | # check relation with stores |
||
| 234 | cursor.execute(" SELECT id " |
||
| 235 | " FROM tbl_stores " |
||
| 236 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 237 | rows_stores = cursor.fetchall() |
||
| 238 | if rows_stores is not None and len(rows_stores) > 0: |
||
| 239 | cursor.close() |
||
| 240 | cnx.disconnect() |
||
| 241 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 242 | title='API.BAD_REQUEST', |
||
| 243 | description='API.THERE_IS_RELATION_WITH_STORES') |
||
| 244 | |||
| 245 | # check relation with spaces |
||
| 246 | cursor.execute(" SELECT id " |
||
| 247 | " FROM tbl_spaces " |
||
| 248 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 249 | rows_factories = cursor.fetchall() |
||
| 250 | if rows_factories is not None and len(rows_factories) > 0: |
||
| 251 | cursor.close() |
||
| 252 | cnx.disconnect() |
||
| 253 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 254 | title='API.BAD_REQUEST', |
||
| 255 | description='API.THERE_IS_RELATIONSHIP_WITH_SPACES') |
||
| 256 | |||
| 257 | # check relation with shopfloors |
||
| 258 | cursor.execute(" SELECT id " |
||
| 259 | " FROM tbl_shopfloors " |
||
| 260 | " WHERE cost_center_id = %s ", (id_,)) |
||
| 261 | rows_shopfloors = cursor.fetchall() |
||
| 262 | if rows_shopfloors is not None and len(rows_shopfloors) > 0: |
||
| 263 | cursor.close() |
||
| 264 | cnx.disconnect() |
||
| 265 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 266 | title='API.BAD_REQUEST', |
||
| 267 | description='API.THERE_IS_RELATION_WITH_SHOPFLOORS') |
||
| 268 | |||
| 269 | cursor.execute(" DELETE FROM tbl_cost_centers WHERE id = %s ", (id_,)) |
||
| 270 | cnx.commit() |
||
| 271 | |||
| 272 | cursor.close() |
||
| 273 | cnx.disconnect() |
||
| 274 | resp.status = falcon.HTTP_204 |
||
| 275 | |||
| 276 | @staticmethod |
||
| 277 | def on_put(req, resp, id_): |
||
| 278 | """Handles PUT requests""" |
||
| 279 | try: |
||
| 280 | raw_json = req.stream.read().decode('utf-8') |
||
| 281 | except Exception as ex: |
||
| 282 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 283 | |||
| 284 | if not id_.isdigit() or int(id_) <= 0: |
||
| 285 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 286 | description='API.INVALID_COST_CENTER_ID') |
||
| 287 | |||
| 288 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 289 | |||
| 290 | if 'name' not in new_values['data'].keys() or len(new_values['data']['name']) <= 0: |
||
| 291 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 292 | description='API.INVALID_NAME_VALUE') |
||
| 293 | name = str.strip(new_values['data']['name']) |
||
| 294 | |||
| 295 | if 'external_id' in new_values['data'].keys() and \ |
||
| 296 | new_values['data']['external_id'] is not None and \ |
||
| 297 | len(str(new_values['data']['external_id'])) > 0: |
||
| 298 | external_id = str.strip(new_values['data']['external_id']) |
||
| 299 | else: |
||
| 300 | external_id = None |
||
| 301 | |||
| 302 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 303 | cursor = cnx.cursor() |
||
| 304 | |||
| 305 | cursor.execute(" SELECT name " |
||
| 306 | " FROM tbl_cost_centers " |
||
| 307 | " WHERE id = %s ", (id_,)) |
||
| 308 | if cursor.fetchone() is None: |
||
| 309 | cursor.close() |
||
| 310 | cnx.disconnect() |
||
| 311 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 312 | description='API.COST_CENTER_NOT_FOUND') |
||
| 313 | |||
| 314 | cursor.execute(" SELECT name " |
||
| 315 | " FROM tbl_cost_centers " |
||
| 316 | " WHERE name = %s AND id != %s ", |
||
| 317 | (name, id_, )) |
||
| 318 | if cursor.fetchone() is not None: |
||
| 319 | cursor.close() |
||
| 320 | cnx.disconnect() |
||
| 321 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 322 | title='API.BAD_REQUEST', |
||
| 323 | description='API.COST_CENTER_NAME_EXISTS') |
||
| 324 | if external_id is not None: |
||
| 325 | cursor.execute(" SELECT name " |
||
| 326 | " FROM tbl_cost_centers " |
||
| 327 | " WHERE external_id = %s AND id != %s ", |
||
| 328 | (external_id, id_, )) |
||
| 329 | if cursor.fetchone() is not None: |
||
| 330 | cursor.close() |
||
| 331 | cnx.disconnect() |
||
| 332 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 333 | title='API.BAD_REQUEST', |
||
| 334 | description='API.COST_CENTER_EXTERNAL_ID_EXISTS') |
||
| 335 | |||
| 336 | cursor.execute(" SELECT name " |
||
| 337 | " FROM tbl_cost_centers " |
||
| 338 | " WHERE id = %s ", (id_,)) |
||
| 339 | if cursor.fetchone() is None: |
||
| 340 | cursor.close() |
||
| 341 | cnx.disconnect() |
||
| 342 | raise falcon.HTTPError(falcon.HTTP_404, |
||
| 343 | title='API.NOT_FOUND', |
||
| 344 | description='API.COST_CENTER_NOT_FOUND') |
||
| 345 | |||
| 346 | update_row = (" UPDATE tbl_cost_centers " |
||
| 347 | " SET name = %s, external_id = %s " |
||
| 348 | " WHERE id = %s ") |
||
| 349 | |||
| 350 | cursor.execute(update_row, (name, |
||
| 351 | external_id, |
||
| 352 | id_,)) |
||
| 353 | cnx.commit() |
||
| 354 | |||
| 355 | cursor.close() |
||
| 356 | cnx.disconnect() |
||
| 357 | |||
| 358 | resp.status = falcon.HTTP_200 |
||
| 359 | |||
| 360 | |||
| 361 | class CostCenterTariffCollection: |
||
| 362 | @staticmethod |
||
| 363 | def __init__(): |
||
| 364 | pass |
||
| 365 | |||
| 366 | @staticmethod |
||
| 367 | def on_options(req, resp, id_): |
||
| 368 | resp.status = falcon.HTTP_200 |
||
| 369 | |||
| 370 | @staticmethod |
||
| 371 | def on_get(req, resp, id_): |
||
| 372 | if not id_.isdigit() or int(id_) <= 0: |
||
| 373 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 374 | description='API.INVALID_COST_CENTER_ID') |
||
| 375 | |||
| 376 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 377 | cursor = cnx.cursor() |
||
| 378 | |||
| 379 | query = (" SELECT t.id, t.name, t.uuid, " |
||
| 380 | " t.tariff_type, t.unit_of_price " |
||
| 381 | " FROM tbl_tariffs t, tbl_cost_centers_tariffs ct " |
||
| 382 | " WHERE t.id = ct.tariff_id AND ct.cost_center_id = %s " |
||
| 383 | " ORDER BY t.name ") |
||
| 384 | cursor.execute(query, (id_,)) |
||
| 385 | rows = cursor.fetchall() |
||
| 386 | |||
| 387 | cursor.close() |
||
| 388 | cnx.disconnect() |
||
| 389 | |||
| 390 | result = list() |
||
| 391 | if rows is not None and len(rows) > 0: |
||
| 392 | for row in rows: |
||
| 393 | meta_result = {"id": row[0], |
||
| 394 | "name": row[1], |
||
| 395 | "uuid": row[2], |
||
| 396 | "tariff_type": row[3], |
||
| 397 | "unit_of_price": row[4]} |
||
| 398 | result.append(meta_result) |
||
| 399 | |||
| 400 | resp.body = json.dumps(result) |
||
| 401 | |||
| 402 | @staticmethod |
||
| 403 | def on_post(req, resp, id_): |
||
| 404 | """Handles POST requests""" |
||
| 405 | try: |
||
| 406 | raw_json = req.stream.read().decode('utf-8') |
||
| 407 | except Exception as ex: |
||
| 408 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 409 | |||
| 410 | if not id_.isdigit() or int(id_) <= 0: |
||
| 411 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 412 | description='API.INVALID_COST_CENTER_ID') |
||
| 413 | |||
| 414 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 415 | |||
| 416 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 417 | cursor = cnx.cursor() |
||
| 418 | |||
| 419 | cursor.execute(" SELECT name " |
||
| 420 | " FROM tbl_cost_centers " |
||
| 421 | " WHERE id = %s ", (id_,)) |
||
| 422 | if cursor.fetchone() is None: |
||
| 423 | cursor.close() |
||
| 424 | cnx.disconnect() |
||
| 425 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 426 | description='API.COST_CENTER_NOT_FOUND') |
||
| 427 | |||
| 428 | cursor.execute(" SELECT name " |
||
| 429 | " FROM tbl_tariffs " |
||
| 430 | " WHERE id = %s ", (new_values['data']['tariff_id'],)) |
||
| 431 | if cursor.fetchone() is None: |
||
| 432 | cursor.close() |
||
| 433 | cnx.disconnect() |
||
| 434 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 435 | description='API.TARIFF_NOT_FOUND') |
||
| 436 | |||
| 437 | cursor.execute(" SELECT id " |
||
| 438 | " FROM tbl_cost_centers_tariffs " |
||
| 439 | " WHERE cost_center_id = %s AND tariff_id = %s ", (id_, new_values['data']['tariff_id'])) |
||
| 440 | rows = cursor.fetchall() |
||
| 441 | if rows is not None and len(rows) > 0: |
||
| 442 | cursor.close() |
||
| 443 | cnx.disconnect() |
||
| 444 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 445 | description='API.TARIFF_ALREADY_ASSOCIATED_WITH_COST_CENTER') |
||
| 446 | |||
| 447 | add_row = (" INSERT INTO tbl_cost_centers_tariffs " |
||
| 448 | " (cost_center_id, tariff_id) " |
||
| 449 | " VALUES (%s, %s) ") |
||
| 450 | cursor.execute(add_row, (id_, new_values['data']['tariff_id'],)) |
||
| 451 | cnx.commit() |
||
| 452 | |||
| 453 | cursor.close() |
||
| 454 | cnx.disconnect() |
||
| 455 | |||
| 456 | resp.status = falcon.HTTP_201 |
||
| 457 | resp.location = '/costcenters/' + str(id_) + '/tariffs/' + str(new_values['data']['tariff_id']) |
||
| 458 | |||
| 459 | |||
| 460 | View Code Duplication | class CostCenterTariffItem: |
|
|
|
|||
| 461 | @staticmethod |
||
| 462 | def __init__(): |
||
| 463 | pass |
||
| 464 | |||
| 465 | @staticmethod |
||
| 466 | def on_options(req, resp, id_, tid): |
||
| 467 | resp.status = falcon.HTTP_200 |
||
| 468 | |||
| 469 | @staticmethod |
||
| 470 | def on_delete(req, resp, id_, tid): |
||
| 471 | if not id_.isdigit() or int(id_) <= 0: |
||
| 472 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 473 | description='API.INVALID_COST_CENTER_ID') |
||
| 474 | |||
| 475 | if not tid.isdigit() or int(tid) <= 0: |
||
| 476 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 477 | description='API.INVALID_TARIFF_ID') |
||
| 478 | |||
| 479 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 480 | cursor = cnx.cursor() |
||
| 481 | |||
| 482 | cursor.execute(" SELECT name " |
||
| 483 | " FROM tbl_cost_centers " |
||
| 484 | " WHERE id = %s ", (id_,)) |
||
| 485 | if cursor.fetchone() is None: |
||
| 486 | cursor.close() |
||
| 487 | cnx.disconnect() |
||
| 488 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 489 | description='API.COST_CENTER_NOT_FOUND') |
||
| 490 | |||
| 491 | cursor.execute(" SELECT name " |
||
| 492 | " FROM tbl_tariffs " |
||
| 493 | " WHERE id = %s ", (tid,)) |
||
| 494 | if cursor.fetchone() is None: |
||
| 495 | cursor.close() |
||
| 496 | cnx.disconnect() |
||
| 497 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 498 | description='API.TARIFF_NOT_FOUND') |
||
| 499 | |||
| 500 | cursor.execute(" SELECT id " |
||
| 501 | " FROM tbl_cost_centers_tariffs " |
||
| 502 | " WHERE cost_center_id = %s AND tariff_id = %s ", (id_, tid)) |
||
| 503 | if cursor.fetchone() is None: |
||
| 504 | cursor.close() |
||
| 505 | cnx.disconnect() |
||
| 506 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 507 | description='API.TARIFF_IS_NOT_ASSOCIATED_WITH_COST_CENTER') |
||
| 508 | |||
| 509 | cursor.execute(" DELETE FROM tbl_cost_centers_tariffs " |
||
| 510 | " WHERE cost_center_id = %s AND tariff_id = %s ", (id_, tid)) |
||
| 511 | cnx.commit() |
||
| 512 | |||
| 513 | cursor.close() |
||
| 514 | cnx.disconnect() |
||
| 515 | |||
| 516 | resp.status = falcon.HTTP_204 |
||
| 517 |