mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Updated StockLocation create/edit view with ownership control
This commit is contained in:
parent
c9b3c16c6f
commit
2d7461f609
@ -35,6 +35,7 @@ from label.models import StockItemLabel
|
|||||||
from .models import StockItem, StockLocation, StockItemTracking, StockItemAttachment, StockItemTestResult
|
from .models import StockItem, StockLocation, StockItemTracking, StockItemAttachment, StockItemTestResult
|
||||||
|
|
||||||
import common.settings
|
import common.settings
|
||||||
|
from common.models import InvenTreeSetting
|
||||||
|
|
||||||
from .admin import StockItemResource
|
from .admin import StockItemResource
|
||||||
|
|
||||||
@ -127,6 +128,7 @@ class StockLocationEdit(AjaxUpdateView):
|
|||||||
""" Customize form data for StockLocation editing.
|
""" Customize form data for StockLocation editing.
|
||||||
|
|
||||||
Limit the choices for 'parent' field to those which make sense.
|
Limit the choices for 'parent' field to those which make sense.
|
||||||
|
If ownership control is enabled and location has parent, disable owner field.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
form = super(AjaxUpdateView, self).get_form()
|
form = super(AjaxUpdateView, self).get_form()
|
||||||
@ -139,8 +141,31 @@ class StockLocationEdit(AjaxUpdateView):
|
|||||||
|
|
||||||
form.fields['parent'].queryset = parent_choices
|
form.fields['parent'].queryset = parent_choices
|
||||||
|
|
||||||
|
if location.parent:
|
||||||
|
form.fields['owner'].initial = location.parent.owner
|
||||||
|
|
||||||
|
# Disable selection if stock ownership control is enabled
|
||||||
|
stock_owner_setting_enable = InvenTreeSetting.get_setting('STOCK_OWNER')
|
||||||
|
if stock_owner_setting_enable:
|
||||||
|
form.fields['owner'].disabled = True
|
||||||
|
|
||||||
return form
|
return form
|
||||||
|
|
||||||
|
def save(self, object, form, **kwargs):
|
||||||
|
""" If location has children and ownership control is enabled:
|
||||||
|
- update all children's owners with location's owner
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.object = form.save()
|
||||||
|
|
||||||
|
stock_owner_setting_enable = InvenTreeSetting.get_setting('STOCK_OWNER')
|
||||||
|
if self.object.get_children() and stock_owner_setting_enable:
|
||||||
|
for child in self.object.get_children():
|
||||||
|
child.owner = self.object.owner
|
||||||
|
child.save()
|
||||||
|
|
||||||
|
return self.object
|
||||||
|
|
||||||
|
|
||||||
class StockLocationQRCode(QRCodeView):
|
class StockLocationQRCode(QRCodeView):
|
||||||
""" View for displaying a QR code for a StockLocation object """
|
""" View for displaying a QR code for a StockLocation object """
|
||||||
@ -1355,6 +1380,41 @@ class StockLocationCreate(AjaxCreateView):
|
|||||||
|
|
||||||
return initials
|
return initials
|
||||||
|
|
||||||
|
def get_form(self):
|
||||||
|
""" Disable owner field when:
|
||||||
|
- creating child location
|
||||||
|
- and stock ownership control is enable
|
||||||
|
"""
|
||||||
|
|
||||||
|
form = super().get_form()
|
||||||
|
|
||||||
|
try:
|
||||||
|
parent = self.get_initial()['parent']
|
||||||
|
if parent:
|
||||||
|
form.fields['owner'].initial = parent.owner
|
||||||
|
|
||||||
|
# Disable selection if stock ownership control is enabled
|
||||||
|
stock_owner_setting_enable = InvenTreeSetting.get_setting('STOCK_OWNER')
|
||||||
|
if stock_owner_setting_enable:
|
||||||
|
form.fields['owner'].disabled = True
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return form
|
||||||
|
|
||||||
|
def save(self, form):
|
||||||
|
""" If parent location exists then use it to set the owner """
|
||||||
|
|
||||||
|
self.object = form.save(commit=False)
|
||||||
|
|
||||||
|
parent = form.cleaned_data.get('parent', None)
|
||||||
|
if parent:
|
||||||
|
self.object.owner = parent.owner
|
||||||
|
|
||||||
|
self.object.save()
|
||||||
|
|
||||||
|
return self.object
|
||||||
|
|
||||||
|
|
||||||
class StockItemSerialize(AjaxUpdateView):
|
class StockItemSerialize(AjaxUpdateView):
|
||||||
""" View for manually serializing a StockItem """
|
""" View for manually serializing a StockItem """
|
||||||
|
Loading…
Reference in New Issue
Block a user