ficus-tracker/app/api_v1/users.py
def create_user_or_return_token():
    """ Create user and return it's token if user doesn't exists otherwise return user token """
    resp_data = {}  # response data
    headers = request.headers or {}

    # Check request
    if 'Authorization' not in headers:
        return bad_request("Missing 'Authorization' header in request")

    # Parse auth
    try:
        login, password = parse_authorization_header(headers.get('Authorization'))
    except Exception as e:
        return server_error(f"Exception occurred during parsing user credentials: {str(e)}")

    try:
        user = User.query.filter_by(login=login).first()
    except Exception as e:
        return server_error(f"Exception occurred during loading user: {str(e)}")

    # Return token if user already exists
    if user:
        # Check credentials
        try:
            if authorize(login, password, user):
                resp_data['token'] = user.token
                resp = create_response_from_data_with_code(resp_data, 200)
            else:
                return unauthorized(login)
        except Exception as e:
            return server_error(f"Exception occurred during authorization: {str(e)}")
    else:
        # Create user
        user = User()
        user.login = login
        user.password_hash = user.generate_password_hash(password)
        user.token = user.generate_token()
        resp_data['token'] = user.token

        # Commit changes to db
        db.session.add(user)
        db.session.commit()

        resp = create_response_from_data_with_code(resp_data, 200)

    return resp
 Create user and return it's token if user doesn't exists otherwise return user token 
Create user and return it's token if user doesn't exists otherwise return user token
Create user and return it ' s token if user doesn ' t exists otherwise return user token
def create_user_or_return_token():
    resp_data = {}  
    headers = request.headers or {}
    if 'Authorization' not in headers:
        return bad_request("Missing 'Authorization' header in request")
    try:
        login, password = parse_authorization_header(headers.get('Authorization'))
    except Exception as e:
        return server_error(f"Exception occurred during parsing user credentials: {str(e)}")
    try:
        user = User.query.filter_by(login=login).first()
    except Exception as e:
        return server_error(f"Exception occurred during loading user: {str(e)}")
    if user:
        try:
            if authorize(login, password, user):
                resp_data['token'] = user.token
                resp = create_response_from_data_with_code(resp_data, 200)
            else:
                return unauthorized(login)
        except Exception as e:
            return server_error(f"Exception occurred during authorization: {str(e)}")
    else:
        user = User()
        user.login = login
        user.password_hash = user.generate_password_hash(password)
        user.token = user.generate_token()
        resp_data['token'] = user.token
        db.session.add(user)
        db.session.commit()
        resp = create_response_from_data_with_code(resp_data, 200)
    return resp
def create_user_or_return_token ( ) : resp_data = { } headers = request . headers or { } if 'Authorization' not in headers : return bad_request ( "Missing 'Authorization' header in request" ) try : login , password = parse_authorization_header ( headers . get ( 'Authorization' ) ) except Exception as e : return server_error ( f"Exception occurred during parsing user credentials: {str(e)}" ) try : user = User . query . filter_by ( login = login ) . first ( ) except Exception as e : return server_error ( f"Exception occurred during loading user: {str(e)}" ) if user : try : if authorize ( login , password , user ) : resp_data [ 'token' ] = user . token resp = create_response_from_data_with_code ( resp_data , 200 ) else : return unauthorized ( login ) except Exception as e : return server_error ( f"Exception occurred during authorization: {str(e)}" ) else : user = User ( ) user . login = login user . password_hash = user . generate_password_hash ( password ) user . token = user . generate_token ( ) resp_data [ 'token' ] = user . token db . session . add ( user ) db . session . commit ( ) resp = create_response_from_data_with_code ( resp_data , 200 ) return resp
Create user and return it's token if user doesn't exists otherwise return user token
Create user and return it ' s token if user doesn ' t exists otherwise return user token
""" Create user and return it's token if user doesn't exists otherwise return user token """ # response data # Check request # Parse auth # Return token if user already exists # Check credentials # Create user # Commit changes to db