2024-06-22 23:28:21 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from rest_framework import serializers
|
2024-11-06 06:25:10 +00:00
|
|
|
from rest_framework.relations import SlugRelatedField
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
from files.models import File
|
|
|
|
from inventory.models import Event, Container, Item
|
2024-11-05 23:03:35 +00:00
|
|
|
from mail.models import EventAddress
|
|
|
|
|
|
|
|
|
2024-11-19 22:03:44 +00:00
|
|
|
# class EventAdressSerializer(serializers.ModelSerializer):
|
2024-11-06 06:25:10 +00:00
|
|
|
# class Meta:
|
|
|
|
# model = EventAddress
|
|
|
|
# fields = ['address']
|
|
|
|
|
|
|
|
# def to_internal_value(self, data):
|
|
|
|
# if not isinstance(data, str):
|
|
|
|
# raise serializers.ValidationError('This field must be a string.')
|
|
|
|
#
|
|
|
|
# def create(self, validated_data):
|
|
|
|
# return EventAddress.objects.create(**validated_data)
|
|
|
|
#
|
|
|
|
# def validate(self, data):
|
|
|
|
# return isinstance(data, str)
|
|
|
|
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
class EventSerializer(serializers.ModelSerializer):
|
2024-11-19 22:03:44 +00:00
|
|
|
# addresses = EventAdressSerializer(many=True, required=False)
|
2024-11-06 06:25:10 +00:00
|
|
|
addresses = SlugRelatedField(many=True, slug_field='address', queryset=EventAddress.objects.all())
|
2024-11-05 23:03:35 +00:00
|
|
|
|
2024-06-22 23:28:21 +00:00
|
|
|
class Meta:
|
|
|
|
model = Event
|
2024-11-05 23:03:35 +00:00
|
|
|
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses']
|
2024-06-22 23:28:21 +00:00
|
|
|
read_only_fields = ['eid']
|
|
|
|
|
2024-11-19 22:03:44 +00:00
|
|
|
|
2024-11-06 06:25:10 +00:00
|
|
|
# def update(self, instance, validated_data):
|
|
|
|
# addresses = validated_data.pop('addresses', None)
|
|
|
|
# instance.save(validated_data)
|
|
|
|
# if addresses:
|
|
|
|
# for address in addresses:
|
|
|
|
# nested_instance, created = EventAddress.objects.get_or_create(address=address)
|
|
|
|
# instance.addresses.add(nested_instance)
|
|
|
|
#
|
|
|
|
# return instance
|
|
|
|
|
|
|
|
|
2024-06-22 23:28:21 +00:00
|
|
|
class ContainerSerializer(serializers.ModelSerializer):
|
|
|
|
itemCount = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Container
|
2024-11-19 22:03:44 +00:00
|
|
|
fields = ['id', 'name', 'itemCount']
|
|
|
|
read_only_fields = ['id', 'itemCount']
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
def get_itemCount(self, instance):
|
2024-11-19 22:03:44 +00:00
|
|
|
return len(instance.items)
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2024-11-13 22:51:54 +00:00
|
|
|
event = serializers.SlugRelatedField(slug_field='slug', queryset=Event.objects.all(),
|
|
|
|
allow_null=True, required=False)
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Item
|
2024-11-19 22:03:44 +00:00
|
|
|
fields = ['cid', 'box', 'id', 'description', 'file', 'dataImage', 'returned', 'event']
|
|
|
|
read_only_fields = ['id']
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
def get_cid(self, instance):
|
2024-11-19 22:03:44 +00:00
|
|
|
return instance.container.id if instance.container else None
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
def get_box(self, instance):
|
2024-11-19 22:03:44 +00:00
|
|
|
return instance.container.name if instance.container else None
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
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:
|
2024-11-19 22:03:44 +00:00
|
|
|
container = Container.objects.get(id=data['cid'])
|
2024-06-22 23:28:21 +00:00
|
|
|
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)
|
2024-07-17 18:22:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|