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
if location.parent: # Is ownership control enabled?
form.fields['owner'].initial = location.parent.owner stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
if not stock_ownership_control:
# Disable selection if stock ownership control is enabled form.fields['owner'].widget = HiddenInput()
stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL') else:
if stock_ownership_control: if location.parent:
form.fields['owner'].initial = location.parent.owner
if not self.request.user.is_superuser:
form.fields['owner'].disabled = True form.fields['owner'].disabled = True
return form return form
@ -1331,10 +1333,13 @@ 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:
# Check if location has owner form.fields['owner'].widget = HiddenInput()
if location.owner: else:
form.fields['owner'].queryset = User.objects.filter(groups=location.owner) if location:
# Check if location has owner
if location.owner:
form.fields['owner'].queryset = User.objects.filter(groups=location.owner)
return form return form
@ -1409,17 +1414,23 @@ class StockLocationCreate(AjaxCreateView):
form = super().get_form() form = super().get_form()
try: # Is ownership control enabled?
parent = self.get_initial()['parent'] stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL')
if parent: if not stock_ownership_control:
form.fields['owner'].initial = parent.owner form.fields['owner'].widget = HiddenInput()
else:
try:
parent_id = form['parent'].value()
parent = StockLocation.objects.get(pk=parent_id)
# Disable selection if stock ownership control is enabled if parent:
stock_ownership_control = InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL') form.fields['owner'].initial = parent.owner
if stock_ownership_control: form.fields['owner'].queryset = Group.objects.filter(pk=parent.owner.pk)
form.fields['owner'].disabled = True form.fields['owner'].disabled = True
except KeyError: except StockLocation.DoesNotExist:
pass pass
except ValueError:
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,13 +1658,16 @@ 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:
# Check if location has owner form.fields['owner'].widget = HiddenInput()
if location.owner: else:
queryset = User.objects.filter(groups=location.owner) if location:
if self.request.user in queryset: # Check if location has owner
form.fields['owner'].initial = self.request.user if location.owner:
form.fields['owner'].queryset = queryset queryset = User.objects.filter(groups=location.owner)
if self.request.user in queryset:
form.fields['owner'].initial = self.request.user
form.fields['owner'].queryset = queryset
return form return form