From d3219470265f69b9a74363facfe0ab4b23a4b530 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 29 May 2019 22:38:42 +1000 Subject: [PATCH] Add stock item fields for each item passed to the form --- InvenTree/stock/forms.py | 5 +++++ InvenTree/stock/views.py | 20 ++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 6614d67d72..5f22602a39 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -61,10 +61,15 @@ class MoveStockItemForm(forms.ModelForm): confirm = forms.BooleanField(required=False, initial=False, label='Confirm Stock Movement', help_text='Confirm movement of stock items') def __init__(self, *args, **kwargs): + stock_items = kwargs.pop('stock_items', []) + super(MoveStockItemForm, self).__init__(*args, **kwargs) self.fields['location'].choices = self.get_location_choices() + for item in stock_items: + self.fields['stock_item_{id}'.format(id=item.id)] = forms.IntegerField(required=True, initial=item.quantity, help_text='Quantity for ' + str(item)) + class Meta: model = StockItem diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 990287202d..44593f3c42 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -131,6 +131,7 @@ class StockItemMoveMultiple(AjaxView, FormMixin): ajax_template_name = 'stock/stock_move.html' ajax_form_title = 'Move Stock' form_class = MoveStockItemForm + items = [] def get_items(self, item_list): """ Return list of stock items. """ @@ -145,20 +146,27 @@ class StockItemMoveMultiple(AjaxView, FormMixin): return items + def get_form_kwargs(self): + + args = super().get_form_kwargs() + + args['stock_items'] = self.get_items(self.items) + + return args + def get(self, request, *args, **kwargs): - item_list = request.GET.getlist('stock[]') - - items = self.get_items(item_list) + # Save list of items! + self.items = request.GET.getlist('stock[]') - print(items) - - return self.renderJsonResponse(request, self.form_class()) + return self.renderJsonResponse(request, self.get_form()) def post(self, request, *args, **kwargs): form = self.get_form() + print(request.POST) + valid = form.is_valid() print("Valid:", valid)