def show_cart_details(user_name):
    from data_models import Cart, Products
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    cart_list = [
        k.to_dict() for k in session.query(Products).filter(
            Products.id == Cart.product_id, Cart.client_username ==
            user_name).all()
    ]
    header = list(cart_list[0].keys())
    table = tabular_format_details(cart_list, header)
    print(table)
    session.close()
    # Show dialog to buy product and to remove product from cart
    with open('data_files/admin_details.yaml') as file:
        dialog = yaml.safe_load(file)
        print(dialog['cart_details']["title"])
        for option in dialog['cart_details']["options"]:
            print(option, end="")
        choice = input("Enter your choice : ")
    if choice.lower() == '1':
        # To buy product
        return buy_products(user_name)
    elif choice.lower() == '2':
        # To remove product
        return remove_item_from_cart(user_name)
    else:
        return show_dialog()
Exemple #2
0
def sqlalchemy_engine(database_url):
    params = {}
    if database_url.startswith("sqlite"):
        params["connect_args"] = {'check_same_thread': False}
        params["poolclass"] = StaticPool

    engine = create_engine(database_url, **params)

    return engine
def show_categories():
    from data_models import Categories
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    category_list = [k.to_dict() for k in session.query(Categories).all()]
    header = list(category_list[0].keys())
    table = tabular_format_details(category_list, header)
    print(table)
    return show_dialog()
def show_products():
    from data_models import Products
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    product_list = [k.to_dict() for k in session.query(Products).all()]
    header = list(product_list[0].keys())
    table = tabular_format_details(product_list, header)
    print(table)
    session.close()
    return show_dialog()
def show_cart_details(user_name):
    from data_models import Cart
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    cart_list = [k.to_dict() for k in session.query(Cart).filter_by(client_username=user_name).all()]
    header = list(cart_list[0].keys())
    table = tabular_format_details(cart_list, header)
    print(table)
    session.close()
    return show_dialog()
def user_details_to_show_cart():
    from data_models import User
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    user_list = [k.to_dict() for k in session.query(User).all()]
    print("Choose user to see cart details :")
    header = list(user_list[0].keys())
    table = tabular_format_details(user_list, header)
    print(table)
    session.close()
    return show_cart_details()
def add_to_cart(product_id):
    from data_models import Products, Cart
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    if product_id == '0':
        return show_dialog()
    else:
        cart = Cart()
        cart.client_id = session_data['user_id']
        cart.product_id = product_id
        cart.status = "In-progress"
        cart.client_username = session_data['username']
        session.add(cart)
        session.commit()
    session.close()
    return show_cart_details(session_data['username'])
def show_product_section(category_name):
    from data_models import Products, Categories
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    product_list = [
        k.to_dict() for k in session.query(Products).filter(
            Products.category_id == Categories.id,
            func.lower(Categories.category_name) ==
            category_name.lower()).all()
    ]
    session.close()
    if len(product_list):
        header = list(product_list[0].keys())
        table = tabular_format_details(product_list, header)
        print(table)
    return add_to_cart()
def add_products(category_id, product_name, product_details, product_price, category_name):
    from data_models import Products
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    product_list = [k.to_dict() for k in session.query(Products).filter_by(product_name=product_name).all()]
    if len(product_list) > 0:
        print("Product details already present")
    else:
        products = Products()
        products.category_id = category_id
        products.category_name = category_name
        products.product_name = product_name
        products.product_details = product_details
        products.product_price = product_price
        session.add(products)
        session.commit()
    session.close()
    return show_products()
Exemple #10
0
def remove_item_from_cart(user_name):
    from data_models import Cart, Products
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    cart_list = [
        k.to_dict() for k in session.query(Products).filter(
            Products.id == Cart.product_id, Cart.client_username ==
            user_name).all()
    ]
    header = list(cart_list[0].keys())
    table = tabular_format_details(cart_list, header)
    print(table)
    choice = input("Enter product id to remove product from cart : ")
    # Fetch cart details on selected product id
    cart_details = session.query(Cart).filter_by(product_id=choice).first()
    session.delete(cart_details)
    session.commit()
    session.close()
    return show_cart_details(user_name)
def add_categories(base_category, category_name, category_level):
    from data_models import Categories
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    # Fetch category list
    category_list = [k.to_dict() for k in session.query(Categories).filter_by(category_name=category_name).all()]
    if len(category_list) > 0:
        # If details exist alreday
        print("Category details already present")
    else:
        # If details not exist then add
        category = Categories()
        category.base_category = base_category
        category.category_name = category_name
        category.category_level = category_level
        session.add(category)
        session.commit()
    session.close()
    return show_categories()
Exemple #12
0
def sub_category(category_name):
    from data_models import Categories
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    sub_category_list = [
        k.to_dict() for k in session.query(Categories).filter(
            func.lower(Categories.base_category) ==
            category_name.lower()).all()
    ]
    session.close()

    if len(sub_category_list) > 0:
        # if category list exist then go to another subcategory
        for cat in sub_category_list:
            print(" - ", cat['category_name'])
        cat_name = input("Enter your choice : ")
        return sub_category(cat_name)
    else:
        # if category list does'nt exist then go to product list
        return show_product_section(category_name)
Exemple #13
0
def buy_products(user_name):
    from data_models import Cart, Products
    engine = create_engine()
    Session = sessionmaker(bind=engine)
    session = Session()
    cart_list = [
        k.to_dict() for k in session.query(Products).filter(
            Products.id == Cart.product_id, Cart.client_username ==
            user_name).all()
    ]
    session.close()
    header = list(cart_list[0].keys())
    table = tabular_format_details(cart_list, header)
    # print(table)
    sum_list = list(map(lambda x: x['product_price'], cart_list))
    print("Actual Price : ", sum(sum_list))
    if sum(sum_list) >= 10000:
        print("Discounted Price : 500", )
        print("Net Price : ", (sum(sum_list) - 500))
    else:
        print("Net Price : ", sum(sum_list))