refactor inventory serializers
This commit is contained in:
parent
4152034e4a
commit
7d1786f143
3 changed files with 92 additions and 107 deletions
|
@ -6,13 +6,7 @@ from rest_framework.response import Response
|
||||||
|
|
||||||
from files.models import File
|
from files.models import File
|
||||||
from inventory.models import Event, Container, Item
|
from inventory.models import Event, Container, Item
|
||||||
|
from inventory.serializers import EventSerializer, ContainerSerializer
|
||||||
|
|
||||||
class EventSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = Event
|
|
||||||
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end']
|
|
||||||
read_only_fields = ['eid']
|
|
||||||
|
|
||||||
|
|
||||||
class EventViewSet(viewsets.ModelViewSet):
|
class EventViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -22,18 +16,6 @@ class EventViewSet(viewsets.ModelViewSet):
|
||||||
authentication_classes = []
|
authentication_classes = []
|
||||||
|
|
||||||
|
|
||||||
class ContainerSerializer(serializers.ModelSerializer):
|
|
||||||
itemCount = serializers.SerializerMethodField()
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = Container
|
|
||||||
fields = ['cid', 'name', 'itemCount']
|
|
||||||
read_only_fields = ['cid', 'itemCount']
|
|
||||||
|
|
||||||
def get_itemCount(self, instance):
|
|
||||||
return Item.objects.filter(container=instance.cid).count()
|
|
||||||
|
|
||||||
|
|
||||||
class ContainerViewSet(viewsets.ModelViewSet):
|
class ContainerViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = ContainerSerializer
|
serializer_class = ContainerSerializer
|
||||||
queryset = Container.objects.all()
|
queryset = Container.objects.all()
|
||||||
|
|
|
@ -1,21 +1,12 @@
|
||||||
from datetime import datetime
|
from django.urls import path
|
||||||
|
|
||||||
from django.urls import path, re_path
|
|
||||||
from django.contrib.auth.decorators import permission_required
|
from django.contrib.auth.decorators import permission_required
|
||||||
from rest_framework import routers, viewsets, serializers
|
from rest_framework import routers, viewsets
|
||||||
from rest_framework.decorators import api_view, permission_classes
|
from rest_framework.decorators import api_view, permission_classes
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
from files.models import File
|
|
||||||
from inventory.models import Event, Container, Item
|
from inventory.models import Event, Container, Item
|
||||||
|
from inventory.serializers import EventSerializer, ContainerSerializer, ItemSerializer
|
||||||
|
|
||||||
class EventSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = Event
|
|
||||||
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end']
|
|
||||||
read_only_fields = ['eid']
|
|
||||||
|
|
||||||
|
|
||||||
class EventViewSet(viewsets.ModelViewSet):
|
class EventViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -24,87 +15,11 @@ class EventViewSet(viewsets.ModelViewSet):
|
||||||
permission_classes = []
|
permission_classes = []
|
||||||
|
|
||||||
|
|
||||||
class ContainerSerializer(serializers.ModelSerializer):
|
|
||||||
itemCount = serializers.SerializerMethodField()
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = Container
|
|
||||||
fields = ['cid', 'name', 'itemCount']
|
|
||||||
read_only_fields = ['cid', 'itemCount']
|
|
||||||
|
|
||||||
def get_itemCount(self, instance):
|
|
||||||
return Item.objects.filter(container=instance.cid).count()
|
|
||||||
|
|
||||||
|
|
||||||
class ContainerViewSet(viewsets.ModelViewSet):
|
class ContainerViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = ContainerSerializer
|
serializer_class = ContainerSerializer
|
||||||
queryset = Container.objects.all()
|
queryset = Container.objects.all()
|
||||||
|
|
||||||
|
|
||||||
class ItemSerializer(serializers.ModelSerializer):
|
|
||||||
dataImage = serializers.CharField(write_only=True, required=False)
|
|
||||||
cid = serializers.SerializerMethodField()
|
|
||||||
box = serializers.SerializerMethodField()
|
|
||||||
file = serializers.SerializerMethodField()
|
|
||||||
returned = serializers.SerializerMethodField(required=False)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = Item
|
|
||||||
fields = ['cid', 'box', 'uid', 'description', 'file', 'dataImage', 'returned']
|
|
||||||
read_only_fields = ['uid']
|
|
||||||
|
|
||||||
def get_cid(self, instance):
|
|
||||||
return instance.container.cid
|
|
||||||
|
|
||||||
def get_box(self, instance):
|
|
||||||
return instance.container.name
|
|
||||||
|
|
||||||
def get_file(self, instance):
|
|
||||||
if len(instance.files.all()) > 0:
|
|
||||||
return instance.files.all().order_by('-created_at')[0].hash
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_returned(self, instance):
|
|
||||||
return instance.returned_at is not None
|
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
|
||||||
container = None
|
|
||||||
returned = False
|
|
||||||
if 'cid' in data:
|
|
||||||
container = Container.objects.get(cid=data['cid'])
|
|
||||||
if 'returned' in data:
|
|
||||||
returned = data['returned']
|
|
||||||
internal = super().to_internal_value(data)
|
|
||||||
if container:
|
|
||||||
internal['container'] = container
|
|
||||||
if returned:
|
|
||||||
internal['returned_at'] = datetime.now()
|
|
||||||
return internal
|
|
||||||
|
|
||||||
def validate(self, attrs):
|
|
||||||
return super().validate(attrs)
|
|
||||||
|
|
||||||
def create(self, validated_data):
|
|
||||||
if 'dataImage' in validated_data:
|
|
||||||
file = File.objects.create(data=validated_data['dataImage'])
|
|
||||||
validated_data.pop('dataImage')
|
|
||||||
item = Item.objects.create(**validated_data)
|
|
||||||
item.files.set([file])
|
|
||||||
return item
|
|
||||||
return Item.objects.create(**validated_data)
|
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
|
||||||
if 'returned' in validated_data:
|
|
||||||
if validated_data['returned']:
|
|
||||||
validated_data['returned_at'] = datetime.now()
|
|
||||||
validated_data.pop('returned')
|
|
||||||
if 'dataImage' in validated_data:
|
|
||||||
file = File.objects.create(data=validated_data['dataImage'])
|
|
||||||
validated_data.pop('dataImage')
|
|
||||||
instance.files.add(file)
|
|
||||||
return super().update(instance, validated_data)
|
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
@permission_required('view_item', raise_exception=True)
|
@permission_required('view_item', raise_exception=True)
|
||||||
|
|
88
core/inventory/serializers.py
Normal file
88
core/inventory/serializers.py
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
from django.utils import timezone
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from files.models import File
|
||||||
|
from inventory.models import Event, Container, Item
|
||||||
|
|
||||||
|
|
||||||
|
class EventSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Event
|
||||||
|
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end']
|
||||||
|
read_only_fields = ['eid']
|
||||||
|
|
||||||
|
|
||||||
|
class ContainerSerializer(serializers.ModelSerializer):
|
||||||
|
itemCount = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Container
|
||||||
|
fields = ['cid', 'name', 'itemCount']
|
||||||
|
read_only_fields = ['cid', 'itemCount']
|
||||||
|
|
||||||
|
def get_itemCount(self, instance):
|
||||||
|
return Item.objects.filter(container=instance.cid).count()
|
||||||
|
|
||||||
|
|
||||||
|
class ItemSerializer(serializers.ModelSerializer):
|
||||||
|
dataImage = serializers.CharField(write_only=True, required=False)
|
||||||
|
cid = serializers.SerializerMethodField()
|
||||||
|
box = serializers.SerializerMethodField()
|
||||||
|
file = serializers.SerializerMethodField()
|
||||||
|
returned = serializers.SerializerMethodField(required=False)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Item
|
||||||
|
fields = ['cid', 'box', 'uid', 'description', 'file', 'dataImage', 'returned']
|
||||||
|
read_only_fields = ['uid']
|
||||||
|
|
||||||
|
def get_cid(self, instance):
|
||||||
|
return instance.container.cid
|
||||||
|
|
||||||
|
def get_box(self, instance):
|
||||||
|
return instance.container.name
|
||||||
|
|
||||||
|
def get_file(self, instance):
|
||||||
|
if len(instance.files.all()) > 0:
|
||||||
|
return instance.files.all().order_by('-created_at')[0].hash
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_returned(self, instance):
|
||||||
|
return instance.returned_at is not None
|
||||||
|
|
||||||
|
def to_internal_value(self, data):
|
||||||
|
container = None
|
||||||
|
returned = False
|
||||||
|
if 'cid' in data:
|
||||||
|
container = Container.objects.get(cid=data['cid'])
|
||||||
|
if 'returned' in data:
|
||||||
|
returned = data['returned']
|
||||||
|
internal = super().to_internal_value(data)
|
||||||
|
if container:
|
||||||
|
internal['container'] = container
|
||||||
|
if returned:
|
||||||
|
internal['returned_at'] = timezone.now()
|
||||||
|
return internal
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
return super().validate(attrs)
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
if 'dataImage' in validated_data:
|
||||||
|
file = File.objects.create(data=validated_data['dataImage'])
|
||||||
|
validated_data.pop('dataImage')
|
||||||
|
item = Item.objects.create(**validated_data)
|
||||||
|
item.files.set([file])
|
||||||
|
return item
|
||||||
|
return Item.objects.create(**validated_data)
|
||||||
|
|
||||||
|
def update(self, instance, validated_data):
|
||||||
|
if 'returned' in validated_data:
|
||||||
|
if validated_data['returned']:
|
||||||
|
validated_data['returned_at'] = timezone.now()
|
||||||
|
validated_data.pop('returned')
|
||||||
|
if 'dataImage' in validated_data:
|
||||||
|
file = File.objects.create(data=validated_data['dataImage'])
|
||||||
|
validated_data.pop('dataImage')
|
||||||
|
instance.files.add(file)
|
||||||
|
return super().update(instance, validated_data)
|
Loading…
Reference in a new issue