2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-01 11:10:54 +00:00

Move Meta class to top of class definition (#4363)

This commit is contained in:
Oliver
2023-02-18 18:51:00 +11:00
committed by GitHub
parent 139274f356
commit cc2e7ee8a5
27 changed files with 1085 additions and 1084 deletions

View File

@ -20,16 +20,6 @@ from .models import (StockItem, StockItemAttachment, StockItemTestResult,
class LocationResource(InvenTreeResource):
"""Class for managing StockLocation data import/export."""
id = Field(attribute='pk', column_name=_('Location ID'))
name = Field(attribute='name', column_name=_('Location Name'))
description = Field(attribute='description', column_name=_('Description'))
parent = Field(attribute='parent', column_name=_('Parent ID'), widget=widgets.ForeignKeyWidget(StockLocation))
parent_name = Field(attribute='parent__name', column_name=_('Parent Name'), readonly=True)
pathstring = Field(attribute='pathstring', column_name=_('Location Path'))
# Calculated fields
items = Field(attribute='item_count', column_name=_('Stock Items'), widget=widgets.IntegerWidget())
class Meta:
"""Metaclass options."""
@ -46,6 +36,16 @@ class LocationResource(InvenTreeResource):
'owner', 'icon',
]
id = Field(attribute='pk', column_name=_('Location ID'))
name = Field(attribute='name', column_name=_('Location Name'))
description = Field(attribute='description', column_name=_('Description'))
parent = Field(attribute='parent', column_name=_('Parent ID'), widget=widgets.ForeignKeyWidget(StockLocation))
parent_name = Field(attribute='parent__name', column_name=_('Parent Name'), readonly=True)
pathstring = Field(attribute='pathstring', column_name=_('Location Path'))
# Calculated fields
items = Field(attribute='item_count', column_name=_('Stock Items'), widget=widgets.IntegerWidget())
def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
"""Rebuild after import to keep tree intact."""
super().after_import(dataset, result, using_transactions, dry_run, **kwargs)
@ -80,6 +80,23 @@ class LocationAdmin(ImportExportModelAdmin):
class StockItemResource(InvenTreeResource):
"""Class for managing StockItem data import/export."""
class Meta:
"""Metaclass options."""
model = StockItem
skip_unchanged = True
report_skipped = False
clean_model_instance = True
exclude = [
# Exclude MPTT internal model fields
'lft', 'rght', 'tree_id', 'level',
# Exclude internal fields
'serial_int', 'metadata',
'barcode_hash', 'barcode_data',
'owner',
]
id = Field(attribute='pk', column_name=_('Stock Item ID'))
part = Field(attribute='part', column_name=_('Part ID'), widget=widgets.ForeignKeyWidget(Part))
part_name = Field(attribute='part__full_name', column_name=_('Part Name'), readonly=True)
@ -114,23 +131,6 @@ class StockItemResource(InvenTreeResource):
# Rebuild the StockItem tree(s)
StockItem.objects.rebuild()
class Meta:
"""Metaclass options."""
model = StockItem
skip_unchanged = True
report_skipped = False
clean_model_instance = True
exclude = [
# Exclude MPTT internal model fields
'lft', 'rght', 'tree_id', 'level',
# Exclude internal fields
'serial_int', 'metadata',
'barcode_hash', 'barcode_data',
'owner',
]
class StockItemAdmin(ImportExportModelAdmin):
"""Admin class for StockItem."""

View File

@ -46,10 +46,6 @@ class LocationBriefSerializer(InvenTree.serializers.InvenTreeModelSerializer):
class StockItemSerializerBrief(InvenTree.serializers.InvenTreeModelSerializer):
"""Brief serializers for a StockItem."""
part_name = serializers.CharField(source='part.full_name', read_only=True)
quantity = InvenTreeDecimalField()
class Meta:
"""Metaclass options."""
@ -69,6 +65,10 @@ class StockItemSerializerBrief(InvenTree.serializers.InvenTreeModelSerializer):
'barcode_hash',
]
part_name = serializers.CharField(source='part.full_name', read_only=True)
quantity = InvenTreeDecimalField()
def validate_serial(self, value):
"""Make sure serial is not to big."""
if abs(extract_int(value)) > 0x7fffffff:
@ -83,6 +83,60 @@ class StockItemSerializer(InvenTree.serializers.InvenTreeModelSerializer):
- Includes serialization for the item location
"""
class Meta:
"""Metaclass options."""
model = StockItem
fields = [
'allocated',
'batch',
'belongs_to',
'build',
'customer',
'delete_on_deplete',
'expired',
'expiry_date',
'is_building',
'link',
'location',
'location_detail',
'notes',
'owner',
'packaging',
'part',
'part_detail',
'purchase_order',
'purchase_order_reference',
'pk',
'quantity',
'sales_order',
'sales_order_reference',
'serial',
'stale',
'status',
'status_text',
'stocktake_date',
'supplier_part',
'supplier_part_detail',
'tracking_items',
'barcode_hash',
'updated',
'purchase_price',
'purchase_price_currency',
]
"""
These fields are read-only in this context.
They can be updated by accessing the appropriate API endpoints
"""
read_only_fields = [
'allocated',
'barcode_hash',
'stocktake_date',
'stocktake_user',
'updated',
]
part = serializers.PrimaryKeyRelatedField(
queryset=part_models.Part.objects.all(),
many=False, allow_null=False,
@ -197,60 +251,6 @@ class StockItemSerializer(InvenTree.serializers.InvenTreeModelSerializer):
if supplier_part_detail is not True:
self.fields.pop('supplier_part_detail')
class Meta:
"""Metaclass options."""
model = StockItem
fields = [
'allocated',
'batch',
'belongs_to',
'build',
'customer',
'delete_on_deplete',
'expired',
'expiry_date',
'is_building',
'link',
'location',
'location_detail',
'notes',
'owner',
'packaging',
'part',
'part_detail',
'purchase_order',
'purchase_order_reference',
'pk',
'quantity',
'sales_order',
'sales_order_reference',
'serial',
'stale',
'status',
'status_text',
'stocktake_date',
'supplier_part',
'supplier_part_detail',
'tracking_items',
'barcode_hash',
'updated',
'purchase_price',
'purchase_price_currency',
]
"""
These fields are read-only in this context.
They can be updated by accessing the appropriate API endpoints
"""
read_only_fields = [
'allocated',
'barcode_hash',
'stocktake_date',
'stocktake_user',
'updated',
]
class SerializeStockItemSerializer(serializers.Serializer):
"""A DRF serializer for "serializing" a StockItem.
@ -567,23 +567,6 @@ class LocationTreeSerializer(InvenTree.serializers.InvenTreeModelSerializer):
class LocationSerializer(InvenTree.serializers.InvenTreeModelSerializer):
"""Detailed information about a stock location."""
@staticmethod
def annotate_queryset(queryset):
"""Annotate extra information to the queryset"""
# Annotate the number of stock items which exist in this category (including subcategories)
queryset = queryset.annotate(
items=stock.filters.annotate_location_items()
)
return queryset
url = serializers.CharField(source='get_absolute_url', read_only=True)
items = serializers.IntegerField(read_only=True)
level = serializers.IntegerField(read_only=True)
class Meta:
"""Metaclass options."""
@ -607,6 +590,23 @@ class LocationSerializer(InvenTree.serializers.InvenTreeModelSerializer):
'barcode_hash',
]
@staticmethod
def annotate_queryset(queryset):
"""Annotate extra information to the queryset"""
# Annotate the number of stock items which exist in this category (including subcategories)
queryset = queryset.annotate(
items=stock.filters.annotate_location_items()
)
return queryset
url = serializers.CharField(source='get_absolute_url', read_only=True)
items = serializers.IntegerField(read_only=True)
level = serializers.IntegerField(read_only=True)
class StockItemAttachmentSerializer(InvenTree.serializers.InvenTreeAttachmentSerializer):
"""Serializer for StockItemAttachment model."""
@ -624,21 +624,6 @@ class StockItemAttachmentSerializer(InvenTree.serializers.InvenTreeAttachmentSer
class StockItemTestResultSerializer(InvenTree.serializers.InvenTreeModelSerializer):
"""Serializer for the StockItemTestResult model."""
user_detail = InvenTree.serializers.UserSerializer(source='user', read_only=True)
key = serializers.CharField(read_only=True)
attachment = InvenTree.serializers.InvenTreeAttachmentSerializerField(required=False)
def __init__(self, *args, **kwargs):
"""Add detail fields."""
user_detail = kwargs.pop('user_detail', False)
super().__init__(*args, **kwargs)
if user_detail is not True:
self.fields.pop('user_detail')
class Meta:
"""Metaclass options."""
@ -664,30 +649,24 @@ class StockItemTestResultSerializer(InvenTree.serializers.InvenTreeModelSerializ
'date',
]
class StockTrackingSerializer(InvenTree.serializers.InvenTreeModelSerializer):
"""Serializer for StockItemTracking model."""
def __init__(self, *args, **kwargs):
"""Add detail fields."""
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')
label = serializers.CharField(read_only=True)
user_detail = InvenTree.serializers.UserSerializer(source='user', read_only=True)
item_detail = StockItemSerializerBrief(source='item', many=False, read_only=True)
key = serializers.CharField(read_only=True)
user_detail = InvenTree.serializers.UserSerializer(source='user', many=False, read_only=True)
attachment = InvenTree.serializers.InvenTreeAttachmentSerializerField(required=False)
deltas = serializers.JSONField(read_only=True)
class StockTrackingSerializer(InvenTree.serializers.InvenTreeModelSerializer):
"""Serializer for StockItemTracking model."""
class Meta:
"""Metaclass options."""
@ -713,6 +692,27 @@ class StockTrackingSerializer(InvenTree.serializers.InvenTreeModelSerializer):
'tracking_type',
]
def __init__(self, *args, **kwargs):
"""Add detail fields."""
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')
label = serializers.CharField(read_only=True)
item_detail = StockItemSerializerBrief(source='item', many=False, read_only=True)
user_detail = InvenTree.serializers.UserSerializer(source='user', many=False, read_only=True)
deltas = serializers.JSONField(read_only=True)
class StockAssignmentItemSerializer(serializers.Serializer):
"""Serializer for a single StockItem with in StockAssignment request.
@ -1124,15 +1124,6 @@ class StockRemoveSerializer(StockAdjustmentSerializer):
class StockTransferSerializer(StockAdjustmentSerializer):
"""Serializer for transferring (moving) stock item(s)."""
location = serializers.PrimaryKeyRelatedField(
queryset=StockLocation.objects.all(),
many=False,
required=True,
allow_null=False,
label=_('Location'),
help_text=_('Destination stock location'),
)
class Meta:
"""Metaclass options."""
@ -1142,6 +1133,15 @@ class StockTransferSerializer(StockAdjustmentSerializer):
'location',
]
location = serializers.PrimaryKeyRelatedField(
queryset=StockLocation.objects.all(),
many=False,
required=True,
allow_null=False,
label=_('Location'),
help_text=_('Destination stock location'),
)
def save(self):
"""Transfer stock."""
request = self.context['request']