From 2949289fabe9a4ed6468ff86e71daf6ee98c26e9 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 6 Feb 2020 23:11:47 +1100 Subject: [PATCH 1/2] Fix for bug a) - Would not create new StockItem for trackable part if Serial Numbers not provided --- InvenTree/stock/forms.py | 1 - InvenTree/stock/views.py | 14 +++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 5c84f3b4df..ca206ca49d 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -44,7 +44,6 @@ class CreateStockItemForm(HelperForm): 'serial_numbers', 'delete_on_deplete', 'status', - 'notes', 'URL', ] diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index cb478e42f3..9b5c65f37a 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -911,7 +911,19 @@ class StockItemCreate(AjaxCreateView): form.errors['serial_numbers'] = e.messages valid = False - else: + else: + # We have a serialized part, but no serial numbers specified... + form.clean() + form._post_clean() + + item = form.save(commit=False) + item.save(user=request.user) + + data['pk'] = item.pk + data['url'] = item.get_absolute_url() + data['success'] = _("Created new stock item") + + else: # Referenced Part object is not marked as "trackable" # For non-serialized items, simply save the form. # We need to call _post_clean() here because it is prevented in the form implementation form.clean() From 4bd4f2a0a3cf1138987f1104a28d0f0adddd8bef Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 6 Feb 2020 23:22:55 +1100 Subject: [PATCH 2/2] Fix for bug b) - Don't attempt to save if there are duplicates - Fix overwritten variable name - Provide correct return data to the form --- .../migrations/0020_auto_20200206_1213.py | 19 +++++++++ InvenTree/stock/models.py | 2 +- InvenTree/stock/views.py | 41 ++++++++++--------- 3 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 InvenTree/stock/migrations/0020_auto_20200206_1213.py diff --git a/InvenTree/stock/migrations/0020_auto_20200206_1213.py b/InvenTree/stock/migrations/0020_auto_20200206_1213.py new file mode 100644 index 0000000000..34be29fd39 --- /dev/null +++ b/InvenTree/stock/migrations/0020_auto_20200206_1213.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.9 on 2020-02-06 12:13 + +from django.db import migrations +import markdownx.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stock', '0019_auto_20200202_1024'), + ] + + operations = [ + migrations.AlterField( + model_name='stockitem', + name='notes', + field=markdownx.models.MarkdownxField(blank=True, help_text='Stock Item Notes', null=True), + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index d0bc23f14d..57f390415f 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -360,7 +360,7 @@ class StockItem(models.Model): choices=StockStatus.items(), validators=[MinValueValidator(0)]) - notes = MarkdownxField(blank=True, help_text=_('Stock Item Notes')) + notes = MarkdownxField(blank=True, null=True, help_text=_('Stock Item Notes')) # If stock item is incoming, an (optional) ETA field # expected_arrival = models.DateField(null=True, blank=True) diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 9b5c65f37a..9d5e2b5dc4 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -884,28 +884,31 @@ class StockItemCreate(AjaxCreateView): form.errors['serial_numbers'] = [_('The following serial numbers already exist: ({sn})'.format(sn=exists))] valid = False - # At this point we have a list of serial numbers which we know are valid, - # and do not currently exist - form.clean() + else: + # At this point we have a list of serial numbers which we know are valid, + # and do not currently exist + form.clean() - data = form.cleaned_data + form_data = form.cleaned_data - for serial in serials: - # Create a new stock item for each serial number - item = StockItem( - part=part, - quantity=1, - serial=serial, - supplier_part=data.get('supplier_part'), - location=data.get('location'), - batch=data.get('batch'), - delete_on_deplete=False, - status=data.get('status'), - notes=data.get('notes'), - URL=data.get('URL'), - ) + for serial in serials: + # 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'), + URL=form_data.get('URL'), + ) - item.save(user=request.user) + item.save(user=request.user) + + data['success'] = _('Created {n} new stock items'.format(n=len(serials))) + valid = True except ValidationError as e: form.errors['serial_numbers'] = e.messages