2024-06-22 23:28:21 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from rest_framework import serializers
|
2024-11-20 23:58:14 +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-20 23:58:14 +00:00
|
|
|
from inventory.shared_serializers import BasicItemSerializer
|
2024-11-05 23:03:35 +00:00
|
|
|
from mail.models import EventAddress
|
2024-11-20 23:58:14 +00:00
|
|
|
from tickets.shared_serializers import BasicIssueSerializer
|
2024-11-05 23:03:35 +00:00
|
|
|
|
|
|
|
|
2024-06-22 23:28:21 +00:00
|
|
|
class EventSerializer(serializers.ModelSerializer):
|
2024-11-06 20:11:18 +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-19 23:23:20 +00:00
|
|
|
fields = ['id', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses']
|
|
|
|
read_only_fields = ['id']
|
2024-06-22 23:28:21 +00:00
|
|
|
|
2024-11-06 20:11:18 +00:00
|
|
|
def to_internal_value(self, data):
|
|
|
|
data = data.copy()
|
|
|
|
addresses = data.pop('addresses', None)
|
|
|
|
dict = super().to_internal_value(data)
|
|
|
|
if addresses:
|
|
|
|
dict['addresses'] = [EventAddress.objects.get_or_create(address=x)[0] for x in addresses]
|
|
|
|
return dict
|
|
|
|
|
2024-06-22 23:28:21 +00:00
|
|
|
class ContainerSerializer(serializers.ModelSerializer):
|
|
|
|
itemCount = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Container
|
2024-11-19 23:23:20 +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-23 00:22:24 +00:00
|
|
|
return len(instance.items)
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
|
2024-11-20 23:58:14 +00:00
|
|
|
class ItemSerializer(BasicItemSerializer):
|
2024-11-23 00:22:24 +00:00
|
|
|
timeline = serializers.SerializerMethodField()
|
2024-06-22 23:28:21 +00:00
|
|
|
dataImage = serializers.CharField(write_only=True, required=False)
|
2024-11-20 23:58:14 +00:00
|
|
|
related_issues = BasicIssueSerializer(many=True, read_only=True)
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Item
|
2024-11-23 00:22:24 +00:00
|
|
|
fields = ['cid', 'box', 'id', 'description', 'file', 'dataImage', 'returned', 'event', 'related_issues',
|
|
|
|
'timeline']
|
2024-11-19 23:23:20 +00:00
|
|
|
read_only_fields = ['id']
|
2024-06-22 23:28:21 +00:00
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
container = None
|
|
|
|
returned = False
|
|
|
|
if 'cid' in data:
|
2024-11-19 23:23:20 +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):
|
2024-11-19 23:23:20 +00:00
|
|
|
if not 'container' in attrs and not self.partial:
|
|
|
|
raise serializers.ValidationError("This field cannot be empty.")
|
2024-06-22 23:28:21 +00:00
|
|
|
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-11-09 04:05:05 +00:00
|
|
|
|
2024-11-23 00:22:24 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_timeline(obj):
|
|
|
|
timeline = []
|
|
|
|
for comment in obj.comments.all():
|
|
|
|
timeline.append({
|
|
|
|
'type': 'comment',
|
|
|
|
'id': comment.id,
|
|
|
|
'timestamp': comment.timestamp,
|
|
|
|
'comment': comment.comment,
|
|
|
|
})
|
|
|
|
for relation in (obj.issue_relation_changes.all()):
|
|
|
|
timeline.append({
|
|
|
|
'type': 'issue_relation',
|
|
|
|
'id': relation.id,
|
|
|
|
'status': relation.status,
|
|
|
|
'timestamp': relation.timestamp,
|
|
|
|
'issue_thread': BasicIssueSerializer(relation.issue_thread).data,
|
|
|
|
})
|
|
|
|
return sorted(timeline, key=lambda x: x['timestamp'])
|
|
|
|
|
2024-11-09 04:05:05 +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
|