Updated StockItem create/edit view with ownership control

This commit is contained in:
eeintech 2020-12-02 13:25:33 -05:00
parent 2d7461f609
commit c66ac2579e
2 changed files with 56 additions and 2 deletions

View File

@ -11,6 +11,7 @@ from django.views.generic import DetailView, ListView, UpdateView
from django.forms.models import model_to_dict
from django.forms import HiddenInput
from django.urls import reverse
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
@ -1326,8 +1327,28 @@ class StockItemEdit(AjaxUpdateView):
if not item.part.trackable and not item.serialized:
form.fields['serial'].widget = HiddenInput()
location = item.location
# Is ownership control enabled?
stock_owner_setting_enable = InvenTreeSetting.get_setting('STOCK_OWNER')
if stock_owner_setting_enable and location:
# Check if location has owner
if location.owner:
form.fields['owner'].queryset = User.objects.filter(groups=location.owner)
return form
def validate(self, item, form):
""" Check that owner is set if stock ownership control is enabled """
owner = form.cleaned_data.get('owner', None)
# Is ownership control enabled?
stock_owner_setting_enable = InvenTreeSetting.get_setting('STOCK_OWNER')
if not owner and stock_owner_setting_enable:
form.add_error('owner', _('Owner is required (ownership control is enabled)'))
class StockItemConvert(AjaxUpdateView):
"""
@ -1602,6 +1623,23 @@ class StockItemCreate(AjaxCreateView):
# Otherwise if the user has selected a SupplierPart, we know what Part they meant!
if form['supplier_part'].value() is not None:
pass
location = None
try:
loc_id = form['location'].value()
location = StockLocation.objects.get(pk=loc_id)
except ValueError:
pass
# Is ownership control enabled?
stock_owner_setting_enable = InvenTreeSetting.get_setting('STOCK_OWNER')
if stock_owner_setting_enable and location:
# Check if location has owner
if location.owner:
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
@ -1674,10 +1712,17 @@ class StockItemCreate(AjaxCreateView):
data = form.cleaned_data
part = data['part']
part = data.get('part', None)
quantity = data.get('quantity', None)
location = data.get('location', None)
owner = data.get('owner', None)
if not part:
return
if not quantity:
return
@ -1708,6 +1753,15 @@ class StockItemCreate(AjaxCreateView):
_('Serial numbers already exist') + ': ' + exists
)
# Is ownership control enabled?
stock_owner_setting_enable = InvenTreeSetting.get_setting('STOCK_OWNER')
if stock_owner_setting_enable:
# Check if owner is set
if not owner:
form.add_error('owner', _('Owner is required (ownership control is enabled)'))
return
def save(self, form, **kwargs):
"""
Create a new StockItem based on the provided form data.

View File

@ -12,7 +12,7 @@
{% if read_only %}
{% else %}
<!-- Check permissions and owner -->
{% if owner_enable.value == "False" or owner_enable.value == "True" and location.owner in user.groups.all %}
{% if owner_enable.value == "False" or owner_enable.value == "True" and location.owner in user.groups.all or user.is_superuser %}
{% if roles.stock.add %}
<button class="btn btn-success" id='item-create'>
<span class='fas fa-plus-circle'></span> {% trans "New Stock Item" %}