latest changes
This commit is contained in:
@@ -17,7 +17,7 @@ from django.utils import timezone
|
||||
from food.models import FoodProduct, Unit
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
FIELDS = 'code,product_name,brands,quantity,serving_size,serving_quantity,image_front_small_url,image_front_url,nutriments'
|
||||
FIELDS = 'code,product_name,brands,quantity,serving_size,serving_quantity,image_front_small_url,image_front_url,nutriments,categories,categories_tags'
|
||||
NUTRIENTS = {
|
||||
'energy-kcal_100g': 'energy_kcal_100',
|
||||
'carbohydrates_100g': 'carbohydrates_100',
|
||||
@@ -154,6 +154,8 @@ def cached_product(user, barcode, *, refresh=False):
|
||||
mapped['fetched_at'] = timezone.now()
|
||||
nutrients = data.get('nutriments') if isinstance(data.get('nutriments'), dict) else {}
|
||||
mapped['raw_data'] = {'code': barcode, 'product_name': mapped['product_name'],
|
||||
'categories': str(data.get('categories') or '')[:1000],
|
||||
'categories_tags': data.get('categories_tags', []) if isinstance(data.get('categories_tags'), list) else [],
|
||||
'nutriments': {key: nutrients.get(key) for key in NUTRIENTS}}
|
||||
product, _ = FoodProduct.objects.update_or_create(owner=user, barcode=barcode, defaults=mapped)
|
||||
return product
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
from django.db.models import Q
|
||||
|
||||
from food.models import FoodProduct
|
||||
|
||||
|
||||
def local_products(user, query=''):
|
||||
"""Shared user-owned catalogue search for food logging and inventory."""
|
||||
products = FoodProduct.objects.filter(owner=user)
|
||||
for token in query.strip().split():
|
||||
products = products.filter(Q(product_name__icontains=token) | Q(brand__icontains=token) |
|
||||
Q(barcode__icontains=token))
|
||||
return products
|
||||
Reference in New Issue
Block a user