Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
nengyuanzhang committed Jun 27, 2024
2 parents 07e1ea3 + 1e916aa commit 0cb8a2d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
61 changes: 36 additions & 25 deletions myems-api/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0cb8a2d

Please sign in to comment.