Example #1
0
def list_item(item_id):
    """Return HTTP response

    List a specific items that matches the given ID.
    """
    item = Item.query.filter_by(id=item_id).first()

    if item is None:
        return make_response(jsonify({
            'status': 'error',
            'message': 'Item not found'
        })), 404

    auth_header = request.headers.get('Authorization')
    if auth_header:
        auth_token = auth_header.split(' ')[0]
    else:
        auth_token = ''

    if auth_token:
        user_id = User.decode_auth_token(auth_token)

    if (user_id == item.user_id):
        is_user_owner = True
    else:
        is_user_owner = False

    return make_response(jsonify(
        {**item.serialize, 'is_user_owner': is_user_owner})), 200
Example #2
0
def get_userid_from_header(header):
    """Returns the user ID or the error exception

    Validates if the given authorization header contains a valid
    user token. If so, decode the token returning the user_id. Otherwise,
    retrieves an error message.
    """
    if header:
        auth_token = header.split(' ')[0]
    else:
        auth_token = ''

    if auth_token:
        resp = User.decode_auth_token(auth_token)

        if isinstance(resp, str):
            raise CustomValueError('error', resp)

        return resp
    else:
        raise CustomValueError('error', 'The auth token provided is not valid')