"nutrients":[
{"title":"Energie","unit":"kcal","amount":734,"rda":2200,"percentage":33},
{"title":"Eiweiß","unit":"g","amount":40,"rda":55,"percentage":73},
{"title":"Fett","unit":"g","amount":32,"rda":75,"percentage":43},
{"title":"Kohlenhydrate","unit":"g","amount":58,"rda":300,"percentage":19}
]
# vvvvvvvvvvvvvvvvvvvvvv
if 'nutrition' in recipe_json or 'nutrients' in recipe_json:
if settings.DEBUG:
print("found nutrition or nutrient")
calories = 0
carbohydrates = 0
fats = 0
proteins = 0
# check out the keys in 'nutrients'
if 'nutrients' in recipe_json:
if settings.DEBUG:
print("found nutrient")
for nutrient in recipe_json['nutrients']:
if 'title' in nutrient and 'amount' in nutrient:
if settings.DEBUG:
print("found nutrient " + nutrient['title'])
if nurient['title'] == "Energie":
calories = nurient['amount']
if nurient['title'] == "Eiweiß":
proteins = nurient['amount']
if nurient['title'] == "Fett":
fats = nurient['amount']
if nurient['title'] == "Kohlenhydrate":
carbohydrates = nurient['amount']
# but prefer values in 'nutrition'
if 'nutrition' in recipe_json:
if settings.DEBUG:
print("found nutrition")
if recipe_json['nutrition']['calories']:
calories = remove_non_digts(recipe_json['nutrition']['calories'])
if recipe_json['nutrition']['carbohydrateContent']:
carbohydrates = remove_non_digts(recipe_json['nutrition']['carbohydrateContent'])
if recipe_json['nutrition']['fatContent']:
fats = remove_non_digts(recipe_json['nutrition']['fatContent'])
if recipe_json['nutrition']['proteinContent']:
proteins = remove_non_digts(recipe_json['nutrition']['proteinContent'])
recipe.nutrition = NutritionInformation.objects.create(
calories=calories,
carbohydrates=carbohydrates,
fats=fats,
proteins=proteins,
source='cookbookapp',
space=self.request.space,
)
# ^^^^^^^^^^^^^^^^^^^^^^