Refactor user registration (#1970)

### What problem does this PR solve?

1. Refactor error message
2. Update function name

### Type of change

- [x] Refactoring

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai 2024-08-15 19:51:09 +08:00 committed by GitHub
parent 4810cb2dc9
commit c9551b7f68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 10 deletions

View File

@ -32,7 +32,7 @@ from api.settings import RetCode, GITHUB_OAUTH, FEISHU_OAUTH, CHAT_MDL, EMBEDDIN
from api.db.services.user_service import UserService, TenantService, UserTenantService
from api.db.services.file_service import FileService
from api.settings import stat_logger
from api.utils.api_utils import get_json_result, cors_reponse
from api.utils.api_utils import get_json_result, construct_response
@manager.route('/login', methods=['POST', 'GET'])
@ -64,7 +64,7 @@ def login():
user.update_date = datetime_format(datetime.now()),
user.save()
msg = "Welcome back!"
return cors_reponse(data=response_data, auth=user.get_id(), retmsg=msg)
return construct_response(data=response_data, auth=user.get_id(), retmsg=msg)
else:
return get_json_result(data=False, retcode=RetCode.AUTHENTICATION_ERROR,
retmsg='Email and Password do not match!')
@ -347,10 +347,12 @@ def user_add():
retmsg=f'Email: {email_address} has already registered!',
retcode=RetCode.OPERATING_ERROR)
# Construct user info data
nickname = req["nickname"]
user_dict = {
"access_token": get_uuid(),
"email": email_address,
"nickname": req["nickname"],
"nickname": nickname,
"password": decrypt(req["password"]),
"login_channel": "password",
"last_login_time": get_format_time(),
@ -361,18 +363,20 @@ def user_add():
try:
users = user_register(user_id, user_dict)
if not users:
raise Exception('Register user failure.')
raise Exception(f'Fail to register {email_address}.')
if len(users) > 1:
raise Exception('Same E-mail exist!')
raise Exception(f'Same E-mail: {email_address} exists!')
user = users[0]
login_user(user)
return cors_reponse(data=user.to_json(),
auth=user.get_id(), retmsg="Welcome aboard!")
return construct_response(data=user.to_json(),
auth=user.get_id(),
retmsg=f"{nickname}, welcome aboard!")
except Exception as e:
rollback_user_registration(user_id)
stat_logger.exception(e)
return get_json_result(
data=False, retmsg='User registration failure!', retcode=RetCode.EXCEPTION_ERROR)
return get_json_result(data=False,
retmsg=f'User registration failure, error: {str(e)}',
retcode=RetCode.EXCEPTION_ERROR)
@manager.route("/tenant_info", methods=["GET"])

View File

@ -216,7 +216,7 @@ def get_json_result(retcode=RetCode.SUCCESS, retmsg='success', data=None):
return jsonify(response)
def cors_reponse(retcode=RetCode.SUCCESS,
def construct_response(retcode=RetCode.SUCCESS,
retmsg='success', data=None, auth=None):
result_dict = {"retcode": retcode, "retmsg": retmsg, "data": data}
response_dict = {}