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:
chunyu
2026-09-15 15:20:23 +08:00
parent 1e95db0bda
commit 37cad3947d
7 changed files with 264 additions and 11 deletions
+81 -10
View File
@@ -1,9 +1,67 @@
from adrf.serializers import (
ModelSerializer, SerializerMethodField,
)
from asgiref.sync import sync_to_async
from .models import Course, Chapter, ChapterContent, CourseMaterial
def _author_display(obj):
"""同步取作者展示名(跑在 sync_to_async 线程里,避免 ASGI 下 SynchronousOnlyOperation)。"""
try:
author = obj.author
except Exception:
return ''
if author is None:
return ''
try:
full = author.get_full_name()
except Exception:
full = ''
try:
username = author.username
except Exception:
username = ''
return full or username or ''
def parse_duration_minutes(raw):
"""把章节 duration 字符串解析为分钟数;解析不出返回 None。
存量格式为中文字符串(如 '30分钟'),纯数字视为分钟。
total_minutes 只对可解析章节求和,不可解析章节不计入(不伪造)。
"""
import re
if raw is None:
return None
s = str(raw).strip()
if not s:
return None
m = re.search(r'(\d+(?:\.\d+)?)', s)
if not m:
return None
try:
return int(float(m.group(1)))
except (ValueError, OverflowError):
return None
def _course_total_minutes(obj):
"""同步聚合课程总分钟数(跑在 sync_to_async 线程里)。"""
try:
durations = list(obj.chapters.values_list('duration', flat=True))
except Exception:
return None
total = 0
any_parsed = False
for d in durations:
m = parse_duration_minutes(d)
if m is not None:
total += m
any_parsed = True
return total if any_parsed else None
class ChapterSerializer(ModelSerializer):
video_poster_url = SerializerMethodField()
video_local_url = SerializerMethodField()
@@ -100,6 +158,7 @@ class CourseListSerializer(ModelSerializer):
author_name = SerializerMethodField()
chapters_count = SerializerMethodField()
students_count = SerializerMethodField()
total_minutes = SerializerMethodField()
cover_image_url = SerializerMethodField()
class Meta:
@@ -107,7 +166,7 @@ class CourseListSerializer(ModelSerializer):
fields = [
'id', 'title', 'description', 'category', 'level',
'cover_image_url', 'color', 'icon_name', 'author_name',
'chapters_count', 'students_count', 'status',
'chapters_count', 'students_count', 'total_minutes', 'status',
'sort_order', 'is_hot', 'is_new', 'created_at', 'updated_at',
]
@@ -115,7 +174,15 @@ class CourseListSerializer(ModelSerializer):
return await obj.chapters.acount()
async def get_students_count(self, obj):
return 0
# 从章节阅读记录去重统计真实在学人数
from user.models import FUser
return await FUser.objects.filter(
chapter_reads__course=obj
).distinct().acount()
async def get_total_minutes(self, obj):
# 可解析章节 duration 求和;全不可解析返回 None(前端不显示,不伪造)
return await sync_to_async(_course_total_minutes)(obj)
async def get_cover_image_url(self, obj):
if obj.cover_image:
@@ -126,9 +193,7 @@ class CourseListSerializer(ModelSerializer):
return ''
async def get_author_name(self, obj):
if obj.author:
return obj.author.get_full_name() or obj.author.username
return ''
return await sync_to_async(_author_display)(obj)
class CourseDetailSerializer(ModelSerializer):
@@ -136,6 +201,7 @@ class CourseDetailSerializer(ModelSerializer):
chapters = ChapterSerializer(many=True, read_only=True)
chapters_count = SerializerMethodField()
students_count = SerializerMethodField()
total_minutes = SerializerMethodField()
cover_image_url = SerializerMethodField()
class Meta:
@@ -143,7 +209,7 @@ class CourseDetailSerializer(ModelSerializer):
fields = [
'id', 'title', 'description', 'category', 'level',
'cover_image_url', 'color', 'icon_name', 'author_name',
'chapters', 'chapters_count', 'students_count', 'status',
'chapters', 'chapters_count', 'students_count', 'total_minutes', 'status',
'sort_order', 'is_hot', 'is_new', 'created_at', 'updated_at',
]
@@ -151,7 +217,14 @@ class CourseDetailSerializer(ModelSerializer):
return await obj.chapters.acount()
async def get_students_count(self, obj):
return 0
# 从章节阅读记录去重统计真实在学人数
from user.models import FUser
return await FUser.objects.filter(
chapter_reads__course=obj
).distinct().acount()
async def get_total_minutes(self, obj):
return await sync_to_async(_course_total_minutes)(obj)
async def get_cover_image_url(self, obj):
if obj.cover_image:
@@ -162,9 +235,7 @@ class CourseDetailSerializer(ModelSerializer):
return ''
async def get_author_name(self, obj):
if obj.author:
return obj.author.get_full_name() or obj.author.username
return ''
return await sync_to_async(_author_display)(obj)
class CourseCreateUpdateSerializer(ModelSerializer):