feat(C-06):关注feed+课程聚合修复
文章列表新增author_user_id/is_following与feed=following过滤;课程students_count改真实在学人数去重统计、新增total_minutes(不可解析不伪造);author_name走sync_to_async防ASGI同步阻塞。
This commit is contained in:
+11
-1
@@ -45,10 +45,12 @@ class ArticleCommentSerializer(ModelSerializer):
|
||||
class ArticleListSerializer(ModelSerializer):
|
||||
author_name = CharField(source='author.first_name', read_only=True)
|
||||
author_avatar = ImageField(source='author.avatar', read_only=True)
|
||||
author_user_id = serializers.PrimaryKeyRelatedField(source='author', read_only=True)
|
||||
comments_count = SerializerMethodField()
|
||||
read_time = SerializerMethodField()
|
||||
is_hot = SerializerMethodField()
|
||||
is_new = SerializerMethodField()
|
||||
is_following = SerializerMethodField()
|
||||
publish_date = DateTimeField(source='created_at', format='%Y-%m-%d', read_only=True)
|
||||
image = SerializerMethodField()
|
||||
|
||||
@@ -56,10 +58,18 @@ class ArticleListSerializer(ModelSerializer):
|
||||
model = Article
|
||||
fields = [
|
||||
'id', 'title', 'excerpt', 'category', 'author_name', 'author_avatar',
|
||||
'views', 'likes', 'comments_count', 'publish_date', 'image', 'tags',
|
||||
'author_user_id', 'views', 'likes', 'comments_count', 'publish_date', 'image', 'tags',
|
||||
'read_time', 'is_featured', 'is_hot', 'is_new', 'is_top', 'status',
|
||||
'is_following',
|
||||
]
|
||||
|
||||
async def get_is_following(self, obj):
|
||||
request = self.context.get('request')
|
||||
if request and request.user.is_authenticated:
|
||||
from user.models import Follow
|
||||
return await Follow.objects.filter(follower=request.user, following=obj.author_id).aexists()
|
||||
return False
|
||||
|
||||
async def get_comments_count(self, obj):
|
||||
return await obj.comments.acount()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user