Merge remote-tracking branch 'inventree/master'

This commit is contained in:
Oliver Walters 2020-05-12 09:16:04 +10:00
commit e4872d9f67
5 changed files with 59 additions and 4 deletions

View File

@ -5,6 +5,8 @@ JSON API for the Stock app
from django_filters.rest_framework import FilterSet, DjangoFilterBackend from django_filters.rest_framework import FilterSet, DjangoFilterBackend
from django_filters import NumberFilter from django_filters import NumberFilter
from rest_framework import status
from django.conf.urls import url, include from django.conf.urls import url, include
from django.urls import reverse from django.urls import reverse
from django.http import JsonResponse from django.http import JsonResponse
@ -658,6 +660,43 @@ class StockTrackingList(generics.ListCreateAPIView):
serializer_class = StockTrackingSerializer serializer_class = StockTrackingSerializer
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
def get_serializer(self, *args, **kwargs):
try:
kwargs['item_detail'] = str2bool(self.request.query_params.get('item_detail', False))
except:
pass
try:
kwargs['user_detail'] = str2bool(self.request.query_params.get('user_detail', False))
except:
pass
kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs)
def create(self, request, *args, **kwargs):
""" Create a new StockItemTracking object
Here we override the default 'create' implementation,
to save the user information associated with the request object.
"""
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
# Record the user who created this Part object
item = serializer.save()
item.user = request.user
item.system = False
# quantity field cannot be explicitly adjusted here
item.quantity = item.item.quantity
item.save()
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
filter_backends = [ filter_backends = [
DjangoFilterBackend, DjangoFilterBackend,
filters.SearchFilter, filters.SearchFilter,

View File

@ -207,11 +207,24 @@ class StockItemAttachmentSerializer(InvenTreeModelSerializer):
class StockTrackingSerializer(InvenTreeModelSerializer): class StockTrackingSerializer(InvenTreeModelSerializer):
""" Serializer for StockItemTracking model """ """ Serializer for StockItemTracking model """
def __init__(self, *args, **kwargs):
item_detail = kwargs.pop('item_detail', False)
user_detail = kwargs.pop('user_detail', False)
super().__init__(*args, **kwargs)
if item_detail is not True:
self.fields.pop('item_detail')
if user_detail is not True:
self.fields.pop('user_detail')
url = serializers.CharField(source='get_absolute_url', read_only=True) url = serializers.CharField(source='get_absolute_url', read_only=True)
user = UserSerializerBrief(many=False, read_only=True) item_detail = StockItemSerializerBrief(source='item', many=False, read_only=True)
item = StockItemSerializerBrief(many=False, read_only=True) user_detail = UserSerializerBrief(source='user', many=False, read_only=True)
class Meta: class Meta:
model = StockItemTracking model = StockItemTracking
@ -219,12 +232,14 @@ class StockTrackingSerializer(InvenTreeModelSerializer):
'pk', 'pk',
'url', 'url',
'item', 'item',
'item_detail',
'date', 'date',
'title', 'title',
'notes', 'notes',
'link', 'link',
'quantity', 'quantity',
'user', 'user',
'user_detail',
'system', 'system',
] ]

View File

@ -38,6 +38,7 @@
return { return {
ordering: '-date', ordering: '-date',
item: {{ item.pk }}, item: {{ item.pk }},
user_detail: true,
}; };
}, },
url: "{% url 'api-stock-track' %}", url: "{% url 'api-stock-track' %}",

View File

@ -6,7 +6,7 @@
{% endblock %} {% endblock %}
{% block settings %} {% block settings %}
<h4>{% transa "Part Parameter Templates" %}</h4> <h4>{% trans "Part Parameter Templates" %}</h4>
<div id='param-buttons'> <div id='param-buttons'>
<button class='btn btn-success' id='new-param'>{% trans "New Parameter" %}</button> <button class='btn btn-success' id='new-param'>{% trans "New Parameter" %}</button>

View File

@ -448,7 +448,7 @@ function loadStockTrackingTable(table, options) {
if (value) if (value)
{ {
// TODO - Format the user's first and last names // TODO - Format the user's first and last names
return value.username; return row.user_detail.username;
} }
else else
{ {