Improved 'move stock' form

- Better error checking on the form (provides form validation messages to user)
This commit is contained in:
Oliver Walters 2019-04-13 00:08:13 +10:00
parent f451d31f00
commit 93bb0bf6f4
3 changed files with 25 additions and 14 deletions

View File

@ -38,6 +38,8 @@ class CreateStockItemForm(HelperForm):
class MoveStockItemForm(forms.ModelForm):
note = forms.CharField(label='Notes', required=True)
class Meta:
model = StockItem

View File

@ -217,21 +217,24 @@ class StockItem(models.Model):
track.save()
@transaction.atomic
def move(self, location, user):
def move(self, location, notes, user):
if location == self.location:
return
if location.pk == self.location.pk:
return False # raise forms.ValidationError("Cannot move item to its current location")
note = "Moved to {loc}".format(loc=location.name)
msg = "Moved to {loc} (from {src})".format(loc=location.name,
src=self.location.name)
self.location = location
self.save()
self.add_transaction_note('Transfer',
self.add_transaction_note(msg,
user,
notes=note,
notes=notes,
system=True)
return True
@transaction.atomic
def stocktake(self, count, user, notes=''):

View File

@ -142,20 +142,26 @@ class StockItemMove(AjaxUpdateView):
if form.is_valid():
obj = form.save()
obj = self.get_object()
try:
loc = StockLocation.objects.get(pk=form['location'].value())
loc_path = loc.pathstring
loc_id = form['location'].value()
if loc_id:
loc = StockLocation.objects.get(pk=form['location'].value())
if str(loc.pk) == str(obj.pk):
form.errors['location'] = ['Item is already in this location']
else:
obj.move(loc, form['note'].value(), request.user)
else:
form.errors['location'] = ['Cannot move to an empty location']
except StockLocation.DoesNotExist:
loc_path = ''
obj.add_transaction_note("Moved item to '{where}'".format(where=loc_path),
request.user,
system=True)
form.errors['location'] = ['Location does not exist']
data = {
'form_valid': form.is_valid(),
'form_valid': form.is_valid() and len(form.errors) == 0,
}
return self.renderJsonResponse(request, form, data)