Files
diary-system/add_comment_demo.py

76 lines
2.2 KiB
Python
Raw Permalink Normal View History

#!/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")