latest changes
This commit is contained in:
+23
-1
@@ -421,7 +421,11 @@ def edit_entry(request, pk):
|
||||
updated = form.save(commit=False)
|
||||
factor = updated.amount / original_amount
|
||||
for field, original in original_nutrients.items():
|
||||
setattr(updated, field, (original * factor).quantize(Decimal('0.001')) if original is not None else None)
|
||||
if form.cleaned_data['edit_nutrition']:
|
||||
value = form.cleaned_data[field]
|
||||
else:
|
||||
value = (original * factor).quantize(Decimal('0.001')) if original is not None else None
|
||||
setattr(updated, field, value)
|
||||
with transaction.atomic():
|
||||
sync_food_hydration(updated)
|
||||
updated.full_clean()
|
||||
@@ -441,6 +445,24 @@ def edit_entry(request, pk):
|
||||
'portion_options': _portion_options(form, product)})
|
||||
|
||||
|
||||
@login_required
|
||||
@require_http_methods(['GET', 'POST'])
|
||||
def edit_product(request, pk):
|
||||
product = get_object_or_404(FoodProduct, pk=pk, owner=request.user)
|
||||
form = ManualFoodForm(request.POST or None, instance=product, user=request.user)
|
||||
if request.method == 'POST' and form.is_valid():
|
||||
product = form.save(commit=False)
|
||||
nutrition_fields = {'energy_kcal_100', 'carbohydrates_100', 'protein_100',
|
||||
'fat_100', 'saturated_fat_100', 'fibre_100', 'sugars_100', 'salt_100'}
|
||||
if product.source == FoodProduct.Source.OPEN_FOOD_FACTS and nutrition_fields.intersection(form.changed_data):
|
||||
product.raw_data = {**(product.raw_data if isinstance(product.raw_data, dict) else {}),
|
||||
'manual_nutrition_override': True}
|
||||
product.save()
|
||||
messages.success(request, 'Product saved. Existing diary entries keep their recorded nutrition.')
|
||||
return redirect('food:product-edit', pk=product.pk)
|
||||
return render(request, 'food/product_edit.html', {'form': form, 'product': product})
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def delete_entry(request, pk):
|
||||
|
||||
Reference in New Issue
Block a user