Skip to content

Commit

Permalink
Add feature to filter when listing products
Browse files Browse the repository at this point in the history
  • Loading branch information
Crinibus committed Oct 27, 2024
1 parent e28101d commit 22db720
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
5 changes: 4 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def main() -> None:
scraper.print_latest_datapoints(args.name, args.id, args.category)

if args.list_products:
scraper.print_all_products()
if any([args.name, args.id, args.category]):
scraper.list_products_with_filters(args.name, args.id, args.category)
else:
scraper.print_all_products()

if args.delete:
scraper.delete(args.category, args.name, args.id, args.all)
Expand Down
2 changes: 1 addition & 1 deletion scraper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .delete_data import delete
from .reset_data import reset
from .search_data import search
from .print_products import print_latest_datapoints, print_all_products
from .print_products import print_latest_datapoints, print_all_products, list_products_with_filters
from .format import Format
import scraper.database as db

Expand Down
50 changes: 44 additions & 6 deletions scraper/print_products.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import scraper.database as db
from scraper.database.models import Product
from scraper.models.product import ProductInfo


Expand Down Expand Up @@ -60,9 +61,46 @@ def print_all_products() -> None:

grouped_products = db.group_products_by_names(products)

for products in grouped_products:
print(f" > {products[0].name.upper()}")
for product in products:
is_active_marker = "\u2713 " if product.is_active else ""
print(f" - {is_active_marker}{product.domain.upper()} - {product.product_code}")
print()
list_grouped_products(grouped_products)


def list_products_with_filters(names: list[str] | None, product_codes: list[str] | None, categories: list[str] | None) -> None:
print("\n----- LISTING PRODUCTS -----")
products_by_filters: list[Product] = []

if names:
products_with_names = db.get_products_by_names(names)
products_by_filters.extend(products_with_names)

if product_codes:
products_with_product_codes = db.get_products_by_product_codes(product_codes)
products_by_filters.extend(products_with_product_codes)

if categories:
products_with_categories = db.get_products_by_categories(categories)
products_by_filters.extend(products_with_categories)

if not products_by_filters:
print("Found no products with filters")
return

categories = set([product.category for product in products_by_filters])
sorted_categories = sorted(categories)

for category in sorted_categories:
print(category.upper())

products_with_category = [product for product in products_by_filters if product.category == category]

grouped_products = db.group_products_by_names(products_with_category)

list_grouped_products(grouped_products)


def list_grouped_products(grouped_products: list[list[Product]]) -> None:
for products in grouped_products:
print(f" > {products[0].name.upper()}")
for product in products:
is_active_marker = "\u2713 " if product.is_active else ""
print(f" - {is_active_marker}{product.domain.upper()} - {product.product_code}")
print()

0 comments on commit 22db720

Please sign in to comment.