tests working again
This commit is contained in:
@ -47,83 +47,6 @@ def convert_recipe(request, pk):
|
||||
@group_required('user')
|
||||
def internal_recipe_update(request, pk):
|
||||
recipe_instance = get_object_or_404(Recipe, pk=pk)
|
||||
status = 200
|
||||
|
||||
if request.method == "POST":
|
||||
form = InternalRecipeForm(request.POST, request.FILES)
|
||||
form.instance = recipe_instance
|
||||
|
||||
if form.is_valid():
|
||||
recipe = recipe_instance
|
||||
recipe.name = form.cleaned_data['name']
|
||||
recipe.instructions = form.cleaned_data['instructions']
|
||||
recipe.working_time = form.cleaned_data['working_time']
|
||||
recipe.waiting_time = form.cleaned_data['waiting_time']
|
||||
|
||||
if form.cleaned_data['image']:
|
||||
recipe.image = form.cleaned_data['image']
|
||||
|
||||
img = Image.open(recipe.image)
|
||||
|
||||
basewidth = 720
|
||||
wpercent = (basewidth / float(img.size[0]))
|
||||
hsize = int((float(img.size[1]) * float(wpercent)))
|
||||
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
|
||||
|
||||
im_io = BytesIO()
|
||||
img.save(im_io, 'PNG', quality=70)
|
||||
recipe.image = File(im_io, name=f'{uuid.uuid4()}_{recipe.pk}.png')
|
||||
elif 'image' in form.changed_data and form.cleaned_data['image'] is False:
|
||||
recipe.image = None
|
||||
|
||||
recipe.save()
|
||||
|
||||
try:
|
||||
form_ingredients = json.loads(form.cleaned_data['ingredients'])
|
||||
except simplejson.errors.JSONDecodeError:
|
||||
form_ingredients = []
|
||||
|
||||
Ingredient.objects.filter(recipe=recipe_instance).delete()
|
||||
|
||||
for i in form_ingredients:
|
||||
recipe_ingredient = Ingredient()
|
||||
recipe_ingredient.recipe = recipe_instance
|
||||
|
||||
if 'note' in i:
|
||||
recipe_ingredient.note = i['note']
|
||||
|
||||
if Food.objects.filter(name=i['ingredient__name']).exists():
|
||||
recipe_ingredient.ingredient = Food.objects.get(name=i['ingredient__name'])
|
||||
else:
|
||||
food = Food()
|
||||
food.name = i['food__name']
|
||||
food.save()
|
||||
recipe_ingredient.ingredient = food
|
||||
|
||||
if isinstance(i['amount'], str):
|
||||
try:
|
||||
recipe_ingredient.amount = float(i['amount'].replace(',', '.'))
|
||||
except ValueError:
|
||||
form.add_error("ingredients", _('There was an error converting your ingredients amount to a number: ') + i['unit__name'])
|
||||
else:
|
||||
recipe_ingredient.amount = i['amount']
|
||||
|
||||
if Unit.objects.filter(name=i['unit__name']).exists():
|
||||
recipe_ingredient.unit = Unit.objects.get(name=i['unit__name'])
|
||||
else:
|
||||
unit = Unit()
|
||||
unit.name = i['unit__name']
|
||||
unit.save()
|
||||
recipe_ingredient.unit = unit
|
||||
|
||||
recipe_ingredient.save()
|
||||
|
||||
recipe.keywords.set(form.cleaned_data['keywords'])
|
||||
|
||||
messages.add_message(request, messages.SUCCESS, _('Recipe saved!'))
|
||||
else:
|
||||
messages.add_message(request, messages.ERROR, _('There was an error saving this recipe!'))
|
||||
status = 403
|
||||
|
||||
return render(request, 'forms/edit_internal_recipe.html', {'recipe': recipe_instance})
|
||||
|
||||
|
Reference in New Issue
Block a user