Enforce 'notes' field for StockItem move

- Better error handling for StockItem.move
This commit is contained in:
Oliver Walters 2019-04-25 23:01:03 +10:00
parent 38fa89d1da
commit 757cd539b2
3 changed files with 34 additions and 13 deletions

View File

@ -225,11 +225,12 @@ function moveStockItems(items, options) {
return;
}
function doMove(location, parts) {
function doMove(location, parts, notes) {
inventreeUpdate("/api/stock/move/",
{
location: location,
'parts[]': parts
'parts[]': parts,
'notes': notes,
},
{
success: function(response) {
@ -267,9 +268,13 @@ function moveStockItems(items, options) {
html += makeOption(loc.pk, loc.name + ' - <i>' + loc.description + '</i>');
}
html += "</select><br><hr>";
html += "</select><br>";
html += "The following stock items will be moved:<br><ul class='list-group'>\n";
html += "<hr><input type='text' id='notes' placeholder='Notes'/>";
html += "<p class='warning-msg' id='note-warning'><i>Note field must be filled</i></p>";
html += "<hr>The following stock items will be moved:<br><ul class='list-group'>\n";
for (i = 0; i < items.length; i++) {
parts.push(items[i].pk);
@ -288,10 +293,19 @@ function moveStockItems(items, options) {
modalSetContent(modal, html);
attachSelect(modal);
$(modal).find('#note-warning').hide();
modalSubmit(modal, function() {
var locId = $(modal).find("#stock-location").val();
doMove(locId, parts);
var notes = $(modal).find('#notes').val();
if (!notes) {
$(modal).find('#note-warning').show();
return false;
}
doMove(locId, parts, notes);
});
},
error: function(error) {
@ -363,7 +377,7 @@ function loadStockTable(table, options) {
return renderLink(row.location.pathstring, row.location.url);
}
else {
return '';
return '<i>No stock location set</i>';
}
}
},

View File

@ -153,6 +153,9 @@ class StockMove(APIView):
errors = []
if u'notes' not in data:
errors.append({'notes': 'Notes field must be supplied'})
for pid in part_list:
try:
part = StockItem.objects.get(pk=pid)
@ -164,7 +167,7 @@ class StockMove(APIView):
raise ValidationError(errors)
for part in parts:
part.move(location, request.user)
part.move(location, data.get('notes'), request.user)
return Response({'success': 'Moved {n} parts to {loc}'.format(
n=len(parts),

View File

@ -221,13 +221,17 @@ class StockItem(models.Model):
@transaction.atomic
def move(self, location, notes, user):
if location.pk == self.location.pk:
return False # raise forms.ValidationError("Cannot move item to its current location")
if location is None:
# TODO - Raise appropriate error (cannot move to blank location)
return False
elif self.location and (location.pk == self.location.pk):
# TODO - Raise appropriate error (cannot move to same location)
return False
msg = "Moved to {loc} (from {src})".format(
loc=location.name,
src=self.location.name
)
msg = "Moved to {loc}".format(loc=str(location))
if self.location:
msg += " (from {loc})".format(loc=str(self.location))
self.location = location
self.save()