latest changes
This commit is contained in:
+46
-1
@@ -1,7 +1,7 @@
|
||||
from django import forms
|
||||
from decimal import Decimal
|
||||
|
||||
from .models import FoodLogEntry, FoodPortion, FoodProduct
|
||||
from .models import FoodLogEntry, FoodPortion, FoodProduct, KitchenInventoryItem
|
||||
|
||||
|
||||
class ManualFoodForm(forms.ModelForm):
|
||||
@@ -139,3 +139,48 @@ class FoodLogForm(forms.ModelForm):
|
||||
elif data.get('make_default'):
|
||||
self.add_error('make_default', 'Save the portion before making it the default.')
|
||||
return data
|
||||
|
||||
|
||||
class InventoryItemForm(forms.ModelForm):
|
||||
location_name = forms.CharField(max_length=100, label='Location', widget=forms.TextInput(attrs={'list': 'inventory-locations', 'autocomplete': 'off'}))
|
||||
category_name = forms.CharField(max_length=100, required=False, label='My category', widget=forms.TextInput(attrs={'list': 'inventory-categories', 'autocomplete': 'off'}))
|
||||
|
||||
class Meta:
|
||||
model = KitchenInventoryItem
|
||||
fields = ('quantity', 'opened', 'best_before', 'notes')
|
||||
widgets = {'quantity': forms.NumberInput(attrs={'min': 1, 'step': 1}),
|
||||
'best_before': forms.DateInput(attrs={'type': 'date'}),
|
||||
'notes': forms.Textarea(attrs={'rows': 2})}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
if self.instance.pk:
|
||||
self.fields['location_name'].initial = self.instance.location.name
|
||||
self.fields['category_name'].initial = self.instance.category.name if self.instance.category else ''
|
||||
|
||||
def clean_location_name(self):
|
||||
value = ' '.join(self.cleaned_data['location_name'].split())
|
||||
if not value:
|
||||
raise forms.ValidationError('Enter a location.')
|
||||
return value
|
||||
|
||||
def clean_category_name(self):
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user