Exemplo n.º 1
0
def user_get_products(id=None):
    """
    get_products returns all product or the product with specific id
    Args:
        id ([type]): product id

    Returns:
        [type]: [description]
    """
    page, per_page = get_page_from_args()
    sort_by = request.args.get('sort_by')
    sort_by_price = request.args.get('sort_by_price')
    is_desc = parse_int(request.args.get('is_desc'))
    category_id = parse_int(request.args.get('category'))
    items = [Product.query.get(id)] if id else Product.get_items(
        category_id=category_id,
        page=page,
        per_page=per_page,
        sort_by=sort_by,
        is_desc=is_desc)

    variations = Variation.get_items(category_id=None,
                                     page=1,
                                     per_page=per_page,
                                     sort_by=sort_by,
                                     is_desc=is_desc)

    all_product_variations = []

    for item in items:
        available_product_variations = []
        for variation in variations:
            if (item.id == variation.product_id):
                available_product_variations.append(variation)

        product_variations = ProductVariations(product=item)
        product_variations.variations = available_product_variations

        all_product_variations.append(product_variations)

    if sort_by_price == 'price':
        sorted_productVariations_by_price = sorted(
            all_product_variations,
            key=lambda x: x.variations[0].price,
            reverse=False)
        return res([
            product_variation.as_dict()
            for product_variation in sorted_productVariations_by_price
        ])

    return res([
        product_variation.as_dict()
        for product_variation in all_product_variations
    ])
Exemplo n.º 2
0
def user_get_productcategories(category=None):
    """
    get_products meeting criteria
    Args:
        id ([type]): product id

    Returns:
        [type]: [description]
    """
    page, per_page = get_page_from_args()
    sort_by = request.args.get('sort_by')
    sort_by_price = request.args.get('sort_by_price')
    is_desc = parse_int(request.args.get('is_desc'))

    category_details = []
    if request.args.get('category') is None:
        category_details = ProductCategory.get_category_from_name(category)
    else:
        category_details = ProductCategory.get_category_from_name(
            request.args.get('category'))
    print(category)
    cat_id = category_details[0].id

    # category_details = ProductCategory.get_category_from_name(category)
    # cat_id = category_details[0].id

    items = Product.get_items(category_id=cat_id,
                              page=page,
                              per_page=per_page,
                              sort_by=sort_by,
                              is_desc=is_desc)

    variations = Variation.get_items(
        category_id=None,
        page=1,
        per_page=per_page,
        sort_by=sort_by,
        is_desc=is_desc
    )  # TODO UPDATE from one page, done as only currently small number of prods

    all_product_variations = []

    for item in items:
        available_product_variations = []
        for variation in variations:
            if (item.id == variation.product_id):
                available_product_variations.append(variation)

        product_variations = ProductVariations(product=item)
        product_variations.variations = available_product_variations

        all_product_variations.append(product_variations)

    if sort_by_price == 'price':
        sorted_productVariations_by_price = sorted(
            all_product_variations,
            key=lambda x: x.variations[0].price,
            reverse=False)
        return res([
            product_variation.as_dict()
            for product_variation in sorted_productVariations_by_price
        ])

    return res([
        product_variation.as_dict()
        for product_variation in all_product_variations
    ])