mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 03:56:43 +00:00
Refactor label API code (#4978)
* Refactor label API code - Add common base class for serializers * More import cleanup
This commit is contained in:
parent
f65281c801
commit
a3150d9cb3
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from .models import PartLabel, StockItemLabel, StockLocationLabel
|
import label.models
|
||||||
|
|
||||||
|
|
||||||
class LabelAdmin(admin.ModelAdmin):
|
class LabelAdmin(admin.ModelAdmin):
|
||||||
@ -10,6 +10,6 @@ class LabelAdmin(admin.ModelAdmin):
|
|||||||
list_display = ('name', 'description', 'label', 'filters', 'enabled')
|
list_display = ('name', 'description', 'label', 'filters', 'enabled')
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(StockItemLabel, LabelAdmin)
|
admin.site.register(label.models.StockItemLabel, LabelAdmin)
|
||||||
admin.site.register(StockLocationLabel, LabelAdmin)
|
admin.site.register(label.models.StockLocationLabel, LabelAdmin)
|
||||||
admin.site.register(PartLabel, LabelAdmin)
|
admin.site.register(label.models.PartLabel, LabelAdmin)
|
||||||
|
@ -12,6 +12,8 @@ from rest_framework.exceptions import NotFound
|
|||||||
|
|
||||||
import common.models
|
import common.models
|
||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
|
import label.models
|
||||||
|
import label.serializers
|
||||||
from InvenTree.api import MetadataView
|
from InvenTree.api import MetadataView
|
||||||
from InvenTree.filters import InvenTreeSearchFilter
|
from InvenTree.filters import InvenTreeSearchFilter
|
||||||
from InvenTree.mixins import ListAPI, RetrieveAPI, RetrieveUpdateDestroyAPI
|
from InvenTree.mixins import ListAPI, RetrieveAPI, RetrieveUpdateDestroyAPI
|
||||||
@ -21,10 +23,6 @@ from plugin.base.label import label as plugin_label
|
|||||||
from plugin.registry import registry
|
from plugin.registry import registry
|
||||||
from stock.models import StockItem, StockLocation
|
from stock.models import StockItem, StockLocation
|
||||||
|
|
||||||
from .models import PartLabel, StockItemLabel, StockLocationLabel
|
|
||||||
from .serializers import (PartLabelSerializer, StockItemLabelSerializer,
|
|
||||||
StockLocationLabelSerializer)
|
|
||||||
|
|
||||||
|
|
||||||
class LabelFilterMixin:
|
class LabelFilterMixin:
|
||||||
"""Mixin for filtering a queryset by a list of object ID values.
|
"""Mixin for filtering a queryset by a list of object ID values.
|
||||||
@ -92,11 +90,11 @@ class LabelListView(LabelFilterMixin, ListAPI):
|
|||||||
"""
|
"""
|
||||||
valid_label_ids = set()
|
valid_label_ids = set()
|
||||||
|
|
||||||
for label in queryset.all():
|
for lbl in queryset.all():
|
||||||
matches = True
|
matches = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
filters = InvenTree.helpers.validateFilterString(label.filters)
|
filters = InvenTree.helpers.validateFilterString(lbl.filters)
|
||||||
except ValidationError:
|
except ValidationError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -113,7 +111,7 @@ class LabelListView(LabelFilterMixin, ListAPI):
|
|||||||
|
|
||||||
# Matched all items
|
# Matched all items
|
||||||
if matches:
|
if matches:
|
||||||
valid_label_ids.add(label.pk)
|
valid_label_ids.add(lbl.pk)
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -285,8 +283,8 @@ class LabelPrintMixin(LabelFilterMixin):
|
|||||||
class StockItemLabelMixin:
|
class StockItemLabelMixin:
|
||||||
"""Mixin for StockItemLabel endpoints"""
|
"""Mixin for StockItemLabel endpoints"""
|
||||||
|
|
||||||
queryset = StockItemLabel.objects.all()
|
queryset = label.models.StockItemLabel.objects.all()
|
||||||
serializer_class = StockItemLabelSerializer
|
serializer_class = label.serializers.StockItemLabelSerializer
|
||||||
|
|
||||||
ITEM_MODEL = StockItem
|
ITEM_MODEL = StockItem
|
||||||
ITEM_KEY = 'item'
|
ITEM_KEY = 'item'
|
||||||
@ -317,8 +315,8 @@ class StockItemLabelPrint(StockItemLabelMixin, LabelPrintMixin, RetrieveAPI):
|
|||||||
class StockLocationLabelMixin:
|
class StockLocationLabelMixin:
|
||||||
"""Mixin for StockLocationLabel endpoints"""
|
"""Mixin for StockLocationLabel endpoints"""
|
||||||
|
|
||||||
queryset = StockLocationLabel.objects.all()
|
queryset = label.models.StockLocationLabel.objects.all()
|
||||||
serializer_class = StockLocationLabelSerializer
|
serializer_class = label.serializers.StockLocationLabelSerializer
|
||||||
|
|
||||||
ITEM_MODEL = StockLocation
|
ITEM_MODEL = StockLocation
|
||||||
ITEM_KEY = 'location'
|
ITEM_KEY = 'location'
|
||||||
@ -348,8 +346,8 @@ class StockLocationLabelPrint(StockLocationLabelMixin, LabelPrintMixin, Retrieve
|
|||||||
|
|
||||||
class PartLabelMixin:
|
class PartLabelMixin:
|
||||||
"""Mixin for PartLabel endpoints"""
|
"""Mixin for PartLabel endpoints"""
|
||||||
queryset = PartLabel.objects.all()
|
queryset = label.models.PartLabel.objects.all()
|
||||||
serializer_class = PartLabelSerializer
|
serializer_class = label.serializers.PartLabelSerializer
|
||||||
|
|
||||||
ITEM_MODEL = Part
|
ITEM_MODEL = Part
|
||||||
ITEM_KEY = 'part'
|
ITEM_KEY = 'part'
|
||||||
@ -377,7 +375,7 @@ label_api_urls = [
|
|||||||
# Detail views
|
# Detail views
|
||||||
path(r'<int:pk>/', include([
|
path(r'<int:pk>/', include([
|
||||||
re_path(r'print/?', StockItemLabelPrint.as_view(), name='api-stockitem-label-print'),
|
re_path(r'print/?', StockItemLabelPrint.as_view(), name='api-stockitem-label-print'),
|
||||||
re_path(r'metadata/', MetadataView.as_view(), {'model': StockItemLabel}, name='api-stockitem-label-metadata'),
|
re_path(r'metadata/', MetadataView.as_view(), {'model': label.models.StockItemLabel}, name='api-stockitem-label-metadata'),
|
||||||
re_path(r'^.*$', StockItemLabelDetail.as_view(), name='api-stockitem-label-detail'),
|
re_path(r'^.*$', StockItemLabelDetail.as_view(), name='api-stockitem-label-detail'),
|
||||||
])),
|
])),
|
||||||
|
|
||||||
@ -390,7 +388,7 @@ label_api_urls = [
|
|||||||
# Detail views
|
# Detail views
|
||||||
path(r'<int:pk>/', include([
|
path(r'<int:pk>/', include([
|
||||||
re_path(r'print/?', StockLocationLabelPrint.as_view(), name='api-stocklocation-label-print'),
|
re_path(r'print/?', StockLocationLabelPrint.as_view(), name='api-stocklocation-label-print'),
|
||||||
re_path(r'metadata/', MetadataView.as_view(), {'model': StockLocationLabel}, name='api-stocklocation-label-metadata'),
|
re_path(r'metadata/', MetadataView.as_view(), {'model': label.models.StockLocationLabel}, name='api-stocklocation-label-metadata'),
|
||||||
re_path(r'^.*$', StockLocationLabelDetail.as_view(), name='api-stocklocation-label-detail'),
|
re_path(r'^.*$', StockLocationLabelDetail.as_view(), name='api-stocklocation-label-detail'),
|
||||||
])),
|
])),
|
||||||
|
|
||||||
@ -403,7 +401,7 @@ label_api_urls = [
|
|||||||
# Detail views
|
# Detail views
|
||||||
path(r'<int:pk>/', include([
|
path(r'<int:pk>/', include([
|
||||||
re_path(r'^print/', PartLabelPrint.as_view(), name='api-part-label-print'),
|
re_path(r'^print/', PartLabelPrint.as_view(), name='api-part-label-print'),
|
||||||
re_path(r'^metadata/', MetadataView.as_view(), {'model': PartLabel}, name='api-part-label-metadata'),
|
re_path(r'^metadata/', MetadataView.as_view(), {'model': label.models.PartLabel}, name='api-part-label-metadata'),
|
||||||
re_path(r'^.*$', PartLabelDetail.as_view(), name='api-part-label-detail'),
|
re_path(r'^.*$', PartLabelDetail.as_view(), name='api-part-label-detail'),
|
||||||
])),
|
])),
|
||||||
|
|
||||||
|
@ -1,63 +1,54 @@
|
|||||||
"""API serializers for the label app"""
|
"""API serializers for the label app"""
|
||||||
|
|
||||||
|
import label.models
|
||||||
from InvenTree.serializers import (InvenTreeAttachmentSerializerField,
|
from InvenTree.serializers import (InvenTreeAttachmentSerializerField,
|
||||||
InvenTreeModelSerializer)
|
InvenTreeModelSerializer)
|
||||||
|
|
||||||
from .models import PartLabel, StockItemLabel, StockLocationLabel
|
|
||||||
|
class LabelSerializerBase(InvenTreeModelSerializer):
|
||||||
|
"""Base class for label serializer"""
|
||||||
|
|
||||||
|
label = InvenTreeAttachmentSerializerField(required=True)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def label_fields():
|
||||||
|
"""Generic serializer fields for a label template"""
|
||||||
|
|
||||||
|
return [
|
||||||
|
'pk',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'label',
|
||||||
|
'filters',
|
||||||
|
'enabled',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class StockItemLabelSerializer(InvenTreeModelSerializer):
|
class StockItemLabelSerializer(LabelSerializerBase):
|
||||||
"""Serializes a StockItemLabel object."""
|
"""Serializes a StockItemLabel object."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Metaclass options."""
|
"""Metaclass options."""
|
||||||
|
|
||||||
model = StockItemLabel
|
model = label.models.StockItemLabel
|
||||||
fields = [
|
fields = LabelSerializerBase.label_fields()
|
||||||
'pk',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'label',
|
|
||||||
'filters',
|
|
||||||
'enabled',
|
|
||||||
]
|
|
||||||
|
|
||||||
label = InvenTreeAttachmentSerializerField(required=True)
|
|
||||||
|
|
||||||
|
|
||||||
class StockLocationLabelSerializer(InvenTreeModelSerializer):
|
class StockLocationLabelSerializer(LabelSerializerBase):
|
||||||
"""Serializes a StockLocationLabel object."""
|
"""Serializes a StockLocationLabel object."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Metaclass options."""
|
"""Metaclass options."""
|
||||||
|
|
||||||
model = StockLocationLabel
|
model = label.models.StockLocationLabel
|
||||||
fields = [
|
fields = LabelSerializerBase.label_fields()
|
||||||
'pk',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'label',
|
|
||||||
'filters',
|
|
||||||
'enabled',
|
|
||||||
]
|
|
||||||
|
|
||||||
label = InvenTreeAttachmentSerializerField(required=True)
|
|
||||||
|
|
||||||
|
|
||||||
class PartLabelSerializer(InvenTreeModelSerializer):
|
class PartLabelSerializer(LabelSerializerBase):
|
||||||
"""Serializes a PartLabel object."""
|
"""Serializes a PartLabel object."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Metaclass options."""
|
"""Metaclass options."""
|
||||||
|
|
||||||
model = PartLabel
|
model = label.models.PartLabel
|
||||||
fields = [
|
fields = LabelSerializerBase.label_fields()
|
||||||
'pk',
|
|
||||||
'name',
|
|
||||||
'description',
|
|
||||||
'label',
|
|
||||||
'filters',
|
|
||||||
'enabled',
|
|
||||||
]
|
|
||||||
|
|
||||||
label = InvenTreeAttachmentSerializerField(required=True)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user