Code Duplication    Length = 38-39 lines in 2 locations

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:

core/offlinemeterfile.py 1 location

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