Code Duplication    Length = 38-39 lines in 2 locations

knowledgefile.py 1 location

@@ 202-240 (lines=39) @@
199
                  "user_display_name": user_dict.get(row[4], None)}
200
        resp.body = json.dumps(result)
201
202
    @staticmethod
203
    def on_delete(req, resp, id_):
204
        if not id_.isdigit() or int(id_) <= 0:
205
            raise falcon.HTTPError(falcon.HTTP_400,
206
                                   title='API.BAD_REQUEST',
207
                                   description='API.INVALID_KNOWLEDGE_FILE_ID')
208
209
        cnx = mysql.connector.connect(**config.myems_system_db)
210
        cursor = cnx.cursor()
211
212
        cursor.execute(" SELECT uuid "
213
                       " FROM tbl_knowledge_files "
214
                       " WHERE id = %s ", (id_,))
215
        row = cursor.fetchone()
216
        if row is None:
217
            cursor.close()
218
            cnx.disconnect()
219
            raise falcon.HTTPError(falcon.HTTP_404,
220
                                   title='API.NOT_FOUND',
221
                                   description='API.KNOWLEDGE_FILE_NOT_FOUND')
222
223
        try:
224
            file_uuid = row[0]
225
            # Define file_path
226
            file_path = os.path.join(config.upload_path, file_uuid)
227
228
            # remove the file from disk
229
            os.remove(file_path)
230
        except Exception as ex:
231
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
232
                                   description='API.KNOWLEDGE_FILE_NOT_FOUND')
233
234
        cursor.execute(" DELETE FROM tbl_knowledge_files WHERE id = %s ", (id_,))
235
        cnx.commit()
236
237
        cursor.close()
238
        cnx.disconnect()
239
240
        resp.status = falcon.HTTP_204
241
242
243
class KnowledgeFileRestore:

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