mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add admin / serializer / API for PartLabel model
This commit is contained in:
parent
a1a4bddcc6
commit
1830467487
@ -3,7 +3,7 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from .models import StockItemLabel, StockLocationLabel
|
from .models import StockItemLabel, StockLocationLabel, PartLabel
|
||||||
|
|
||||||
|
|
||||||
class LabelAdmin(admin.ModelAdmin):
|
class LabelAdmin(admin.ModelAdmin):
|
||||||
@ -13,3 +13,4 @@ class LabelAdmin(admin.ModelAdmin):
|
|||||||
|
|
||||||
admin.site.register(StockItemLabel, LabelAdmin)
|
admin.site.register(StockItemLabel, LabelAdmin)
|
||||||
admin.site.register(StockLocationLabel, LabelAdmin)
|
admin.site.register(StockLocationLabel, LabelAdmin)
|
||||||
|
admin.site.register(PartLabel, LabelAdmin)
|
||||||
|
@ -15,9 +15,10 @@ import InvenTree.helpers
|
|||||||
import common.models
|
import common.models
|
||||||
|
|
||||||
from stock.models import StockItem, StockLocation
|
from stock.models import StockItem, StockLocation
|
||||||
|
from part.models import Part
|
||||||
|
|
||||||
from .models import StockItemLabel, StockLocationLabel
|
from .models import StockItemLabel, StockLocationLabel, PartLabel
|
||||||
from .serializers import StockItemLabelSerializer, StockLocationLabelSerializer
|
from .serializers import StockItemLabelSerializer, StockLocationLabelSerializer, PartLabelSerializer
|
||||||
|
|
||||||
|
|
||||||
class LabelListView(generics.ListAPIView):
|
class LabelListView(generics.ListAPIView):
|
||||||
@ -132,6 +133,7 @@ class StockItemLabelMixin:
|
|||||||
for key in ['item', 'item[]', 'items', 'items[]']:
|
for key in ['item', 'item[]', 'items', 'items[]']:
|
||||||
if key in params:
|
if key in params:
|
||||||
items = params.getlist(key, [])
|
items = params.getlist(key, [])
|
||||||
|
break
|
||||||
|
|
||||||
valid_ids = []
|
valid_ids = []
|
||||||
|
|
||||||
@ -376,6 +378,112 @@ class StockLocationLabelPrint(generics.RetrieveAPIView, StockLocationLabelMixin,
|
|||||||
return self.print(request, locations)
|
return self.print(request, locations)
|
||||||
|
|
||||||
|
|
||||||
|
class PartLabelMixin:
|
||||||
|
"""
|
||||||
|
Mixin for extracting Part objects from query parameters
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get_parts(self):
|
||||||
|
"""
|
||||||
|
Return a list of requested Part objects
|
||||||
|
"""
|
||||||
|
|
||||||
|
parts = []
|
||||||
|
|
||||||
|
params = self.request.query_params
|
||||||
|
|
||||||
|
for key in ['part', 'part[]', 'parts', 'parts[]']:
|
||||||
|
if key in params:
|
||||||
|
parts = parts.getlist(key, [])
|
||||||
|
break
|
||||||
|
|
||||||
|
valid_ids = []
|
||||||
|
|
||||||
|
for part in parts:
|
||||||
|
try:
|
||||||
|
valid_ids.append(int(part))
|
||||||
|
except (ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# List of Part objects which match provided values
|
||||||
|
return Part.objects.filter(pk__in=valid_ids)
|
||||||
|
|
||||||
|
|
||||||
|
class PartLabelList(LabelListView, PartLabelMixin):
|
||||||
|
"""
|
||||||
|
API endpoint for viewing list of PartLabel objects
|
||||||
|
"""
|
||||||
|
|
||||||
|
queryset = PartLabel.objects.all()
|
||||||
|
serializer_class = PartLabelSerializer
|
||||||
|
|
||||||
|
def filter_queryset(self, queryset):
|
||||||
|
|
||||||
|
queryset = super().filter_queryset(queryset)
|
||||||
|
|
||||||
|
parts = self.get_parts()
|
||||||
|
|
||||||
|
if len(parts) > 0:
|
||||||
|
|
||||||
|
valid_label_ids = set()
|
||||||
|
|
||||||
|
for label in queryset.all():
|
||||||
|
|
||||||
|
matches = True
|
||||||
|
|
||||||
|
try:
|
||||||
|
filters = InvenTree.helpers.validateFilterString(label.filters)
|
||||||
|
except ValidationError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for part in parts:
|
||||||
|
|
||||||
|
part_query = Part.objects.filter(pk=part.pk)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if not part_query.filter(**filters).exists():
|
||||||
|
matches = False
|
||||||
|
break
|
||||||
|
except FieldError:
|
||||||
|
matches = False
|
||||||
|
break
|
||||||
|
|
||||||
|
if matches:
|
||||||
|
valid_label_ids.add(label.pk)
|
||||||
|
|
||||||
|
# Reduce queryset to only valid matches
|
||||||
|
queryset = queryset.filter(pk__in=[pk for pk in valid_label_ids])
|
||||||
|
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
|
class PartLabelDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
"""
|
||||||
|
API endpoint for a single PartLabel object
|
||||||
|
"""
|
||||||
|
|
||||||
|
queryset = PartLabel.objects.all()
|
||||||
|
serializer_class = PartLabelSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class PartLabelPrint(generics.RetrieveAPIView, PartLabelMixin, LabelPrintMixin):
|
||||||
|
"""
|
||||||
|
API endpoint for printing a PartLabel object
|
||||||
|
"""
|
||||||
|
|
||||||
|
queryset = PartLabel.objects.all()
|
||||||
|
serializer_class = PartLabelSerializer
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Check if valid part(s) have been provided
|
||||||
|
"""
|
||||||
|
|
||||||
|
parts = self.get_parts()
|
||||||
|
|
||||||
|
return self.print(request, parts)
|
||||||
|
|
||||||
|
|
||||||
label_api_urls = [
|
label_api_urls = [
|
||||||
|
|
||||||
# Stock item labels
|
# Stock item labels
|
||||||
@ -401,4 +509,16 @@ label_api_urls = [
|
|||||||
# List view
|
# List view
|
||||||
url(r'^.*$', StockLocationLabelList.as_view(), name='api-stocklocation-label-list'),
|
url(r'^.*$', StockLocationLabelList.as_view(), name='api-stocklocation-label-list'),
|
||||||
])),
|
])),
|
||||||
|
|
||||||
|
# Part labels
|
||||||
|
url(r'^part/', include([
|
||||||
|
# Detail views
|
||||||
|
url(r'^(?P<pk>\d+)/', include([
|
||||||
|
url(r'^print/', PartLabelPrint.as_view(), name='api-part-label-print'),
|
||||||
|
url(r'^.*$', PartLabelDetail.as_view(), name='api-part-label-detail'),
|
||||||
|
])),
|
||||||
|
|
||||||
|
# List view
|
||||||
|
url(r'^.*$', PartLabelList.as_view(), name='api-part-label-list'),
|
||||||
|
])),
|
||||||
]
|
]
|
||||||
|
@ -12,7 +12,6 @@ import datetime
|
|||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.fields import Field
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.core.validators import FileExtensionValidator, MinValueValidator
|
from django.core.validators import FileExtensionValidator, MinValueValidator
|
||||||
from django.core.exceptions import ValidationError, FieldError
|
from django.core.exceptions import ValidationError, FieldError
|
||||||
|
@ -4,7 +4,7 @@ from __future__ import unicode_literals
|
|||||||
from InvenTree.serializers import InvenTreeModelSerializer
|
from InvenTree.serializers import InvenTreeModelSerializer
|
||||||
from InvenTree.serializers import InvenTreeAttachmentSerializerField
|
from InvenTree.serializers import InvenTreeAttachmentSerializerField
|
||||||
|
|
||||||
from .models import StockItemLabel, StockLocationLabel
|
from .models import StockItemLabel, StockLocationLabel, PartLabel
|
||||||
|
|
||||||
|
|
||||||
class StockItemLabelSerializer(InvenTreeModelSerializer):
|
class StockItemLabelSerializer(InvenTreeModelSerializer):
|
||||||
@ -43,3 +43,22 @@ class StockLocationLabelSerializer(InvenTreeModelSerializer):
|
|||||||
'filters',
|
'filters',
|
||||||
'enabled',
|
'enabled',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class PartLabelSerializer(InvenTreeModelSerializer):
|
||||||
|
"""
|
||||||
|
Serializes a PartLabel object
|
||||||
|
"""
|
||||||
|
|
||||||
|
label = InvenTreeAttachmentSerializerField(required=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = PartLabel
|
||||||
|
fields = [
|
||||||
|
'pk',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'label',
|
||||||
|
'filters',
|
||||||
|
'enabled',
|
||||||
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user