38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
"""验证 images.py 未引用扫描切换到 Article 后逻辑正确。
|
|
独立执行 get_unreferenced_images 相同的查询,观察统计。
|
|
"""
|
|
import re
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.database import Base # noqa: F401
|
|
from app.models.image import Image
|
|
from app.models.article import Article
|
|
from app.models.column import ColumnModel # noqa: F401 触发关系映射
|
|
|
|
engine = create_engine('sqlite:///cms.db')
|
|
with Session(engine) as db:
|
|
articles = db.query(Article).all()
|
|
print(f"articles 总数: {len(articles)}")
|
|
|
|
referenced_paths = set()
|
|
for post in articles:
|
|
if post.cover_image:
|
|
referenced_paths.add(post.cover_image)
|
|
if post.content:
|
|
md_images = re.findall(r'!\[.*?\]\((.*?)\)', post.content)
|
|
html_images = re.findall(r'<img[^>]+src=["\']([^"\']+)["\']', post.content)
|
|
for path in md_images + html_images:
|
|
referenced_paths.add(path)
|
|
|
|
print(f"被引用的图片路径(去重): {len(referenced_paths)}")
|
|
|
|
all_images = db.query(Image).all()
|
|
print(f"images 表总数: {len(all_images)}")
|
|
|
|
unreferenced = [img for img in all_images if img.file_path not in referenced_paths]
|
|
referenced = [img for img in all_images if img.file_path in referenced_paths]
|
|
print(f"被引用: {len(referenced)}")
|
|
print(f"未引用: {len(unreferenced)}")
|
|
print(f"\n结论: 只要被引用>0,就证明 images.py 切 Article 后能识别 articles 表的图片")
|