def create_token(): """This function logs in and generates the access token.""" json_values = request.json values_required = {"username", "password"} response = Response(json.dumps(json_error(ResponsesREST.INVALID_INPUT.value)), status=ResponsesREST.INVALID_INPUT.value, mimetype="application/json") if all(key in json_values for key in values_required): if validator_login.is_valid(json_values): account_login = Account() account_login.username = json_values["username"] account_login.password = encode_password(json_values["password"]) account_login.member_ate_status = AccountStatus.ACTIVE.value result = account_login.login() if result in (ResponsesREST.SERVER_ERROR.value, ResponsesREST.NOT_FOUND.value, ResponsesREST.INVALID_REQUEST.value): response = Response(json.dumps(json_error(result)), status=result, mimetype="application/json") else: account_login.member_ate_type = result.member_ate_type account_login.id_member_ate = result.id_member_ate account_login.id_city = result.id_city token = Auth.generate_token(account_login) session.permanent = True session["token"] = token response = Response(json.dumps({"token": token, "memberATEType": account_login.member_ate_type, "idMemberATE": account_login.id_member_ate, "idCity": account_login.id_city}), status=ResponsesREST.CREATED.value, mimetype="application/json") return response
def add_employee_account(id_account): """This function adds the type of user employed to an already created account.""" response = Response(json.dumps( json_error(ResponsesREST.INVALID_INPUT.value)), status=ResponsesREST.INVALID_INPUT.value, mimetype="application/json") if validator_id.is_valid({"id": id_account}): account_status = Account() account_status.id_member_ate = id_account account_status.member_ate_type = AccountRole.CLIENT_EMPLOYEE.value result = account_status.add_employee_account() if result == ResponsesREST.SUCCESSFUL.value: response = Response(status=result) else: response = Response(json.dumps(json_error(result)), status=result, mimetype="application/json") return response