mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Logic fixes for CreateStockItem form
- Improved data validation - Fix bug where form was not checked for validity
This commit is contained in:
parent
3d0bea15ae
commit
3df8f33080
@ -227,6 +227,18 @@ class StockItem(MPTTModel):
|
|||||||
'status': 'Status cannot be marked as ASSIGNED_TO_OTHER_ITEM if the belongs_to field is not set',
|
'status': 'Status cannot be marked as ASSIGNED_TO_OTHER_ITEM if the belongs_to field is not set',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if self.part.trackable:
|
||||||
|
# Trackable parts must have integer values for quantity field!
|
||||||
|
if not self.quantity == int(self.quantity):
|
||||||
|
raise ValidationError({
|
||||||
|
'quantity': _('Quantity must be integer value for trackable parts')
|
||||||
|
})
|
||||||
|
|
||||||
|
if self.quantity <= 0:
|
||||||
|
raise ValidationError({
|
||||||
|
'quantity': _('Quantity must be greater than zero')
|
||||||
|
})
|
||||||
|
|
||||||
# The 'supplier_part' field must point to the same part!
|
# The 'supplier_part' field must point to the same part!
|
||||||
try:
|
try:
|
||||||
if self.supplier_part is not None:
|
if self.supplier_part is not None:
|
||||||
|
@ -984,19 +984,23 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
if valid:
|
if valid:
|
||||||
part_id = form['part'].value()
|
part_id = form['part'].value()
|
||||||
try:
|
try:
|
||||||
part = Part.objects.get(id=part_id)
|
self.part = Part.objects.get(id=part_id)
|
||||||
quantity = Decimal(form['quantity'].value())
|
quantity = Decimal(form['quantity'].value())
|
||||||
except (Part.DoesNotExist, ValueError, InvalidOperation):
|
except (Part.DoesNotExist, ValueError, InvalidOperation):
|
||||||
part = None
|
self.part = None
|
||||||
quantity = 1
|
quantity = 1
|
||||||
valid = False
|
valid = False
|
||||||
form.errors['quantity'] = [_('Invalid quantity')]
|
form.errors['quantity'] = [_('Invalid quantity')]
|
||||||
|
|
||||||
if part is None:
|
if quantity <= 0:
|
||||||
|
form.errors['quantity'] = [_('Quantity must be greater than zero')]
|
||||||
|
valid = False
|
||||||
|
|
||||||
|
if self.part is None:
|
||||||
form.errors['part'] = [_('Invalid part selection')]
|
form.errors['part'] = [_('Invalid part selection')]
|
||||||
else:
|
else:
|
||||||
# A trackable part must provide serial numbesr
|
# A trackable part must provide serial numbesr
|
||||||
if part.trackable:
|
if self.part.trackable:
|
||||||
sn = request.POST.get('serial_numbers', '')
|
sn = request.POST.get('serial_numbers', '')
|
||||||
|
|
||||||
sn = str(sn).strip()
|
sn = str(sn).strip()
|
||||||
@ -1009,7 +1013,7 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
existing = []
|
existing = []
|
||||||
|
|
||||||
for serial in serials:
|
for serial in serials:
|
||||||
if not StockItem.check_serial_number(part, serial):
|
if self.part.checkIfSerialNumberExists(serial):
|
||||||
existing.append(serial)
|
existing.append(serial)
|
||||||
|
|
||||||
if len(existing) > 0:
|
if len(existing) > 0:
|
||||||
@ -1024,10 +1028,12 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
|
|
||||||
form_data = form.cleaned_data
|
form_data = form.cleaned_data
|
||||||
|
|
||||||
|
if form.is_valid():
|
||||||
|
|
||||||
for serial in serials:
|
for serial in serials:
|
||||||
# Create a new stock item for each serial number
|
# Create a new stock item for each serial number
|
||||||
item = StockItem(
|
item = StockItem(
|
||||||
part=part,
|
part=self.part,
|
||||||
quantity=1,
|
quantity=1,
|
||||||
serial=serial,
|
serial=serial,
|
||||||
supplier_part=form_data.get('supplier_part'),
|
supplier_part=form_data.get('supplier_part'),
|
||||||
@ -1052,6 +1058,8 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
form.clean()
|
form.clean()
|
||||||
form._post_clean()
|
form._post_clean()
|
||||||
|
|
||||||
|
if form.is_valid():
|
||||||
|
|
||||||
item = form.save(commit=False)
|
item = form.save(commit=False)
|
||||||
item.save(user=request.user)
|
item.save(user=request.user)
|
||||||
|
|
||||||
@ -1059,12 +1067,15 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
data['url'] = item.get_absolute_url()
|
data['url'] = item.get_absolute_url()
|
||||||
data['success'] = _("Created new stock item")
|
data['success'] = _("Created new stock item")
|
||||||
|
|
||||||
|
valid = True
|
||||||
|
|
||||||
else: # Referenced Part object is not marked as "trackable"
|
else: # Referenced Part object is not marked as "trackable"
|
||||||
# For non-serialized items, simply save the form.
|
# For non-serialized items, simply save the form.
|
||||||
# We need to call _post_clean() here because it is prevented in the form implementation
|
# We need to call _post_clean() here because it is prevented in the form implementation
|
||||||
form.clean()
|
form.clean()
|
||||||
form._post_clean()
|
form._post_clean()
|
||||||
|
|
||||||
|
if form.is_valid:
|
||||||
item = form.save(commit=False)
|
item = form.save(commit=False)
|
||||||
item.save(user=request.user)
|
item.save(user=request.user)
|
||||||
|
|
||||||
@ -1072,7 +1083,9 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
data['url'] = item.get_absolute_url()
|
data['url'] = item.get_absolute_url()
|
||||||
data['success'] = _("Created new stock item")
|
data['success'] = _("Created new stock item")
|
||||||
|
|
||||||
data['form_valid'] = valid
|
valid = True
|
||||||
|
|
||||||
|
data['form_valid'] = valid and form.is_valid()
|
||||||
|
|
||||||
return self.renderJsonResponse(request, form, data=data)
|
return self.renderJsonResponse(request, form, data=data)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user