From 4e3702384bcaeea89f7e4c3bcc4db6ae0d061d6d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 28 Apr 2019 23:50:35 +1000 Subject: [PATCH 1/4] Limit SupplierPart option in Part edit form - Only allow selection of SupplierPart that match the Part being edited --- InvenTree/part/views.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index d0f3a1ac01..4d7a861a07 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -169,6 +169,21 @@ class PartEdit(AjaxUpdateView): ajax_form_title = 'Edit Part Properties' context_object_name = 'part' + def get_form(self): + """ Create form for Part editing. + Overrides default get_form() method to limit the choices + for the 'default_supplier' field to SupplierParts that reference this part + """ + + form = super(AjaxUpdateView, self).get_form() + + part = self.get_object() + + form.fields['default_supplier'].queryset = SupplierPart.objects.filter(part=part) + + return form + + class BomExport(AjaxView): From bc12af5994ac1a10eef182d4b2dd369d7ff746f3 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 28 Apr 2019 23:55:21 +1000 Subject: [PATCH 2/4] Remove the 'supplier_part' field when first creating a Part object - As the Part does not yet exist, there are no matching SupplierPart objects --- InvenTree/part/views.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 4d7a861a07..9b350c9fe7 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -91,6 +91,15 @@ class PartCreate(AjaxCreateView): return context + def get_form(self): + form = super(AjaxCreateView, self).get_form() + + # Hide the default_supplier field (there are no matching supplier parts yet!) + #form.fields['default_supplier'].widget.attrs['hidden'] = True + del form.fields['default_supplier'] + + return form + # Pre-fill the category field if a valid category is provided def get_initial(self): """ Get initial data for the new Part object: From 87411293cf87f50acf98ec9cf89ad6f3546f9442 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 28 Apr 2019 23:57:29 +1000 Subject: [PATCH 3/4] docstring improvements --- InvenTree/part/views.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 9b350c9fe7..3bb63fcc7b 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -92,12 +92,15 @@ class PartCreate(AjaxCreateView): return context def get_form(self): + """ Create Form for making new Part object. + Remove the 'default_supplier' field as there are not yet any matching SupplierPart objects + """ form = super(AjaxCreateView, self).get_form() # Hide the default_supplier field (there are no matching supplier parts yet!) #form.fields['default_supplier'].widget.attrs['hidden'] = True del form.fields['default_supplier'] - + return form # Pre-fill the category field if a valid category is provided @@ -449,6 +452,9 @@ class SupplierPartCreate(AjaxCreateView): context_object_name = 'part' def get_form(self): + """ Create Form instance to create a new SupplierPart object. + Hide some fields if they are not appropriate in context + """ form = super(AjaxCreateView, self).get_form() if form.initial.get('supplier', None): From 05333e7e7d482597c8222e206965344810d53440 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 28 Apr 2019 23:58:14 +1000 Subject: [PATCH 4/4] PEP fixes --- InvenTree/part/views.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 3bb63fcc7b..af8559fb01 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -98,7 +98,6 @@ class PartCreate(AjaxCreateView): form = super(AjaxCreateView, self).get_form() # Hide the default_supplier field (there are no matching supplier parts yet!) - #form.fields['default_supplier'].widget.attrs['hidden'] = True del form.fields['default_supplier'] return form @@ -196,7 +195,6 @@ class PartEdit(AjaxUpdateView): return form - class BomExport(AjaxView): model = Part