mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 20:16:44 +00:00
Updated StockItem create/edit view with ownership control
This commit is contained in:
parent
2d7461f609
commit
c66ac2579e
@ -11,6 +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.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
|
|
||||||
@ -1326,8 +1327,28 @@ class StockItemEdit(AjaxUpdateView):
|
|||||||
if not item.part.trackable and not item.serialized:
|
if not item.part.trackable and not item.serialized:
|
||||||
form.fields['serial'].widget = HiddenInput()
|
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
|
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):
|
class StockItemConvert(AjaxUpdateView):
|
||||||
"""
|
"""
|
||||||
@ -1602,6 +1623,23 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
# Otherwise if the user has selected a SupplierPart, we know what Part they meant!
|
# Otherwise if the user has selected a SupplierPart, we know what Part they meant!
|
||||||
if form['supplier_part'].value() is not None:
|
if form['supplier_part'].value() is not None:
|
||||||
pass
|
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
|
return form
|
||||||
|
|
||||||
@ -1674,10 +1712,17 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
|
|
||||||
data = form.cleaned_data
|
data = form.cleaned_data
|
||||||
|
|
||||||
part = data['part']
|
part = data.get('part', None)
|
||||||
|
|
||||||
quantity = data.get('quantity', None)
|
quantity = data.get('quantity', None)
|
||||||
|
|
||||||
|
location = data.get('location', None)
|
||||||
|
|
||||||
|
owner = data.get('owner', None)
|
||||||
|
|
||||||
|
if not part:
|
||||||
|
return
|
||||||
|
|
||||||
if not quantity:
|
if not quantity:
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -1708,6 +1753,15 @@ class StockItemCreate(AjaxCreateView):
|
|||||||
_('Serial numbers already exist') + ': ' + exists
|
_('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):
|
def save(self, form, **kwargs):
|
||||||
"""
|
"""
|
||||||
Create a new StockItem based on the provided form data.
|
Create a new StockItem based on the provided form data.
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
{% if read_only %}
|
{% if read_only %}
|
||||||
{% else %}
|
{% else %}
|
||||||
<!-- Check permissions and owner -->
|
<!-- 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 %}
|
{% if roles.stock.add %}
|
||||||
<button class="btn btn-success" id='item-create'>
|
<button class="btn btn-success" id='item-create'>
|
||||||
<span class='fas fa-plus-circle'></span> {% trans "New Stock Item" %}
|
<span class='fas fa-plus-circle'></span> {% trans "New Stock Item" %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user