Code Duplication    Length = 54-54 lines in 2 locations

myems-api/core/point.py 2 locations

@@ 1083-1136 (lines=54) @@
1080
    def on_options(req, resp, id_):
1081
        resp.status = falcon.HTTP_200
1082
1083
    @staticmethod
1084
    def on_get(req, resp, id_):
1085
        """Handles GET requests"""
1086
        admin_control(req)
1087
        if not id_.isdigit() or int(id_) <= 0:
1088
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1089
                                   description='API.INVALID_POINT_ID')
1090
1091
        cnx = mysql.connector.connect(**config.myems_system_db)
1092
        cursor = cnx.cursor()
1093
1094
        query = (" SELECT id, name, uuid "
1095
                 " FROM tbl_data_sources ")
1096
        cursor.execute(query)
1097
        rows_data_sources = cursor.fetchall()
1098
1099
        data_source_dict = dict()
1100
        if rows_data_sources is not None and len(rows_data_sources) > 0:
1101
            for row in rows_data_sources:
1102
                data_source_dict[row[0]] = {"id": row[0],
1103
                                            "name": row[1],
1104
                                            "uuid": row[2]}
1105
1106
        query = (" SELECT id, name, data_source_id, object_type, units, "
1107
                 "        high_limit, low_limit, higher_limit, lower_limit, ratio, offset_constant, "
1108
                 "        is_trend, is_virtual, address, description, faults, definitions "
1109
                 " FROM tbl_points "
1110
                 " WHERE id = %s ")
1111
        cursor.execute(query, (id_,))
1112
        row = cursor.fetchone()
1113
        cursor.close()
1114
        cnx.close()
1115
        if row is None:
1116
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1117
                                   description='API.POINT_NOT_FOUND')
1118
1119
        result = {"id": row[0],
1120
                  "name": row[1],
1121
                  "data_source": data_source_dict.get(row[2], None),
1122
                  "object_type": row[3],
1123
                  "units": row[4],
1124
                  "high_limit": row[5],
1125
                  "low_limit": row[6],
1126
                  "higher_limit": row[7],
1127
                  "lower_limit": row[8],
1128
                  "ratio": Decimal(row[9]),
1129
                  "offset_constant": Decimal(row[10]),
1130
                  "is_trend": bool(row[11]),
1131
                  "is_virtual": bool(row[12]),
1132
                  "address": row[13],
1133
                  "description": row[14],
1134
                  "faults": row[15],
1135
                  "definitions": row[16]}
1136
        resp.text = json.dumps(result)
1137
1138
1139
class PointImport:
@@ 270-323 (lines=54) @@
267
    def on_options(req, resp, id_):
268
        resp.status = falcon.HTTP_200
269
270
    @staticmethod
271
    def on_get(req, resp, id_):
272
        """Handles GET requests"""
273
        admin_control(req)
274
        if not id_.isdigit() or int(id_) <= 0:
275
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
276
                                   description='API.INVALID_POINT_ID')
277
278
        cnx = mysql.connector.connect(**config.myems_system_db)
279
        cursor = cnx.cursor()
280
281
        query = (" SELECT id, name, uuid "
282
                 " FROM tbl_data_sources ")
283
        cursor.execute(query)
284
        rows_data_sources = cursor.fetchall()
285
286
        data_source_dict = dict()
287
        if rows_data_sources is not None and len(rows_data_sources) > 0:
288
            for row in rows_data_sources:
289
                data_source_dict[row[0]] = {"id": row[0],
290
                                            "name": row[1],
291
                                            "uuid": row[2]}
292
293
        query = (" SELECT id, name, data_source_id, object_type, units, "
294
                 "        high_limit, low_limit, higher_limit, lower_limit, ratio, offset_constant, "
295
                 "        is_trend, is_virtual, address, description, faults, definitions "
296
                 " FROM tbl_points "
297
                 " WHERE id = %s ")
298
        cursor.execute(query, (id_,))
299
        row = cursor.fetchone()
300
        cursor.close()
301
        cnx.close()
302
        if row is None:
303
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
304
                                   description='API.POINT_NOT_FOUND')
305
306
        result = {"id": row[0],
307
                  "name": row[1],
308
                  "data_source": data_source_dict.get(row[2], None),
309
                  "object_type": row[3],
310
                  "units": row[4],
311
                  "high_limit": row[5],
312
                  "low_limit": row[6],
313
                  "higher_limit": row[7],
314
                  "lower_limit": row[8],
315
                  "ratio": Decimal(row[9]),
316
                  "offset_constant": Decimal(row[10]),
317
                  "is_trend": bool(row[11]),
318
                  "is_virtual": bool(row[12]),
319
                  "address": row[13],
320
                  "description": row[14],
321
                  "faults": row[15],
322
                  "definitions": row[16]}
323
        resp.text = json.dumps(result)
324
325
    @staticmethod
326
    @user_logger