class PartyDetailSerializer(serializers.ModelSerializer): '''Сериализация списка групп''' staff = LowReadContactSerializer(many=True, read_only=True) members = PageSerializer(many=True, read_only=True) blacklist = LowReadContactSerializer(many=True, read_only=True) admin = LowReadContactSerializer(read_only=True) num_members = serializers.IntegerField(read_only=True) joined = serializers.BooleanField(read_only=True) class Meta: model = Party fields = '__all__'
class NotificationSerializer(serializers.ModelSerializer): '''Сериализация уведомлений''' sender = LowReadContactSerializer(read_only=True) class Meta: model = Notice fields = ['sender', 'event', 'seen']
class MessageSerializer(serializers.ModelSerializer): '''Message serializer''' contact = LowReadContactSerializer(read_only=True) class Meta: model = Message fields = '__all__'
class PageSerializer(serializers.ModelSerializer): '''Сериалазиация странички''' user = LowReadContactSerializer() class Meta: model = Page fields = ['user']
class PostListSerializer(BasePostSerialzier): '''Сериализация списка постов''' user = LowReadContactSerializer(read_only=True) parent = RecursivePostSerialzier(read_only=True) is_watched = serializers.BooleanField(read_only=True) num_reviews = serializers.IntegerField(read_only=True) group_owner = PartyShortSerializer(read_only=True) num_comments = serializers.IntegerField(read_only=True) owner = PageSerializer(read_only=True)
class CommentSerializer(AbstractPostSerializer, serializers.ModelSerializer): '''Сериализация коммента к посту''' user = LowReadContactSerializer(read_only=True) children = RecursiveCommentSerialzier(many=True, read_only=True) class Meta: list_serializer_class = FilterCommentSerializer model = Comment exclude = ['likes', 'parent']
class PostParentSerializer(AbstractPostSerializer, serializers.ModelSerializer): '''Вывод родителя поста''' parent = RecursivePostSerialzier(read_only=True) user = LowReadContactSerializer(read_only=True) compressed_image = serializers.ImageField(read_only=True) group_owner = PartyShortSerializer(read_only=True) class Meta: model = Post fields = [ 'id', 'user', 'owner', 'parent', 'text', 'image', 'timestamp', 'group_owner', 'compressed_image' ]
class ChatOverviewSerializer(ChatSerializer): '''Сериализация контакта при обзоре''' participants = LowReadContactSerializer(many=True) def to_representation(self, value): response = super().to_representation(value) if value.is_chat: companion = user = self.context['request'].user for participant in value.participants.all(): if participant != user: companion = participant break if companion.avatar_id: small_avatar = Photo.objects.get( id=companion.avatar_id).small_picture.url else: small_avatar = None d = { 'id': companion.id, 'first_name': companion.first_name, 'last_name': companion.last_name, 'phone_number': companion.phone_number, 'slug': companion.slug, 'small_avatar': small_avatar, } response.update({'companion': d}) else: name = '' if not value.name: for participant in value.participants.all(): name += f'{participant.first_name},' response.update({'name': name[:-1]}) return response