Pārlūkot izejas kodu

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>
tags/v0.10.0
Jin Hai pirms 1 gada
vecāks
revīzija
c9551b7f68
Revīzijas autora e-pasta adrese nav piesaistīta nevienam kontam
2 mainītis faili ar 14 papildinājumiem un 10 dzēšanām
  1. 13
    9
      api/apps/user_app.py
  2. 1
    1
      api/utils/api_utils.py

+ 13
- 9
api/apps/user_app.py Parādīt failu

from api.db.services.user_service import UserService, TenantService, UserTenantService from api.db.services.user_service import UserService, TenantService, UserTenantService
from api.db.services.file_service import FileService from api.db.services.file_service import FileService
from api.settings import stat_logger 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']) @manager.route('/login', methods=['POST', 'GET'])
user.update_date = datetime_format(datetime.now()), user.update_date = datetime_format(datetime.now()),
user.save() user.save()
msg = "Welcome back!" 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: else:
return get_json_result(data=False, retcode=RetCode.AUTHENTICATION_ERROR, return get_json_result(data=False, retcode=RetCode.AUTHENTICATION_ERROR,
retmsg='Email and Password do not match!') retmsg='Email and Password do not match!')
retmsg=f'Email: {email_address} has already registered!', retmsg=f'Email: {email_address} has already registered!',
retcode=RetCode.OPERATING_ERROR) retcode=RetCode.OPERATING_ERROR)


# Construct user info data
nickname = req["nickname"]
user_dict = { user_dict = {
"access_token": get_uuid(), "access_token": get_uuid(),
"email": email_address, "email": email_address,
"nickname": req["nickname"],
"nickname": nickname,
"password": decrypt(req["password"]), "password": decrypt(req["password"]),
"login_channel": "password", "login_channel": "password",
"last_login_time": get_format_time(), "last_login_time": get_format_time(),
try: try:
users = user_register(user_id, user_dict) users = user_register(user_id, user_dict)
if not users: if not users:
raise Exception('Register user failure.')
raise Exception(f'Fail to register {email_address}.')
if len(users) > 1: if len(users) > 1:
raise Exception('Same E-mail exist!')
raise Exception(f'Same E-mail: {email_address} exists!')
user = users[0] user = users[0]
login_user(user) 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: except Exception as e:
rollback_user_registration(user_id) rollback_user_registration(user_id)
stat_logger.exception(e) 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"]) @manager.route("/tenant_info", methods=["GET"])

+ 1
- 1
api/utils/api_utils.py Parādīt failu

return jsonify(response) return jsonify(response)




def cors_reponse(retcode=RetCode.SUCCESS,
def construct_response(retcode=RetCode.SUCCESS,
retmsg='success', data=None, auth=None): retmsg='success', data=None, auth=None):
result_dict = {"retcode": retcode, "retmsg": retmsg, "data": data} result_dict = {"retcode": retcode, "retmsg": retmsg, "data": data}
response_dict = {} response_dict = {}

Notiek ielāde…
Atcelt
Saglabāt