fixed ingredient dropping on parser fail

This commit is contained in:
vabene1111
2021-01-07 22:56:05 +01:00
parent e9f2b875b9
commit 78fa5338d3
3 changed files with 5 additions and 3 deletions

View File

@ -76,7 +76,7 @@ def find_recipe_json(ld_json, url):
if ingredient:
ingredients.append({'amount': amount, 'unit': {'text': unit, 'id': random.randrange(10000, 99999)}, 'ingredient': {'text': ingredient, 'id': random.randrange(10000, 99999)}, "note": note, 'original': x})
except:
pass
ingredients.append({'amount': 0, 'unit': {'text': "", 'id': random.randrange(10000, 99999)}, 'ingredient': {'text': x, 'id': random.randrange(10000, 99999)}, "note": "", 'original': x})
ld_json['recipeIngredient'] = ingredients
else:

View File

@ -146,7 +146,7 @@ class Unit(models.Model):
class Food(models.Model):
name = models.CharField(unique=True, max_length=128)
name = models.CharField(unique=True, max_length=128, validators=[MinLengthValidator(1)])
recipe = models.ForeignKey('Recipe', null=True, blank=True, on_delete=models.SET_NULL)
def __str__(self):

View File

@ -124,7 +124,9 @@ def import_url(request):
for ing in data['recipeIngredient']:
ingredient = Ingredient()
ingredient.food, f_created = Food.objects.get_or_create(name=ing['ingredient']['text'])
if ing['ingredient']['text'] != '':
ingredient.food, f_created = Food.objects.get_or_create(name=ing['ingredient']['text'])
if ing['unit'] and ing['unit']['text'] != '':
ingredient.unit, u_created = Unit.objects.get_or_create(name=ing['unit']['text'])