Code Duplication    Length = 72-73 lines in 3 locations

contact.py 1 location

@@ 156-228 (lines=73) @@
153
                  "description": row[5]}
154
        resp.body = json.dumps(result)
155
156
    @staticmethod
157
    def on_delete(req, resp, id_):
158
        if not id_.isdigit() or int(id_) <= 0:
159
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
160
                                   description='API.INVALID_CONTACT_ID')
161
162
        cnx = mysql.connector.connect(**config.myems_system_db)
163
        cursor = cnx.cursor()
164
165
        cursor.execute(" SELECT name "
166
                       " FROM tbl_contacts "
167
                       " WHERE id = %s ", (id_,))
168
        if cursor.fetchone() is None:
169
            cursor.close()
170
            cnx.disconnect()
171
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
172
                                   description='API.CONTACT_NOT_FOUND')
173
174
        # check relation with shopfloors
175
        cursor.execute(" SELECT id "
176
                       " FROM tbl_shopfloors "
177
                       " WHERE contact_id = %s ", (id_,))
178
        rows_shopfloors = cursor.fetchall()
179
        if rows_shopfloors is not None and len(rows_shopfloors) > 0:
180
            cursor.close()
181
            cnx.disconnect()
182
            raise falcon.HTTPError(falcon.HTTP_400,
183
                                   title='API.BAD_REQUEST',
184
                                   description='API.THERE_IS_RELATION_WITH_SHOPFLOORS')
185
186
        # check relation with spaces
187
        cursor.execute(" SELECT id "
188
                       " FROM tbl_spaces "
189
                       " WHERE contact_id = %s ", (id_,))
190
        rows_spaces = cursor.fetchall()
191
        if rows_spaces is not None and len(rows_spaces) > 0:
192
            cursor.close()
193
            cnx.disconnect()
194
            raise falcon.HTTPError(falcon.HTTP_400,
195
                                   title='API.BAD_REQUEST',
196
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
197
198
        # check relation with stores
199
        cursor.execute(" SELECT id "
200
                       " FROM tbl_stores "
201
                       " WHERE contact_id = %s ", (id_,))
202
        rows_stores = cursor.fetchall()
203
        if rows_stores is not None and len(rows_stores) > 0:
204
            cursor.close()
205
            cnx.disconnect()
206
            raise falcon.HTTPError(falcon.HTTP_400,
207
                                   title='API.BAD_REQUEST',
208
                                   description='API.THERE_IS_RELATION_WITH_STORES')
209
210
        # check relation with tenants
211
        cursor.execute(" SELECT id "
212
                       " FROM tbl_tenants "
213
                       " WHERE contact_id = %s ", (id_,))
214
        rows_tenants = cursor.fetchall()
215
        if rows_tenants is not None and len(rows_tenants) > 0:
216
            cursor.close()
217
            cnx.disconnect()
218
            raise falcon.HTTPError(falcon.HTTP_400,
219
                                   title='API.BAD_REQUEST',
220
                                   description='API.THERE_IS_RELATION_WITH_TENANTS')
221
222
        cursor.execute(" DELETE FROM tbl_contacts WHERE id = %s ", (id_,))
223
        cnx.commit()
224
225
        cursor.close()
226
        cnx.disconnect()
227
228
        resp.status = falcon.HTTP_204
229
230
    @staticmethod
231
    def on_put(req, resp, id_):

sensor.py 1 location

@@ 129-201 (lines=73) @@
126
127
        resp.body = json.dumps(meta_result)
128
129
    @staticmethod
130
    def on_delete(req, resp, id_):
131
        if not id_.isdigit() or int(id_) <= 0:
132
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
133
                                   description='API.INVALID_SENSOR_ID')
134
135
        cnx = mysql.connector.connect(**config.myems_system_db)
136
        cursor = cnx.cursor()
137
138
        cursor.execute(" SELECT name "
139
                       " FROM tbl_sensors "
140
                       " WHERE id = %s ", (id_,))
141
        if cursor.fetchone() is None:
142
            cursor.close()
143
            cnx.disconnect()
144
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
145
                                   description='API.SENSOR_NOT_FOUND')
146
147
        # check relation with spaces
148
        cursor.execute(" SELECT id "
149
                       " FROM tbl_spaces_sensors "
150
                       " WHERE sensor_id = %s ", (id_,))
151
        rows_spaces = cursor.fetchall()
152
        if rows_spaces is not None and len(rows_spaces) > 0:
153
            cursor.close()
154
            cnx.disconnect()
