Calculate initial values for the view

This commit is contained in:
Oliver Walters 2020-10-04 21:02:20 +11:00
parent fd22e713ff
commit a686500df1
2 changed files with 44 additions and 16 deletions

View File

@ -600,12 +600,13 @@ class StockItem(MPTTModel):
return self.installedItemCount() > 0 return self.installedItemCount() > 0
@transaction.atomic @transaction.atomic
def installIntoStockItem(self, otherItem, user, notes): def installIntoStockItem(self, otherItem, quantity, user, notes):
""" """
Install this stock item into another stock item. Install this stock item into another stock item.
Args Args
otherItem: The stock item to install this item into otherItem: The stock item to install this item into
quantity: The quantity of stock to install
user: The user performing the operation user: The user performing the operation
notes: Any notes associated with the operation notes: Any notes associated with the operation
""" """

View File

@ -699,25 +699,52 @@ class StockItemInstall(AjaxUpdateView):
form_class = StockForms.InstallStockForm form_class = StockForms.InstallStockForm
ajax_form_title = _('Install Stock Item') ajax_form_title = _('Install Stock Item')
def get_stock_items(self):
"""
Return a list of stock items suitable for displaying to the user.
Requirements:
- Items must be in stock
Filters:
- Items can be filtered by Part reference
"""
items = StockItem.objects.filter(StockItem.IN_STOCK_FILTER)
# Filter by Part association
try:
part = self.request.GET.get('part', None)
print(self.request.GET)
if part is not None:
part = Part.objects.get(pk=part)
items = items.filter(part=part)
except (ValueError, Part.DoesNotExist):
pass
return items
def get_initial(self):
initials = super().get_initial()
items = self.get_stock_items()
# If there is a single stock item available, we can use it!
if items.count() == 1:
item = items.first()
initials['stock_item'] = item.pk
initials['quantity_to_install'] = item.quantity
return initials
def get_form(self): def get_form(self):
form = super().get_form() form = super().get_form()
queryset = form.fields['stock_item'].queryset form.fields['stock_item'].queryset = self.get_stock_items()
part = self.request.GET.get('part', None)
# Filter the available stock items based on the Part reference
if part:
try:
part = Part.objects.get(pk=part)
queryset = queryset.filter(part=part)
except (ValueError, Part.DoesNotExist):
pass
form.fields['stock_item'].queryset = queryset
return form return form