class MediaListSerializer(serializers.ModelSerializer): creator = UserSerializer() image = ImageSerializer() class Meta: model = Media fields = ("pk", "title", "image", "likes", "views", "creator", "status", "is_ban")
class GameSerializer(serializers.ModelSerializer): image = ImageSerializer() players_count = serializers.SerializerMethodField() game_user_pk = serializers.SerializerMethodField() def get_players_count(self, obj): return obj.game_gameusers.filter(is_deleted=False).count() def get_game_user_pk(self, obj): request = self.context["request"] user = request.user try: return GameUser.objects.get(user=user, game=obj).pk except GameUser.DoesNotExist: return None class Meta: model = Game fields = ( "pk", "slug", "name", "image", "description", "rules", "views", "likes", "hash_tags", "creator", "status", "players_count", "game_user_pk", )
class GroupListSerializer(serializers.ModelSerializer): image = ImageSerializer() relationship_theme = ChoiceDisplayField(choices=ThemeChoices.THEMES) group_type = ChoiceDisplayField(choices=ComminityTypeChoices.TYPES) requests_count = serializers.SerializerMethodField() request = serializers.SerializerMethodField() def get_requests_count(self, obj): return obj.requests.filter( status=MembershipRequest.STATUS_WAITING_MODERATION).count() def get_request(self, obj): user = self.context["request"].user request = obj.requests.filter(user=user).first() if request: return request.pk class Meta: model = Group fields = ( "pk", "slug", "name", "image", "description", "relationship_theme", "group_type", "creator", "moderators", "users", "requests_count", "request", )
class UserListSerializer(serializers.ModelSerializer): city = CitySerializer() role = ChoiceDisplayField(choices=User.ROLES) approver = UserSerializer() avatar = ImageSerializer() role = ChoiceDisplayField(choices=User.ROLES) gender = ChoiceDisplayField(choices=User.GENDERS) relationship_formats = ChoiceDisplayField(choices=User.FORMATS) relationship_themes = ChoiceDisplayField(choices=ThemeChoices.THEMES) class Meta: model = User fields = ( "pk", "slug", "name", "avatar", "email", "is_real", "approver", "role", "about", "is_ban", "gender", "relationship_formats", "relationship_themes", "birthday", "birthday_second", "city", )
class MediaSerializer(serializers.ModelSerializer): image = ImageSerializer() creator = UserSerializer() content_type = ContentTypeField() title = serializers.SerializerMethodField() def get_title(self, obj): return obj.get_title() class Meta: model = Media fields = ( "pk", "title", "description", "image", "video_code", "media_type", "hash_tags", "views", "likes", "comments_count", "creator", "status", "object_id", "content_type", )
class SignInVerifySerializer(serializers.ModelSerializer): avatar = ImageSerializer() city = CitySerializer() class Meta: model = User fields = ("pk", "slug", "name", "email", "role", "is_real", "avatar", "city", "black_list")
class NewsListSerializer(serializers.ModelSerializer): image = ImageSerializer() creator = UserSerializer() class Meta: model = News fields = ("pk", "title", "image", "creator", "description", "publish_date", "end_publish_date")
class PartyListSerializer(serializers.ModelSerializer): image = ImageSerializer() city = CitySerializer() class Meta: model = Party fields = ("pk", "slug", "image", "name", "description", "city", "status", "is_ban")
class ClubListSerializer(serializers.ModelSerializer): creator = UserSerializer() image = ImageSerializer() city = CitySerializer() class Meta: model = Club fields = ("pk", "slug", "image", "name", "description", "city", "creator", "status", "is_ban")
class UserSerializer(serializers.ModelSerializer): avatar = ImageSerializer() city = CitySerializer() online = serializers.BooleanField(source="is_online") class Meta: model = User fields = ("pk", "slug", "name", "avatar", "online", "last_seen", "city", "about", "role")
class GameItemSerializer(serializers.ModelSerializer): image = ImageSerializer() players_count = serializers.SerializerMethodField() def get_players_count(self, obj): return obj.game_gameusers.filter(is_deleted=False).count() class Meta: model = Game fields = ("pk", "slug", "name", "image", "description", "views", "likes", "creator", "status", "players_count")
class PartyListSerializer(serializers.ModelSerializer): image = ImageSerializer() city = CitySerializer() club = ClubPartySerializer() theme = ChoiceDisplayField(choices=ThemeChoices.THEMES) party_type = ChoiceDisplayField(choices=ComminityTypeChoices.TYPES) users = serializers.SerializerMethodField() user_status = serializers.SerializerMethodField() def get_users(self, obj): # TODO optimization return (PartyUser.objects.filter(party=obj).exclude( status__in=(PartyUser.STATUS_UNKNOWN, )).values_list("user_id", flat=True)) def get_user_status(self, obj): # TODO optimization user = self.context["request"].user party_user = PartyUser.objects.filter(party=obj, user=user).first() if party_user: return party_user.status else: return PartyUser.STATUS_UNKNOWN class Meta: model = Party fields = ( "pk", "slug", "club", "name", "image", "short_description", "description", "party_type", "city", "users", "views", "likes", "comments_count", "hash_tags", "theme", "start_date", "end_date", "city", "address", "geo", "status", "user_status", )
class NewsItemSerializer(serializers.ModelSerializer): image = ImageSerializer() creator = UserSerializer() news_type = ChoiceDisplayField(choices=News.TYPES) likes = serializers.SerializerMethodField() views = serializers.SerializerMethodField() comments_count = serializers.SerializerMethodField() def get_likes(self, obj): parent = obj.content_object if parent and hasattr(parent, "likes"): return parent.likes.all().values_list("pk", flat=True) return obj.likes.all().values_list("pk", flat=True) def get_views(self, obj): parent = obj.content_object if parent and hasattr(parent, "views"): return parent.views return obj.views def get_comments_count(self, obj): parent = obj.content_object if parent and hasattr(parent, "comments_count"): return parent.comments_count return obj.comments_count class Meta: model = News fields = ( "pk", "news_type", "slug", "object_id", "title", "image", "creator", "description", "publish_date", "views", "likes", "comments_count", "hash_tags", )
class MediaItemSerializer(serializers.ModelSerializer): image = ImageSerializer() creator = UserSerializer() class Meta: model = Media fields = ( "pk", "title", "description", "image", "video_code", "media_type", "hash_tags", "views", "likes", "comments_count", "creator", "status", )
class PostListSerializer(serializers.ModelSerializer): creator = UserSerializer() theme = ChoiceDisplayField(choices=Post.THEMES) image = ImageSerializer() class Meta: model = Post fields = ( "pk", "slug", "image", "title", "description", "theme", "likes", "views", "creator", "status", "is_ban", )
class PostItemSerializer(serializers.ModelSerializer): image = ImageSerializer() theme = ChoiceDisplayField(choices=Post.THEMES) class Meta: model = Post fields = ( "pk", "slug", "title", "image", "description", "views", "likes", "comments_count", "creator", "status", "theme", "is_whisper", )
class PostSerializer(serializers.ModelSerializer): image = ImageSerializer() creator = UserSerializer() class Meta: model = Post fields = ( "pk", "slug", "title", "image", "description", "post", "views", "likes", "comments_count", "hash_tags", "creator", "status", "theme", "is_whisper", )
class NewsSerializer(serializers.ModelSerializer): image = ImageSerializer() creator = UserSerializer() news_type = ChoiceDisplayField(choices=News.TYPES) class Meta: model = News fields = ( "pk", "news_type", "slug", "object_id", "title", "image", "creator", "description", "news", "publish_date", "views", "likes", "comments_count", "hash_tags", )
def get_attachments_data(self, obj): serializer = ImageSerializer(obj.attachments.all(), many=True, context=self.context) return serializer.data
def get_avatar(self, obj): avatar = obj.get_chat_avatar(current_user=self.context["request"].user) serializer = ImageSerializer(avatar, context=self.context) return serializer.data