155
            raise falcon.HTTPError(falcon.HTTP_400,
156
                                   title='API.BAD_REQUEST',
157
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
158
159
        # check relation with tenants
160
        cursor.execute(" SELECT id "
161
                       " FROM tbl_tenants_sensors "
162
                       " WHERE sensor_id = %s ", (id_,))
163
        rows_tenants = cursor.fetchall()
164
        if rows_tenants is not None and len(rows_tenants) > 0:
165
            cursor.close()
166
            cnx.disconnect()
167
            raise falcon.HTTPError(falcon.HTTP_400,
168
                                   title='API.BAD_REQUEST',
169
                                   description='API.THERE_IS_RELATION_WITH_TENANTS')
170
171
        # check relation with stores
172
        cursor.execute(" SELECT store_id "
173
                       " FROM tbl_stores_sensors "
174
                       " WHERE sensor_id = %s ", (id_,))
175
        rows_stores = cursor.fetchall()
176
        if rows_stores is not None and len(rows_stores) > 0:
177
            cursor.close()
178
            cnx.disconnect()
179
            raise falcon.HTTPError(falcon.HTTP_400,
180
                                   title='API.BAD_REQUEST',
181
                                   description='API.THERE_IS_RELATION_WITH_STORES')
182
183
        # check relation with points
184
        cursor.execute(" SELECT id "
185
                       " FROM tbl_sensors_points "
186
                       " WHERE sensor_id = %s ", (id_,))
187
        rows_points = cursor.fetchall()
188
        if rows_points is not None and len(rows_points) > 0:
189
            cursor.close()
190
            cnx.disconnect()
191
            raise falcon.HTTPError(falcon.HTTP_400,
192
                                   title='API.BAD_REQUEST',
193
                                   description='API.THERE_IS_RELATION_WITH_POINTS')
194
195
        cursor.execute(" DELETE FROM tbl_sensors WHERE id = %s ", (id_,))
196
        cnx.commit()
197
198
        cursor.close()
199
        cnx.disconnect()
200
201
        resp.status = falcon.HTTP_204
202
203
    @staticmethod
204
    def on_put(req, resp, id_):

combinedequipment.py 1 location

@@ 200-271 (lines=72) @@
197
198
        resp.body = json.dumps(meta_result)
199
200
    @staticmethod
201
    def on_delete(req, resp, id_):
202
        if not id_.isdigit() or int(id_) <= 0:
203
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
204
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
205
206
        cnx = mysql.connector.connect(**config.myems_system_db)
207
        cursor = cnx.cursor()
208
209
        # check relation with space
210
        cursor.execute(" SELECT space_id "
211
                       " FROM tbl_spaces_combined_equipments "
212
                       " WHERE combined_equipment_id = %s ",
213
                       (id_,))
214
        rows_combined_equipments = cursor.fetchall()
215
        if rows_combined_equipments is not None and len(rows_combined_equipments) > 0:
216
            cursor.close()
217
            cnx.disconnect()
218
            raise falcon.HTTPError(falcon.HTTP_400,
219
                                   title='API.BAD_REQUEST',
220
                                   description='API.THERE_IS_RELATION_WITH_SPACE')
221
222
        # check relation with meter
223
        cursor.execute(" SELECT meter_id "
224
                       " FROM tbl_combined_equipments_meters "
225
                       " WHERE combined_equipment_id = %s ",
226
                       (id_,))
227
        rows_meters = cursor.fetchall()
228
        if rows_meters is not None and len(rows_meters) > 0:
229
            cursor.close()
230
            cnx.disconnect()
231
            raise falcon.HTTPError(falcon.HTTP_400,
232
                                   title='API.BAD_REQUEST',
233
                                   description='API.THERE_IS_RELATION_WITH_METER')
234
235
        # check relation with offline meter
236
        cursor.execute(" SELECT offline_meter_id "
237
                       " FROM tbl_combined_equipments_offline_meters "
238
                       " WHERE combined_equipment_id = %s ",
239
                       (id_,))
240
        rows_offline_meters = cursor.fetchall()
241
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
242
            cursor.close()
243
            cnx.disconnect()
244
            raise falcon.HTTPError(falcon.HTTP_400,
245
                                   title='API.BAD_REQUEST',
246
                                   description='API.THERE_IS_RELATION_WITH_OFFLINE_METER')
247
248
        # check relation with virtual meter
249
        cursor.execute(" SELECT virtual_meter_id "
250
                       " FROM tbl_combined_equipments_virtual_meters "
251
                       " WHERE combined_equipment_id = %s ",
252
                       (id_,))
253
        rows_virtual_meters = cursor.fetchall()
254
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
255
            cursor.close()
256
            cnx.disconnect()
257
            raise falcon.HTTPError(falcon.HTTP_400,
258
                                   title='API.BAD_REQUEST',
259
                                   description='API.THERE_IS_RELATION_WITH_VIRTUAL_METER')
260
261
        # delete all associated parameters
262
        cursor.execute(" DELETE FROM tbl_combined_equipments_parameters WHERE combined_equipment_id = %s ", (id_,))
263
        cnx.commit()
264
265
        cursor.execute(" DELETE FROM tbl_combined_equipments WHERE id = %s ", (id_,))
266
        cnx.commit()
267
268
        cursor.close()
269
        cnx.disconnect()
270
271
        resp.status = falcon.HTTP_204
272
273
    @staticmethod
274
    def on_put(req, resp, id_):