Code Duplication    Length = 38-39 lines in 2 locations

core/offlinemeterfile.py 1 location

@@ 171-208 (lines=38) @@
168
                  "status": row[4]}
169
        resp.body = json.dumps(result)
170
171
    @staticmethod
172
    def on_delete(req, resp, id_):
173
        if not id_.isdigit() or int(id_) <= 0:
174
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
175
                                   description='API.INVALID_OFFLINE_METER_FILE_ID')
176
177
        cnx = mysql.connector.connect(**config.myems_historical_db)
178
        cursor = cnx.cursor()
179
180
        cursor.execute(" SELECT uuid "
181
                       " FROM tbl_offline_meter_files "
182
                       " WHERE id = %s ", (id_,))
183
        row = cursor.fetchone()
184
        if row is None:
185
            cursor.close()
186
            cnx.disconnect()
187
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
188
                                   description='API.OFFLINE_METER_FILE_NOT_FOUND')
189
190
        try:
191
            file_uuid = row[0]
192
            # Define file_path
193
            file_path = os.path.join(config.upload_path, file_uuid)
194
195
            # remove the file from disk
196
            os.remove(file_path)
197
        except Exception as ex:
198
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
199
                                   description='API.OFFLINE_METER_FILE_NOT_FOUND')
200
201
        # Note: the energy data imported from the deleted file will not be deleted
202
        cursor.execute(" DELETE FROM tbl_offline_meter_files WHERE id = %s ", (id_,))
203
        cnx.commit()
204
205
        cursor.close()
206
        cnx.disconnect()
207
208
        resp.status = falcon.HTTP_204
209

core/knowledgefile.py 1 location

@@ 230-268 (lines=39) @@
227
                  "user_display_name": user_dict.get(row[4], None)}
228
        resp.body = json.dumps(result)
229
230
    @staticmethod
231
    def on_delete(req, resp, id_):
232
        if not id_.isdigit() or int(id_) <= 0:
233
            raise falcon.HTTPError(falcon.HTTP_400,
234
                                   title='API.BAD_REQUEST',
235
                                   description='API.INVALID_KNOWLEDGE_FILE_ID')
236
237
        cnx = mysql.connector.connect(**config.myems_system_db)
238
        cursor = cnx.cursor()
239
240
        cursor.execute(" SELECT uuid "
241
                       " FROM tbl_knowledge_files "
242
                       " WHERE id = %s ", (id_,))
243
        row = cursor.fetchone()
244
        if row is None:
245
            cursor.close()
246
            cnx.disconnect()
247
            raise falcon.HTTPError(falcon.HTTP_404,
248
                                   title='API.NOT_FOUND',
249
                                   description='API.KNOWLEDGE_FILE_NOT_FOUND')
250
251
        try:
252
            file_uuid = row[0]
253
            # Define file_path
254
            file_path = os.path.join(config.upload_path, file_uuid)
255
256
            # remove the file from disk
257
            os.remove(file_path)
258
        except Exception as ex:
259
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
260
                                   description='API.KNOWLEDGE_FILE_NOT_FOUND')
261
262
        cursor.execute(" DELETE FROM tbl_knowledge_files WHERE id = %s ", (id_,))
263
        cnx.commit()
264
265
        cursor.close()
266
        cnx.disconnect()
267
268
        resp.status = falcon.HTTP_204
269
270
271
class KnowledgeFileRestore: