Code Duplication    Length = 79-80 lines in 2 locations

gsmmodem.py 1 location

@@ 9-88 (lines=80) @@
6
import re
7
8
9
class GSMModemCollection:
10
    @staticmethod
11
    def __init__():
12
        pass
13
14
    @staticmethod
15
    def on_options(req, resp):
16
        resp.status = falcon.HTTP_200
17
18
    @staticmethod
19
    def on_get(req, resp):
20
        cnx = mysql.connector.connect(**config.myems_fdd_db)
21
        cursor = cnx.cursor()
22
23
        query = (" SELECT id, serial_port, baud_rate "
24
                 " FROM tbl_gsm_modems ")
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],
34
                               "serial_port": row[1],
35
                               "baud_rate": row[2]}
36
                result.append(meta_result)
37
38
        resp.body = json.dumps(result)
39
40
    @staticmethod
41
    def on_post(req, resp):
42
        """Handles POST requests"""
43
        try:
44
            raw_json = req.stream.read().decode('utf-8')
45
        except Exception as ex:
46
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
47
48
        new_values = json.loads(raw_json, encoding='utf-8')
49
50
        if 'serial_port' not in new_values['data'].keys() or \
51
                not isinstance(new_values['data']['serial_port'], str) or \
52
                len(str.strip(new_values['data']['serial_port'])) == 0:
53
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
54
                                   description='API.INVALID_SERIAL_PORT')
55
56
        serial_port = str.strip(new_values['data']['serial_port'])
57
58
        if 'baud_rate' not in new_values['data'].keys() or \
59
                not isinstance(new_values['data']['baud_rate'], int) or \
60
                new_values['data']['baud_rate'] <= 0:
61
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
62
                                   description='API.INVALID_BAUD_RATE')
63
        baud_rate = float(new_values['data']['baud_rate'])
64
65
        cnx = mysql.connector.connect(**config.myems_fdd_db)
66
        cursor = cnx.cursor()
67
68
        cursor.execute(" SELECT id "
69
                       " FROM tbl_gsm_modems "
70
                       " WHERE serial_port = %s ", (serial_port,))
71
        if cursor.fetchone() is not None:
72
            cursor.close()
73
            cnx.disconnect()
74
            raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST',
75
                                   description='API.GSM_MODEM_SERIAL_PORT_IS_ALREADY_IN_USE')
76
77
        add_value = (" INSERT INTO tbl_gsm_modems "
78
                     "    (serial_port, baud_rate) "
79
                     " VALUES (%s, %s) ")
80
        cursor.execute(add_value, (serial_port,
81
                                   baud_rate))
82
        new_id = cursor.lastrowid
83
        cnx.commit()
84
        cursor.close()
85
        cnx.disconnect()
86
87
        resp.status = falcon.HTTP_201
88
        resp.location = '/gsmmodems/' + str(new_id)
89
90
91
class GSMModemItem:

privilege.py 1 location

@@ 7-85 (lines=79) @@
4
import config
5
6
7
class PrivilegeCollection:
8
    @staticmethod
9
    def __init__():
10
        pass
11
12
    @staticmethod
13
    def on_options(req, resp):
14
        resp.status = falcon.HTTP_200
15
16
    @staticmethod
17
    def on_get(req, resp):
18
        cnx = mysql.connector.connect(**config.myems_user_db)
19
        cursor = cnx.cursor()
20
21
        query = (" SELECT id, name, data "
22
                 " FROM tbl_privileges "
23
                 " ORDER BY id DESC ")
24
        cursor.execute(query)
25
        rows = cursor.fetchall()
26
        cursor.close()
27
        cnx.disconnect()
28
29
        result = list()
30
        if rows is not None and len(rows) > 0:
31
            for row in rows:
32
                meta_result = {"id": row[0],
33
                               "name": row[1],
34
                               "data": row[2]}
35
                result.append(meta_result)
36
37
        resp.body = json.dumps(result)
38
39
    @staticmethod
40
    def on_post(req, resp):
41
        """Handles POST requests"""
42
        try:
43
            raw_json = req.stream.read().decode('utf-8')
44
            new_values = json.loads(raw_json, encoding='utf-8')
45
        except Exception as ex:
46
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
47
48
        if 'name' not in new_values['data'] or \
49
            not isinstance(new_values['data']['name'], str) or \
50
                len(str.strip(new_values['data']['name'])) == 0:
51
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
52
                                   description='API.INVALID_PRIVILEGE_NAME')
53
        name = str.strip(new_values['data']['name'])
54
55
        if 'data' not in new_values['data'] or \
56
            not isinstance(new_values['data']['data'], str) or \
57
                len(str.strip(new_values['data']['data'])) == 0:
58
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
59
                                   description='API.INVALID_PRIVILEGE_DATA')
60
        data = str.strip(new_values['data']['data'])
61
62
        cnx = mysql.connector.connect(**config.myems_user_db)
63
        cursor = cnx.cursor()
64
65
        cursor.execute(" SELECT name "
66
                       " FROM tbl_privileges "
67
                       " WHERE name = %s ", (name,))
68
        if cursor.fetchone() is not None:
69
            cursor.close()
70
            cnx.disconnect()
71
            raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST',
72
                                   description='API.PRIVILEGE_NAME_IS_ALREADY_IN_USE')
73
74
        add_row = (" INSERT INTO tbl_privileges "
75
                   "             (name, data) "
76
                   " VALUES (%s, %s) ")
77
78
        cursor.execute(add_row, (name, data, ))
79
        new_id = cursor.lastrowid
80
        cnx.commit()
81
        cursor.close()
82
        cnx.disconnect()
83
84
        resp.status = falcon.HTTP_201
85
        resp.location = '/privileges/' + str(new_id)
86
87
88
class PrivilegeItem: