Files
diary-system/add_comment_demo.py
maoshen 00a6aef16b feat: 添加批注功能
后端:
- Comment 模型(支持日记/任务/经验三种内容类型)
- CommentSerializer 和 CommentViewSet
- API: /api/comments/ - 批注 CRUD
- API: /api/comments/by_content/?content_type=diary&object_id=1 - 按内容获取批注
- 日记/任务/经验序列化器嵌套显示批注

前端:
- 批注样式(comments-section, comment-item)
- 添加批注输入框

使用方式:
- 北极星可以在任何日记/任务/经验下添加批注
- 批注会显示在内容下方
- 支持查看历史批注
2026-04-14 11:46:52 +00:00

76 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
批注功能演示 - 北极星可以使用这个脚本添加批注
"""
import os
import sys
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'diary_system.settings')
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))
django.setup()
from diary.models import Comment, DiaryEntry, Task, Experience
def add_comment(content_type, object_id, content, created_by='北极星'):
"""添加批注"""
comment = Comment.objects.create(
content_type=content_type,
object_id=object_id,
content=content,
created_by=created_by
)
print(f"✅ 批注已添加:{content_type} #{object_id}")
print(f" 内容:{content[:50]}...")
return comment
def show_comments(content_type, object_id):
"""查看批注"""
comments = Comment.objects.filter(
content_type=content_type,
object_id=object_id
)
print(f"\n📝 {content_type} #{object_id} 的批注:\n")
if not comments:
print(" 暂无批注")
return
for comment in comments:
print(f" [{comment.created_at.strftime('%Y-%m-%d %H:%M')}] {comment.created_by}:")
print(f" {comment.content}")
print()
if __name__ == '__main__':
# 示例:查看今天的日记
print("📖 批注功能演示\n")
print("=" * 60)
# 获取今天的日记
from django.utils import timezone
today = timezone.now().date()
entry = DiaryEntry.objects.filter(date=today).first()
if entry:
print(f"\n今天的日记:{entry.title}")
print(f"ID: {entry.id}")
# 查看批注
show_comments('diary', entry.id)
# 添加示例批注
print("\n添加示例批注...")
add_comment('diary', entry.id, '今天的日记内容很丰富!继续保持!')
# 再次查看
show_comments('diary', entry.id)
else:
print("今天还没有日记")
print("\n" + "=" * 60)
print("\n💡 使用方法:")
print(" 1. 查看批注show_comments('diary', 1)")
print(" 2. 添加批注add_comment('diary', 1, '你的批注内容')")
print("\n 支持的内容类型diary, task, experience")