latest changes
This commit is contained in:
+28
-25
@@ -7,15 +7,16 @@ from .models import FoodLogEntry, FoodPortion, FoodProduct, KitchenInventoryItem
|
||||
|
||||
class ManualFoodForm(forms.ModelForm):
|
||||
basis_amount = forms.DecimalField(min_value=Decimal('0.001'), max_digits=10, decimal_places=3,
|
||||
initial=100, label='Nutrition basis amount')
|
||||
initial=100, required=False, label='Nutrition basis amount')
|
||||
class Meta:
|
||||
model = FoodProduct
|
||||
fields = ('product_name', 'brand', 'barcode', 'base_unit', 'energy_kcal_100', 'carbohydrates_100', 'protein_100', 'fat_100', 'fibre_100', 'sugars_100', 'salt_100')
|
||||
fields = ('product_name', 'brand', 'barcode', 'base_unit', 'energy_kcal_100', 'carbohydrates_100', 'protein_100', 'fat_100', 'saturated_fat_100', 'fibre_100', 'sugars_100', 'salt_100')
|
||||
labels = {
|
||||
'energy_kcal_100': 'Calories for base quantity',
|
||||
'carbohydrates_100': 'Carbohydrates for base quantity',
|
||||
'protein_100': 'Protein for base quantity',
|
||||
'fat_100': 'Fat for base quantity',
|
||||
'saturated_fat_100': 'Saturated fat for base quantity (optional)',
|
||||
'fibre_100': 'Fibre for base quantity (optional)',
|
||||
'sugars_100': 'Sugars for base quantity (optional)',
|
||||
'salt_100': 'Salt for base quantity (optional)',
|
||||
@@ -25,14 +26,21 @@ class ManualFoodForm(forms.ModelForm):
|
||||
self.user = kwargs.pop('user', None)
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['barcode'].required = False
|
||||
for field in ('energy_kcal_100', 'carbohydrates_100', 'protein_100', 'fat_100'):
|
||||
self.fields[field].required = True
|
||||
self.fields['base_unit'].required = False
|
||||
if self.instance.pk:
|
||||
self.fields['base_unit'].disabled = True
|
||||
|
||||
def clean_base_unit(self):
|
||||
return self.cleaned_data['base_unit'] or self.instance.base_unit or 'g'
|
||||
|
||||
def clean_basis_amount(self):
|
||||
return self.cleaned_data['basis_amount'] or Decimal('100')
|
||||
|
||||
def clean_barcode(self):
|
||||
barcode = self.cleaned_data['barcode'].strip()
|
||||
if barcode and (not barcode.isascii() or not barcode.isdigit()):
|
||||
raise forms.ValidationError('Enter a numeric barcode.')
|
||||
if barcode and self.user and FoodProduct.objects.filter(owner=self.user, barcode=barcode).exists():
|
||||
if barcode and self.user and FoodProduct.objects.filter(owner=self.user, barcode=barcode).exclude(pk=self.instance.pk).exists():
|
||||
raise forms.ValidationError('This barcode is already saved in your foods.')
|
||||
return barcode
|
||||
|
||||
@@ -42,7 +50,7 @@ class ManualFoodForm(forms.ModelForm):
|
||||
if basis:
|
||||
factor = Decimal('100') / basis
|
||||
for name in ('energy_kcal_100', 'carbohydrates_100', 'protein_100', 'fat_100',
|
||||
'fibre_100', 'sugars_100', 'salt_100'):
|
||||
'saturated_fat_100', 'fibre_100', 'sugars_100', 'salt_100'):
|
||||
value = data.get(name)
|
||||
if value is not None and value * factor > Decimal('9999999.999'):
|
||||
self.add_error(name, 'This amount is too large for the chosen base quantity.')
|
||||
@@ -52,7 +60,7 @@ class ManualFoodForm(forms.ModelForm):
|
||||
product = super().save(commit=False)
|
||||
factor = Decimal('100') / self.cleaned_data['basis_amount']
|
||||
for name in ('energy_kcal_100', 'carbohydrates_100', 'protein_100', 'fat_100',
|
||||
'fibre_100', 'sugars_100', 'salt_100'):
|
||||
'saturated_fat_100', 'fibre_100', 'sugars_100', 'salt_100'):
|
||||
value = self.cleaned_data[name]
|
||||
setattr(product, name, (value * factor).quantize(Decimal('0.001')) if value is not None else None)
|
||||
if commit:
|
||||
@@ -162,9 +170,15 @@ class FoodLogForm(forms.ModelForm):
|
||||
|
||||
|
||||
class FoodLogEditForm(FoodLogForm):
|
||||
edit_nutrition = forms.BooleanField(required=False, label='Use these nutrition values for this entry')
|
||||
|
||||
class Meta(FoodLogForm.Meta):
|
||||
fields = FoodLogForm.Meta.fields + ('quantity',)
|
||||
labels = {**FoodLogForm.Meta.labels, 'quantity': 'Quantity (portions)'}
|
||||
fields = FoodLogForm.Meta.fields + ('quantity', 'calories_kcal', 'carbohydrates_g',
|
||||
'protein_g', 'fat_g', 'fibre_g', 'sugars_g', 'salt_g')
|
||||
labels = {**FoodLogForm.Meta.labels, 'quantity': 'Quantity (portions)',
|
||||
'calories_kcal': 'Calories (kcal)', 'carbohydrates_g': 'Carbohydrates (g)',
|
||||
'protein_g': 'Protein (g)', 'fat_g': 'Fat (g)', 'fibre_g': 'Fibre (g)',
|
||||
'sugars_g': 'Sugars (g)', 'salt_g': 'Salt (g)'}
|
||||
widgets = {**FoodLogForm.Meta.widgets,
|
||||
'quantity': forms.NumberInput(attrs={'min': '0.01', 'step': '0.01'})}
|
||||
|
||||
@@ -234,19 +248,8 @@ class InventoryItemForm(forms.ModelForm):
|
||||
return ' '.join(self.cleaned_data['category_name'].split())
|
||||
|
||||
|
||||
class InventoryManualProductForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = FoodProduct
|
||||
fields = ('product_name', 'brand', 'barcode', 'package_quantity')
|
||||
|
||||
def __init__(self, *args, user=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.user = user
|
||||
|
||||
def clean_barcode(self):
|
||||
barcode = self.cleaned_data['barcode'].strip()
|
||||
if barcode and (not barcode.isascii() or not barcode.isdigit()):
|
||||
raise forms.ValidationError('Enter a numeric barcode.')
|
||||
if barcode and self.user and FoodProduct.objects.filter(owner=self.user, barcode=barcode).exists():
|
||||
raise forms.ValidationError('This barcode is already saved in your products.')
|
||||
return barcode
|
||||
class InventoryManualProductForm(ManualFoodForm):
|
||||
class Meta(ManualFoodForm.Meta):
|
||||
fields = ('product_name', 'brand', 'barcode', 'package_quantity', 'base_unit',
|
||||
'energy_kcal_100', 'carbohydrates_100', 'protein_100', 'fat_100',
|
||||
'saturated_fat_100', 'fibre_100', 'sugars_100', 'salt_100')
|
||||
|
||||
Reference in New Issue
Block a user