96 lines
3.3 KiB
Python
96 lines
3.3 KiB
Python
from django.utils import timezone
|
|
from rest_framework import serializers
|
|
from rest_framework.relations import SlugRelatedField
|
|
|
|
from files.models import File
|
|
from inventory.models import Event, Container, Item
|
|
from inventory.shared_serializers import BasicItemSerializer
|
|
from mail.models import EventAddress
|
|
from tickets.shared_serializers import BasicIssueSerializer
|
|
|
|
|
|
class EventAdressSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = EventAddress
|
|
fields = ['address']
|
|
|
|
|
|
class EventSerializer(serializers.ModelSerializer):
|
|
addresses = EventAdressSerializer(many=True, required=False)
|
|
|
|
class Meta:
|
|
model = Event
|
|
fields = ['id', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses']
|
|
read_only_fields = ['id']
|
|
|
|
|
|
class ContainerSerializer(serializers.ModelSerializer):
|
|
itemCount = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Container
|
|
fields = ['id', 'name', 'itemCount']
|
|
read_only_fields = ['id', 'itemCount']
|
|
|
|
def get_itemCount(self, instance):
|
|
return Item.objects.filter(container=instance.id).count()
|
|
|
|
|
|
class ItemSerializer(BasicItemSerializer):
|
|
dataImage = serializers.CharField(write_only=True, required=False)
|
|
related_issues = BasicIssueSerializer(many=True, read_only=True)
|
|
|
|
class Meta:
|
|
model = Item
|
|
fields = ['cid', 'box', 'id', 'description', 'file', 'dataImage', 'returned', 'event', 'related_issues']
|
|
read_only_fields = ['id']
|
|
|
|
def to_internal_value(self, data):
|
|
container = None
|
|
returned = False
|
|
if 'cid' in data:
|
|
container = Container.objects.get(id=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):
|
|
if not 'container' in attrs and not self.partial:
|
|
raise serializers.ValidationError("This field cannot be empty.")
|
|
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)
|
|
|
|
|
|
class SearchResultSerializer(serializers.Serializer):
|
|
search_score = serializers.IntegerField()
|
|
item = ItemSerializer()
|
|
|
|
def to_representation(self, instance):
|
|
return {**ItemSerializer(instance['item']).data, 'search_score': instance['search_score']}
|
|
|
|
class Meta:
|
|
model = Item
|