Skip to content

Commit

Permalink
Refactor user registration (#1970)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

1. Refactor error message
2. Update function name

### Type of change

- [x] Refactoring

Signed-off-by: Jin Hai <[email protected]>
  • Loading branch information
JinHai-CN authored Aug 15, 2024
1 parent 4810cb2 commit c9551b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions api/apps/user_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
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'])
Expand Down Expand Up @@ -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!')
Expand Down Expand Up @@ -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(),
Expand All @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion api/utils/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down

0 comments on commit c9551b7

Please sign in to comment.