Code Duplication    Length = 41-41 lines in 2 locations

energyflowdiagram.py 1 location

@@ 121-161 (lines=41) @@
118
        cnx.disconnect()
119
        resp.body = json.dumps(result)
120
121
    @staticmethod
122
    def on_post(req, resp):
123
        """Handles POST requests"""
124
        try:
125
            raw_json = req.stream.read().decode('utf-8')
126
        except Exception as ex:
127
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
128
129
        new_values = json.loads(raw_json, encoding='utf-8')
130
131
        if 'name' not in new_values['data'].keys() or \
132
                not isinstance(new_values['data']['name'], str) or \
133
                len(str.strip(new_values['data']['name'])) == 0:
134
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
135
                                   description='API.INVALID_ENERGY_FLOW_DIAGRAM_NAME')
136
        name = str.strip(new_values['data']['name'])
137
138
        cnx = mysql.connector.connect(**config.myems_system_db)
139
        cursor = cnx.cursor()
140
141
        cursor.execute(" SELECT name "
142
                       " FROM tbl_energy_flow_diagrams "
143
                       " WHERE name = %s ", (name,))
144
        if cursor.fetchone() is not None:
145
            cursor.close()
146
            cnx.disconnect()
147
            raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST',
148
                                   description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE')
149
150
        add_values = (" INSERT INTO tbl_energy_flow_diagrams "
151
                      "    (name, uuid) "
152
                      " VALUES (%s, %s) ")
153
        cursor.execute(add_values, (name,
154
                                    str(uuid.uuid4())))
155
        new_id = cursor.lastrowid
156
        cnx.commit()
157
        cursor.close()
158
        cnx.disconnect()
159
160
        resp.status = falcon.HTTP_201
161
        resp.location = '/energyflowdiagrams/' + str(new_id)
162
163
164
class EnergyFlowDiagramItem:

gateway.py 1 location

@@ 49-89 (lines=41) @@
46
47
        resp.body = json.dumps(result)
48
49
    @staticmethod
50
    def on_post(req, resp):
51
        """Handles POST requests"""
52
        try:
53
            raw_json = req.stream.read().decode('utf-8')
54
        except Exception as ex:
55
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)
56
57
        new_values = json.loads(raw_json, encoding='utf-8')
58
59
        if 'name' not in new_values['data'].keys() or \
60
                not isinstance(new_values['data']['name'], str) or \
61
                len(str.strip(new_values['data']['name'])) == 0:
62
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
63
                                   description='API.INVALID_GATEWAY_NAME')
64
        name = str.strip(new_values['data']['name'])
65
66
        cnx = mysql.connector.connect(**config.myems_system_db)
67
        cursor = cnx.cursor()
68
69
        cursor.execute(" SELECT name "
70
                       " FROM tbl_gateways "
71
                       " WHERE name = %s ", (name,))
72
        if cursor.fetchone() is not None:
73
            cursor.close()
74
            cnx.disconnect()
75
            raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST',
76
                                   description='API.GATEWAY_NAME_IS_ALREADY_IN_USE')
77
78
        add_values = (" INSERT INTO tbl_gateways (name, uuid, token) "
79
                      " VALUES (%s, %s, %s) ")
80
        cursor.execute(add_values, (name,
81
                                    str(uuid.uuid4()),
82
                                    str(uuid.uuid4())))
83
        new_id = cursor.lastrowid
84
        cnx.commit()
85
        cursor.close()
86
        cnx.disconnect()
87
88
        resp.status = falcon.HTTP_201
89
        resp.location = '/gateways/' + str(new_id)
90
91
92
class GatewayItem: