Logic fixes for CreateStockItem form

- Improved data validation
- Fix bug where form was not checked for validity
This commit is contained in:
Oliver Walters 2020-05-16 12:04:53 +10:00
parent 3d0bea15ae
commit 3df8f33080
2 changed files with 59 additions and 34 deletions

View File

@ -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:

View File

@ -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,24 +1028,26 @@ class StockItemCreate(AjaxCreateView):
form_data = form.cleaned_data form_data = form.cleaned_data
for serial in serials: if form.is_valid():
# Create a new stock item for each serial number
item = StockItem(
part=part,
quantity=1,
serial=serial,
supplier_part=form_data.get('supplier_part'),
location=form_data.get('location'),
batch=form_data.get('batch'),
delete_on_deplete=False,
status=form_data.get('status'),
link=form_data.get('link'),
)
item.save(user=request.user) for serial in serials:
# Create a new stock item for each serial number
item = StockItem(
part=self.part,
quantity=1,
serial=serial,
supplier_part=form_data.get('supplier_part'),
location=form_data.get('location'),
batch=form_data.get('batch'),
delete_on_deplete=False,
status=form_data.get('status'),
link=form_data.get('link'),
)
data['success'] = _('Created {n} new stock items'.format(n=len(serials))) item.save(user=request.user)
valid = True
data['success'] = _('Created {n} new stock items'.format(n=len(serials)))
valid = True
except ValidationError as e: except ValidationError as e:
form.errors['serial_numbers'] = e.messages form.errors['serial_numbers'] = e.messages
@ -1052,12 +1058,16 @@ class StockItemCreate(AjaxCreateView):
form.clean() form.clean()
form._post_clean() form._post_clean()
item = form.save(commit=False) if form.is_valid():
item.save(user=request.user)
data['pk'] = item.pk item = form.save(commit=False)
data['url'] = item.get_absolute_url() item.save(user=request.user)
data['success'] = _("Created new stock item")
data['pk'] = item.pk
data['url'] = item.get_absolute_url()
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.
@ -1065,14 +1075,17 @@ class StockItemCreate(AjaxCreateView):
form.clean() form.clean()
form._post_clean() form._post_clean()
item = form.save(commit=False) if form.is_valid:
item.save(user=request.user) item = form.save(commit=False)
item.save(user=request.user)
data['pk'] = item.pk data['pk'] = item.pk
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)