added recipe properties

This commit is contained in:
vabene1111
2023-05-06 19:14:25 +02:00
parent 763f71a05c
commit 60f31608b9
17 changed files with 1099 additions and 735 deletions

View File

@@ -1,6 +1,6 @@
from django.db.models import Q
from cookbook.models import Unit, SupermarketCategory, FoodProperty, FoodPropertyType, Supermarket, SupermarketCategoryRelation, Food, Automation, UnitConversion
from cookbook.models import Unit, SupermarketCategory, FoodProperty, PropertyType, Supermarket, SupermarketCategoryRelation, Food, Automation, UnitConversion
class OpenDataImporter:
@@ -55,14 +55,14 @@ class OpenDataImporter:
insert_list = []
for k in list(self.data[datatype].keys()):
insert_list.append(FoodPropertyType(
insert_list.append(PropertyType(
name=self.data[datatype][k]['name'],
unit=self.data[datatype][k]['unit'],
open_data_slug=k,
space=self.request.space
))
return FoodPropertyType.objects.bulk_create(insert_list, update_conflicts=True, update_fields=('open_data_slug',), unique_fields=('space', 'name',))
return PropertyType.objects.bulk_create(insert_list, update_conflicts=True, update_fields=('open_data_slug',), unique_fields=('space', 'name',))
def import_supermarket(self):
datatype = 'supermarket'
@@ -114,7 +114,7 @@ class OpenDataImporter:
existing_objects[f[2]] = f
self._update_slug_cache(Unit, 'unit')
self._update_slug_cache(FoodPropertyType, 'property')
self._update_slug_cache(PropertyType, 'property')
pref_unit_key = 'preferred_unit_metric'
pref_shopping_unit_key = 'preferred_packaging_unit_metric'

View File

@@ -1,4 +1,4 @@
from cookbook.models import FoodPropertyType, Unit, Food, FoodProperty, Recipe, Step
from cookbook.models import PropertyType, Unit, Food, FoodProperty, Recipe, Step
class FoodPropertyHelper:
@@ -19,7 +19,7 @@ class FoodPropertyHelper:
"""
ingredients = []
computed_properties = {}
property_types = FoodPropertyType.objects.filter(space=self.space).all()
property_types = PropertyType.objects.filter(space=self.space).all()
for s in recipe.steps.all():
ingredients += s.ingredients.all()
@@ -64,10 +64,10 @@ class FoodPropertyHelper:
food_1 = Food.objects.create(name='Food 1', space=self.space)
food_2 = Food.objects.create(name='Food 2', space=self.space)
property_fat = FoodPropertyType.objects.create(name='Fat', unit='g', space=self.space)
property_calories = FoodPropertyType.objects.create(name='Calories', unit='kcal', space=self.space)
property_nuts = FoodPropertyType.objects.create(name='Nuts', space=self.space)
property_price = FoodPropertyType.objects.create(name='Price', unit='', space=self.space)
property_fat = PropertyType.objects.create(name='Fat', unit='g', space=self.space)
property_calories = PropertyType.objects.create(name='Calories', unit='kcal', space=self.space)
property_nuts = PropertyType.objects.create(name='Nuts', space=self.space)
property_price = PropertyType.objects.create(name='Price', unit='', space=self.space)
food_1_property_fat = FoodProperty.objects.create(food_amount=100, food_unit=unit_gram, food=food_1, property_amount=50, property_type=property_fat, space=self.space)
food_1_property_nuts = FoodProperty.objects.create(food_amount=100, food_unit=unit_gram, food=food_1, property_amount=1, property_type=property_nuts, space=self.space)
@@ -87,3 +87,18 @@ class FoodPropertyHelper:
step_2 = Step.objects.create(instruction='instruction_step_1', space=self.space)
step_2.ingredients.create(amount=50, unit=unit_gram, food=food_1, space=self.space)
recipe_1.steps.add(step_2)
class RecipePropertyHelper:
space = None
def __init__(self, space):
"""
Helper to perform recipe property operations
:param space: space to limit scope to
"""
self.space = space
def parse_properties_from_schema(self, schema):
pass

View File

@@ -1,5 +1,6 @@
# import random
import re
import traceback
from html import unescape
from django.core.cache import caches
@@ -12,7 +13,8 @@ from recipe_scrapers._utils import get_host_name, get_minutes
# from cookbook.helper import recipe_url_import as helper
from cookbook.helper.ingredient_parser import IngredientParser
from cookbook.models import Automation, Keyword
from cookbook.models import Automation, Keyword, PropertyType
# from unicodedata import decomposition
@@ -193,6 +195,13 @@ def get_from_scraper(scrape, request):
except Exception:
pass
try:
recipe_json['properties'] = get_recipe_properties(request.space, scrape.schema.nutrients())
print(recipe_json['properties'])
except Exception:
traceback.print_exc()
pass
if recipe_json['source_url']:
automations = Automation.objects.filter(type=Automation.INSTRUCTION_REPLACE, space=request.space, disabled=False).only('param_1', 'param_2', 'param_3').order_by('order').all()[:512]
for a in automations:
@@ -203,6 +212,30 @@ def get_from_scraper(scrape, request):
return recipe_json
def get_recipe_properties(space, property_data):
# {'servingSize': '1', 'calories': '302 kcal', 'proteinContent': '7,66g', 'fatContent': '11,56g', 'carbohydrateContent': '41,33g'}
properties = {
"property-calories": "calories",
"property-carbohydrates": "carbohydrateContent",
"property-proteins": "proteinContent",
"property-fats": "fatContent",
}
recipe_properties = []
for pt in PropertyType.objects.filter(space=space, open_data_slug__in=list(properties.keys())).all():
for p in list(properties.keys()):
if pt.open_data_slug == p:
if properties[p] in property_data:
recipe_properties.append({
'property_type': {
'id': pt.id,
'name': pt.name,
},
'property_amount': parse_servings(property_data[properties[p]]) / float(property_data['servingSize']),
})
return recipe_properties
def get_from_youtube_scraper(url, request):
"""A YouTube Information Scraper."""
kw, created = Keyword.objects.get_or_create(name='YouTube', space=request.space)