From 1e916aada3945eae0bef72e203cdac7175b362bb Mon Sep 17 00:00:00 2001 From: nengyuanzhang <13011132526@163.com> Date: Thu, 27 Jun 2024 19:57:44 +0800 Subject: [PATCH] fixed issue of on_delete action in myems-api --- CHANGELOG.md | 1 + myems-api/core/user.py | 61 +++++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39b73366f2..b73e5166f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - replaced svg with svg_id in energy storage power station ### Fixed - added check relations statements to point on_delete action in myems-api +- fixed issue of on_delete action in myems-api ### Removed diff --git a/myems-api/core/user.py b/myems-api/core/user.py index 4f046b160f..7eb3d00956 100644 --- a/myems-api/core/user.py +++ b/myems-api/core/user.py @@ -280,37 +280,48 @@ def on_delete(req, resp, id_): raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_USER_ID') - cnx = mysql.connector.connect(**config.myems_user_db) - cursor = cnx.cursor() - - cursor.execute(" SELECT name " - " FROM tbl_users " - " WHERE id = %s ", (id_,)) - if cursor.fetchone() is None: - cursor.close() - cnx.close() + cnx_user_db = mysql.connector.connect(**config.myems_user_db) + cursor_user_db = cnx_user_db.cursor() + user_uuid = None + cursor_user_db.execute(" SELECT uuid " + " FROM tbl_users " + " WHERE id = %s ", (id_,)) + row = cursor_user_db.fetchone() + if row is None: + cursor_user_db.close() + cnx_user_db.close() raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', description='API.USER_NOT_FOUND') + else: + user_uuid = row[0] - # check if this user is being used by microgrid - cursor.execute(" SELECT id " - " FROM tbl_microgrids_users " - " WHERE user_id = %s ", (id_,)) - rows_microgrid = cursor.fetchall() - if rows_microgrid is not None and len(rows_microgrid) > 0: - cursor.close() - cnx.close() - raise falcon.HTTPError(status=falcon.HTTP_400, - title='API.BAD_REQUEST', - description='API.THERE_IS_RELATION_WITH_MICROGRIDS') + cnx_system_db = mysql.connector.connect(**config.myems_system_db) + cursor_system_db = cnx_system_db.cursor() - # TODO: delete associated objects - cursor.execute(" DELETE FROM tbl_users WHERE id = %s ", (id_,)) - cnx.commit() + # check if this user is being used by energy storage power stations + cursor_system_db.execute(" DELETE FROM tbl_energy_storage_power_stations_users WHERE user_id = %s ", (id_,)) + cnx_system_db.commit() - cursor.close() - cnx.close() + # check if this user is being used by microgrids + cursor_system_db.execute(" DELETE FROM tbl_microgrids_users WHERE user_id = %s ", (id_,)) + cnx_system_db.commit() + + cursor_user_db.execute(" DELETE FROM tbl_sessions WHERE user_uuid = %s ", (user_uuid,)) + cnx_user_db.commit() + + cursor_user_db.execute(" DELETE FROM tbl_logs WHERE user_uuid = %s ", (user_uuid,)) + cnx_user_db.commit() + + cursor_user_db.execute(" DELETE FROM tbl_notifications WHERE user_id = %s ", (id_,)) + cnx_user_db.commit() + + cursor_user_db.execute(" DELETE FROM tbl_users WHERE id = %s ", (id_,)) + cnx_user_db.commit() + cursor_user_db.close() + cnx_user_db.close() + cursor_system_db.close() + cnx_system_db.close() resp.status = falcon.HTTP_204 @staticmethod