mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +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 .models import PartLabel, StockItemLabel, StockLocationLabel
|
||||
import label.models
|
||||
|
||||
|
||||
class LabelAdmin(admin.ModelAdmin):
|
||||
@ -10,6 +10,6 @@ class LabelAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', 'description', 'label', 'filters', 'enabled')
|
||||
|
||||
|
||||
admin.site.register(StockItemLabel, LabelAdmin)
|
||||
admin.site.register(StockLocationLabel, LabelAdmin)
|
||||
admin.site.register(PartLabel, LabelAdmin)
|
||||
admin.site.register(label.models.StockItemLabel, LabelAdmin)
|
||||
admin.site.register(label.models.StockLocationLabel, LabelAdmin)
|
||||
admin.site.register(label.models.PartLabel, LabelAdmin)
|
||||
|
@ -12,6 +12,8 @@ from rest_framework.exceptions import NotFound
|
||||
|
||||
import common.models
|
||||
import InvenTree.helpers
|
||||
import label.models
|
||||
import label.serializers
|
||||
from InvenTree.api import MetadataView
|
||||
from InvenTree.filters import InvenTreeSearchFilter
|
||||
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 stock.models import StockItem, StockLocation
|
||||
|
||||
from .models import PartLabel, StockItemLabel, StockLocationLabel
|
||||
from .serializers import (PartLabelSerializer, StockItemLabelSerializer,
|
||||
StockLocationLabelSerializer)
|
||||
|
||||
|
||||
class LabelFilterMixin:
|
||||
"""Mixin for filtering a queryset by a list of object ID values.
|
||||
@ -92,11 +90,11 @@ class LabelListView(LabelFilterMixin, ListAPI):
|
||||
"""
|
||||
valid_label_ids = set()
|
||||
|
||||
for label in queryset.all():
|
||||
for lbl in queryset.all():
|
||||
matches = True
|
||||
|
||||
try:
|
||||
filters = InvenTree.helpers.validateFilterString(label.filters)
|
||||
filters = InvenTree.helpers.validateFilterString(lbl.filters)
|
||||
except ValidationError:
|
||||
continue
|
||||
|
||||
@ -113,7 +111,7 @@ class LabelListView(LabelFilterMixin, ListAPI):
|
||||
|
||||
# Matched all items
|
||||
if matches:
|
||||
valid_label_ids.add(label.pk)
|
||||
valid_label_ids.add(lbl.pk)
|
||||
else:
|
||||
continue
|
||||
|
||||
@ -285,8 +283,8 @@ class LabelPrintMixin(LabelFilterMixin):
|
||||
class StockItemLabelMixin:
|
||||
"""Mixin for StockItemLabel endpoints"""
|
||||
|
||||
queryset = StockItemLabel.objects.all()
|
||||
serializer_class = StockItemLabelSerializer
|
||||
queryset = label.models.StockItemLabel.objects.all()
|
||||
serializer_class = label.serializers.StockItemLabelSerializer
|
||||
|
||||
ITEM_MODEL = StockItem
|
||||
ITEM_KEY = 'item'
|
||||
@ -317,8 +315,8 @@ class StockItemLabelPrint(StockItemLabelMixin, LabelPrintMixin, RetrieveAPI):
|
||||
class StockLocationLabelMixin:
|
||||
"""Mixin for StockLocationLabel endpoints"""
|
||||
|
||||
queryset = StockLocationLabel.objects.all()
|
||||
serializer_class = StockLocationLabelSerializer
|
||||
queryset = label.models.StockLocationLabel.objects.all()
|
||||
serializer_class = label.serializers.StockLocationLabelSerializer
|
||||
|
||||
ITEM_MODEL = StockLocation
|
||||
ITEM_KEY = 'location'
|
||||
@ -348,8 +346,8 @@ class StockLocationLabelPrint(StockLocationLabelMixin, LabelPrintMixin, Retrieve
|
||||
|
||||
class PartLabelMixin:
|
||||
"""Mixin for PartLabel endpoints"""
|
||||
queryset = PartLabel.objects.all()
|
||||
serializer_class = PartLabelSerializer
|
||||
queryset = label.models.PartLabel.objects.all()
|
||||
serializer_class = label.serializers.PartLabelSerializer
|
||||
|
||||
ITEM_MODEL = Part
|
||||
ITEM_KEY = 'part'
|
||||
@ -377,7 +375,7 @@ label_api_urls = [
|
||||
# Detail views
|
||||
path(r'<int:pk>/', include([
|
||||
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'),
|
||||
])),
|
||||
|
||||
@ -390,7 +388,7 @@ label_api_urls = [
|
||||
# Detail views
|
||||
path(r'<int:pk>/', include([
|
||||
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'),
|
||||
])),
|
||||
|
||||
@ -403,7 +401,7 @@ label_api_urls = [
|
||||
# Detail views
|
||||
path(r'<int:pk>/', include([
|
||||
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'),
|
||||
])),
|
||||
|
||||
|
@ -1,63 +1,54 @@
|
||||
"""API serializers for the label app"""
|
||||
|
||||
import label.models
|
||||
from InvenTree.serializers import (InvenTreeAttachmentSerializerField,
|
||||
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."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
model = StockItemLabel
|
||||
fields = [
|
||||
'pk',
|
||||
'name',
|
||||
'description',
|
||||
'label',
|
||||
'filters',
|
||||
'enabled',
|
||||
]
|
||||
|
||||
label = InvenTreeAttachmentSerializerField(required=True)
|
||||
model = label.models.StockItemLabel
|
||||
fields = LabelSerializerBase.label_fields()
|
||||
|
||||
|
||||
class StockLocationLabelSerializer(InvenTreeModelSerializer):
|
||||
class StockLocationLabelSerializer(LabelSerializerBase):
|
||||
"""Serializes a StockLocationLabel object."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
model = StockLocationLabel
|
||||
fields = [
|
||||
'pk',
|
||||
'name',
|
||||
'description',
|
||||
'label',
|
||||
'filters',
|
||||
'enabled',
|
||||
]
|
||||
|
||||
label = InvenTreeAttachmentSerializerField(required=True)
|
||||
model = label.models.StockLocationLabel
|
||||
fields = LabelSerializerBase.label_fields()
|
||||
|
||||
|
||||
class PartLabelSerializer(InvenTreeModelSerializer):
|
||||
class PartLabelSerializer(LabelSerializerBase):
|
||||
"""Serializes a PartLabel object."""
|
||||
|
||||
class Meta:
|
||||
"""Metaclass options."""
|
||||
|
||||
model = PartLabel
|
||||
fields = [
|
||||
'pk',
|
||||
'name',
|
||||
'description',
|
||||
'label',
|
||||
'filters',
|
||||
'enabled',
|
||||
]
|
||||
|
||||
label = InvenTreeAttachmentSerializerField(required=True)
|
||||
model = label.models.PartLabel
|
||||
fields = LabelSerializerBase.label_fields()
|
||||
|
Loading…
Reference in New Issue
Block a user