2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-30 12:36:45 +00:00

Hidden owner field when ownership control is disabled

This commit is contained in:
eeintech 2020-12-02 14:05:45 -05:00
parent de1dfdcc38
commit 1a7a460ba8

View File

@ -11,7 +11,7 @@ from django.views.generic import DetailView, ListView, UpdateView
from django.forms.models import model_to_dict from django.forms.models import model_to_dict
from django.forms import HiddenInput from django.forms import HiddenInput
from django.urls import reverse from django.urls import reverse
from django.contrib.auth.models import User from django.contrib.auth.models import User, Group
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -142,12 +142,14 @@ class StockLocationEdit(AjaxUpdateView):
form.fields['parent'].queryset = parent_choices form.fields['parent'].queryset = parent_choices
# Is ownership control enabled?
stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
if not stock_ownership_control:
form.fields['owner'].widget = HiddenInput()
else:
if location.parent: if location.parent:
form.fields['owner'].initial = location.parent.owner form.fields['owner'].initial = location.parent.owner
if not self.request.user.is_superuser:
# Disable selection if stock ownership control is enabled
stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
if stock_ownership_control:
form.fields['owner'].disabled = True form.fields['owner'].disabled = True
return form return form
@ -1331,7 +1333,10 @@ class StockItemEdit(AjaxUpdateView):
# Is ownership control enabled? # Is ownership control enabled?
stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL') stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
if stock_ownership_control and location: if not stock_ownership_control:
form.fields['owner'].widget = HiddenInput()
else:
if location:
# Check if location has owner # Check if location has owner
if location.owner: if location.owner:
form.fields['owner'].queryset = User.objects.filter(groups=location.owner) form.fields['owner'].queryset = User.objects.filter(groups=location.owner)
@ -1409,16 +1414,22 @@ class StockLocationCreate(AjaxCreateView):
form = super().get_form() form = super().get_form()
# Is ownership control enabled?
stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
if not stock_ownership_control:
form.fields['owner'].widget = HiddenInput()
else:
try: try:
parent = self.get_initial()['parent'] parent_id = form['parent'].value()
parent = StockLocation.objects.get(pk=parent_id)
if parent: if parent:
form.fields['owner'].initial = parent.owner form.fields['owner'].initial = parent.owner
form.fields['owner'].queryset = Group.objects.filter(pk=parent.owner.pk)
# Disable selection if stock ownership control is enabled
stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
if stock_ownership_control:
form.fields['owner'].disabled = True form.fields['owner'].disabled = True
except KeyError: except StockLocation.DoesNotExist:
pass
except ValueError:
pass pass
return form return form
@ -1436,6 +1447,20 @@ class StockLocationCreate(AjaxCreateView):
return self.object return self.object
def validate(self, item, form):
""" Check that owner is set if stock ownership control is enabled """
# parent = form.cleaned_data.get('parent', None)
owner = form.cleaned_data.get('owner', None)
# Is ownership control enabled?
stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
if stock_ownership_control:
if not owner:
form.add_error('owner', _('Owner is required (ownership control is enabled)'))
class StockItemSerialize(AjaxUpdateView): class StockItemSerialize(AjaxUpdateView):
""" View for manually serializing a StockItem """ """ View for manually serializing a StockItem """
@ -1633,7 +1658,10 @@ class StockItemCreate(AjaxCreateView):
# Is ownership control enabled? # Is ownership control enabled?
stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL') stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
if stock_ownership_control and location: if not stock_ownership_control:
form.fields['owner'].widget = HiddenInput()
else:
if location:
# Check if location has owner # Check if location has owner
if location.owner: if location.owner:
queryset = User.objects.filter(groups=location.owner) queryset = User.objects.filter(groups=location.owner)