Exemplo n.º 1
0
def delete_user(user_id: int):
    user = User.query.filter(User.id == user_id).first()
    if user is not None:
        if user.root:
            raise CustomError(Error.DELETE_ROOT_FORBIDDEN, 403)
        if user.deleted:
            raise CustomError(Error.USER_ALREADY_DELETED, 400)
        user.deleted = True
        user.deleted_at = datetime.now()
    else:
        raise CustomError(Error.USER_NOT_FOUND, 404)
Exemplo n.º 2
0
def update_user(user_id: int, user_data: dict):
    user = User.query.filter_by(id=user_id).first()
    if user is not None:
        if user.root:
            raise CustomError(Error.UPDATE_ROOT_FORBIDDEN, 403)
        for key, value in user_data.items():
            if hasattr(user, key):
                setattr(user, key, value)
            else:
                # TODO: raise error
                pass
    else:
        raise CustomError(Error.USER_NOT_FOUND, 404)
Exemplo n.º 3
0
def activate_user(token):
    try:
        email = confirm_token(token)
    except:
        raise CustomError(Error.USER_CONFIRMATION_LINK_INVALID_OR_EXPIRED,
                          HTTPStatus.NOT_FOUND)
    user = User.query.filter_by(email=email).first()
    if user is None:
        raise CustomError(Error.USER_NOT_FOUND, HTTPStatus.NOT_FOUND)
    if user.is_active:
        raise CustomError(Error.USER_ALREADY_ACTIVATED, HTTPStatus.CONFLICT)
    else:
        user.is_active = True
Exemplo n.º 4
0
def login_user(email, password):
    if is_not_none_or_empty(email, password):
        user = User.query.filter_by(email=email).first()
        if user is None or user.deleted:
            raise CustomError(Error.AUTH_LOGIN_USER_NOT_FOUND_ERROR, 404)
        elif not user.is_active:
            raise CustomError(Error.AUTH_LOGIN_USER_NOT_ACTIVATED, 400)
        elif user.verify_password(password):
            flask_login.login_user(user)
        else:
            raise CustomError(Error.AUTH_LOGIN_WRONG_PASSWORD, 400)

    else:
        raise CustomError(Error.AUTH_LOGIN_NOT_VALID_DATA_ERROR, 400)
Exemplo n.º 5
0
def get_product_by_name(product_name):
    product = Product.query.filter_by(deleted=False).filter_by(
        name=product_name).first()
    if product is not None:
        return product
    else:
        raise CustomError(Error.PRODUCT_NOT_FOUND, HTTPStatus.NOT_FOUND)
Exemplo n.º 6
0
def get_product(product_id):
    product = Product.query.filter_by(deleted=False).filter_by(
        id=product_id).first()
    if product is not None:
        return product
    else:
        raise CustomError(Error.PRODUCT_NOT_FOUND, 404)
Exemplo n.º 7
0
def add_cart_item(cart_item: CartItem, user_id: int):
    try:
        get_db().session.commit()
    except Exception as e:
        logging.warning('Problem with add new cart item: ' +
                        str(cart_item.product_id))
        traceback.print_exc()
        # TODO: set id of product and user id
        raise CustomError(Error.CART_ITEM_ADD_ERROR, 400)
Exemplo n.º 8
0
def add_product(product: Product, user_id: int):
    product.added_by = user_id
    try:
        get_db().session.add(product)
        get_db().session.commit()
    except Exception as e:
        logging.warning('Problem with add new product: ' + product.name)
        traceback.print_exc()
        raise CustomError(Error.PRODUCT_ADD_ERROR, 400)
Exemplo n.º 9
0
def put_favourite(product_id, user_id):
    favourite = Favourite.query.filter_by(product_id=product_id).filter_by(
        user_id=user_id).first()
    if favourite is None:
        favourite = Favourite()
        favourite.product_id = product_id
        favourite.user_id = user_id
        get_db().session.add(favourite)
    else:
        raise CustomError(Error.FAVOURITE_CHANGE_ERROR, HTTPStatus.CONFLICT)
Exemplo n.º 10
0
def delete_cart_items(cart_item_id: int, user_id: int):
    cart_item = CartItem.query.filter_by(user_id).filter_by(
        cart_item_id).delete()
    try:
        get_db().session.commit()
    except Exception as e:
        # TODO: set id of product and user id
        logging.warning('Problem with delete new cart item: ' +
                        str(cart_item.product_id))
        traceback.print_exc()
        # TODO: set id of product and user id
        raise CustomError(Error.CART_ITEM_ADD_ERROR, 400)
Exemplo n.º 11
0
def update_product(product_data: dict, product_id: int, user_id: int):
    product = Product.query.filter_by(deleted=False).filter_by(
        id=product_id).first()
    if product is not None:
        for key, value in product_data.items():
            if hasattr(product, key):
                setattr(product, key, value)
            else:
                # TODO: raise error
                pass
        product.added_by = user_id
    else:
        raise CustomError(Error.PRODUCT_NOT_FOUND, HTTPStatus.NOT_FOUND)
Exemplo n.º 12
0
def put_cart_items(quantity: int, cart_item_id: int, user_id: int):
    cart_item = CartItem.query.filter_by(user_id).filter_by(
        cart_item_id).first()
    cart_item.quantity = quantity
    try:
        get_db().session.commit()
    except Exception as e:
        # TODO: set id of product and user id
        logging.warning('Problem with update cart item: ' +
                        str(cart_item.product_id))
        traceback.print_exc()
        # TODO: set id of product and user id
        raise CustomError(Error.CART_ITEM_UPDATE_ERROR, 400)
Exemplo n.º 13
0
def process_commit(error_enum):
    try:
        get_db().session.commit()
    except Exception as e:
        logging.warning('traceback: ' + traceback.print_exc())
        raise CustomError(error_enum, 400)
Exemplo n.º 14
0
def get_user_by_id(user_id: int) -> User:
    user = User.query.filter(User.id == user_id).first()
    if user is not None:
        return user
    else:
        raise CustomError(Error.USER_NOT_FOUND, 404)
Exemplo n.º 15
0
def add_user(user_to_create: User):
    user = User.query.filter_by(email=user_to_create.email).first()
    if user is None:
        get_db().session.add(user_to_create)
    else:
        raise CustomError(Error.USER_ALREADY_EXISTS, 409)