完善: CSRF安全修复 + 后台任务管理 + 压测全模块覆盖 + 文档

This commit is contained in:
张志挺
2026-08-26 15:21:48 +08:00
parent f39008193f
commit 36ac5f737b
61 changed files with 7281 additions and 867 deletions
+44 -30
View File
@@ -1,4 +1,18 @@
"""
简化版搜索引擎 - 直接使用 LIKE 查询,不依赖 FTS5
"""
import re
from auth import User
def _resolve_name(name):
"""将 username 解析为 display_name"""
if not name:
return ''
user = User.filter(username=name).first()
if user:
return user.display_name or user.username
return name
class SearchEngine:
@@ -17,18 +31,35 @@ class SearchEngine:
def search(self, query, status_field='status', status_value=1):
if not query or not query.strip():
return []
keywords = self._tokenize(query)
if not keywords:
return []
results = []
for mc in self._models:
items = mc['cls'].filter(**{status_field: status_value}).all()
for item in items:
score, highlights = self._score_item(item, keywords, mc['fields'])
if score > 0:
cls = mc['cls']
field_names = [f[0] for f in mc['fields']]
# 构建 LIKE 查询:对每个搜索字段做 OR 匹配
conditions = []
params = []
for kw in keywords:
for field_name in field_names:
conditions.append('"{}" LIKE ?'.format(field_name))
params.append('%{}%'.format(kw))
if status_field:
conditions.append('"{}" = ?'.format(status_field))
params.append(status_value)
sql = 'SELECT * FROM "{}" WHERE {} ORDER BY id DESC LIMIT 50'.format(
cls._table_name, ' OR '.join(conditions))
try:
cursor = cls.raw(sql, params)
rows = cursor.fetchall()
for row in rows:
item = cls._from_row(row)
title_field = 'title' if hasattr(item, 'title') else None
title = getattr(item, title_field, '') if title_field else ''
content = getattr(item, 'content', '')
@@ -38,14 +69,16 @@ class SearchEngine:
'id': item.id,
'title': title,
'content_preview': self._make_preview(content, keywords),
'author_name': author,
'author_name': _resolve_name(author),
'type': mc['type'],
'type_verbose': mc['verbose'],
'url': mc['url_tpl'].format(id=item.id),
'score': score,
'score': self._score_item(item, keywords, mc['fields']),
'created_at': getattr(item, 'created_at', ''),
'highlights': highlights,
'highlights': [],
})
except Exception:
continue
results.sort(key=lambda r: (-r['score'], r['id']))
return results
@@ -57,36 +90,17 @@ class SearchEngine:
def _score_item(self, item, keywords, fields):
score = 0
highlights = []
query_phrase = ' '.join(keywords)
for field_name, weight in fields:
value = str(getattr(item, field_name, '') or '').lower()
if not value:
continue
for kw in keywords:
count = value.count(kw)
if count > 0:
score += weight * count
if field_name == 'title':
score += weight * count * 2
if query_phrase in value:
score += 20
matched_kws = 0
for kw in keywords:
for field_name, _ in fields:
value = str(getattr(item, field_name, '') or '').lower()
if kw in value:
matched_kws += 1
break
if matched_kws > 1:
score += matched_kws * 5
return score, highlights
return score
def _make_preview(self, content, keywords, max_len=200):
if not content:
@@ -160,4 +174,4 @@ def register_search_models():
def search_all(query):
return search_engine.search(query)
return search_engine.search(query)