Esempio n. 1
0
def add_new_product(name, price, dimension, material,
                    category):  #if something is optional=>null
    """ create a new product in the database"""

    product = Product.get_or_none(
        name=name
    )  # first name is class parameter, second is the name form the argument

    data = {
        "name": name,
        "price": price,
        "dimension": dimension,
        "material": material,
        "category": category
    }
    if product is None:  #is None?
        print(material)

        product = Product.insert(data).execute()

    else:  #the product already exit -> we update it

        ProductCategory.delete().where(
            ProductCategory.product == product).execute(
            )  # find the productcategory element associated with the product
        Product.update(data).where(Product.name == name)

    # correspond to the two options (if and else)
    query = Category.get_or_none(name=category)
    if query is None:
        query = Category.create(name=category)
    ProductCategory.create(product=product,
                           category=query)  # join two table in another table

    return product
Esempio n. 2
0
def get_products_by_dimension(min_dimension):
    results = []
    products = Product.select()
    for product in products:
        if product.dimension <= min_dimension:
            results.append(product)
    return results
Esempio n. 3
0
def get_products_by_price(min_price):
    results = []
    products = Product.select()
    for product in products:
        if product.price <= min_price:
            results.append(product)
    return results
Esempio n. 4
0
def search_products(query, category):
    query = query.lower()
    products = Product.select().where(Product.name.contains(query))
    if category is not None:
        filtered_products = []
        for product in products:
            categories = []
            category_of_product = Product.select().where(
                ProductCategory.product == product)
            for product_category in category_of_product:
                category_name = product_category.category.name
                category.append(category_name)

            if category in categories:
                filtered_products.append(product)
        return filtered_products
    return products
Esempio n. 5
0
def get_products_by_category(category):
    results = []

    mycategory = Category.get(name=category)
    products = Product.select()
    for product in products:
        for a in product.category:
            if a.category == mycategory:
                results.append(product)
    return results
Esempio n. 6
0
def get_products_by_material(material):
    results = []

    mymaterial = Material.get(name=material)
    products = Product.select()
    for product in products:
        for a in product.material:
            if a.material == mymaterial:
                results.append(product)
    return results
Esempio n. 7
0
def get_product_by_name(name):
    product = Product.get(name=name)
    return product