-
Notifications
You must be signed in to change notification settings - Fork 2.9k
CN.1875084792839892:1c1cf413b873302f5fb459f485ff2f96_692e5f206cdc22648f7bdc86.693037da6cdc22648f7bdcce.693037dad6bed23c03ffddc3:Trae CN.T(2025/12/3 21:15:06) #797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,14 +285,68 @@ def get_queryset(self): | |
|
|
||
|
|
||
| class EsSearchView(SearchView): | ||
| def get_queryset(self): | ||
| queryset = super(EsSearchView, self).get_queryset() | ||
|
|
||
| # 获取排序参数 | ||
| sort_by = self.request.GET.get('sort', 'relevance') | ||
|
|
||
| # 根据排序参数对结果进行排序 | ||
| if sort_by == 'time': | ||
| # 按时间排序(最新在前) | ||
| queryset = queryset.order_by('-pub_date') | ||
| elif sort_by == 'views': | ||
| # 按浏览量排序(最多在前) | ||
| queryset = queryset.order_by('-views') | ||
| # 默认按相关性排序 | ||
|
|
||
| return queryset | ||
|
Comment on lines
+288
to
+303
|
||
|
|
||
| def get_context(self): | ||
| paginator, page = self.build_page() | ||
|
|
||
| # 获取当前排序参数 | ||
| sort_by = self.request.GET.get('sort', 'relevance') | ||
|
Comment on lines
+292
to
+309
|
||
|
|
||
| # 关键词高亮处理 | ||
| query = self.query | ||
| if query: | ||
| # 替换HTML特殊字符以避免XSS攻击 | ||
| query = query.replace('&', '&').replace('<', '<').replace('>', '>') | ||
|
Comment on lines
+314
to
+315
|
||
|
|
||
| # 创建正则表达式,不区分大小写 | ||
| import re | ||
|
||
| regex = re.compile(r'(' + re.escape(query) + r')', re.IGNORECASE) | ||
|
|
||
| # 对搜索结果中的标题和摘要进行关键词高亮 | ||
| for result in page.object_list: | ||
| article = result.object | ||
|
|
||
| # 高亮标题中的关键词 | ||
| if article.title: | ||
| article.title = regex.sub(r'<em class="highlight">\1</em>', article.title) | ||
|
|
||
| # 高亮摘要中的关键词 | ||
| if hasattr(article, 'excerpt') and article.excerpt: | ||
| article.excerpt = regex.sub(r'<em class="highlight">\1</em>', article.excerpt) | ||
|
|
||
| # 如果没有摘要,从正文中提取部分内容并高亮 | ||
| elif article.body: | ||
| # 提取前200个字符作为摘要 | ||
| excerpt = article.body[:200] | ||
| if len(article.body) > 200: | ||
| excerpt += '...' | ||
|
|
||
| # 高亮摘要中的关键词 | ||
| article.excerpt = regex.sub(r'<em class="highlight">\1</em>', excerpt) | ||
|
Comment on lines
+325
to
+341
|
||
|
|
||
| context = { | ||
| "query": self.query, | ||
| "form": self.form, | ||
| "page": page, | ||
| "paginator": paginator, | ||
| "suggestion": None, | ||
| "sort_by": sort_by, # 将当前排序参数传递到模板 | ||
| } | ||
| if hasattr(self.results, "query") and self.results.query.backend.include_spelling: | ||
| context["suggestion"] = self.results.query.get_spelling_suggestion() | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -52,7 +52,7 @@ def env_to_bool(env, default): | |||||||||||||||||
| 'django.contrib.staticfiles', | ||||||||||||||||||
| 'django.contrib.sites', | ||||||||||||||||||
| 'django.contrib.sitemaps', | ||||||||||||||||||
| 'mdeditor', | ||||||||||||||||||
| # 'mdeditor', | ||||||||||||||||||
|
||||||||||||||||||
| # 'mdeditor', | |
| 'mdeditor', |
Copilot
AI
Dec 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing from MySQL to SQLite is a significant downgrade for production environments. SQLite lacks concurrent write support, full-text search capabilities, and doesn't scale well. This change should only be for development/testing purposes and should not be committed to the main branch without proper configuration management (e.g., using environment variables to switch between databases).
| 'ENGINE': 'django.db.backends.sqlite3', | |
| 'NAME': BASE_DIR / 'db.sqlite3', | |
| 'ENGINE': os.environ.get('DJANGO_DB_ENGINE', 'django.db.backends.sqlite3'), | |
| 'NAME': os.environ.get('DJANGO_DB_NAME', BASE_DIR / 'db.sqlite3'), | |
| 'USER': os.environ.get('DJANGO_DB_USER', ''), | |
| 'PASSWORD': os.environ.get('DJANGO_DB_PASSWORD', ''), | |
| 'HOST': os.environ.get('DJANGO_DB_HOST', ''), | |
| 'PORT': os.environ.get('DJANGO_DB_PORT', ''), |
Copilot
AI
Dec 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The os module is imported again locally within the else block, but it's already imported at the top of the file (line 12). This redundant import should be removed.
| import os |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,7 +22,21 @@ | |||||||||||||||||||||||||||||
| from haystack.utils import get_identifier, get_model_ct | ||||||||||||||||||||||||||||||
| from haystack.utils import log as logging | ||||||||||||||||||||||||||||||
| from haystack.utils.app_loading import haystack_get_model | ||||||||||||||||||||||||||||||
| from jieba.analyse import ChineseAnalyzer | ||||||||||||||||||||||||||||||
| import jieba | ||||||||||||||||||||||||||||||
| from whoosh.analysis import Tokenizer, Token | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class ChineseTokenizer(Tokenizer): | ||||||||||||||||||||||||||||||
| def __call__(self, text, **kwargs): | ||||||||||||||||||||||||||||||
| # 使用jieba分词 | ||||||||||||||||||||||||||||||
| words = jieba.cut(text) | ||||||||||||||||||||||||||||||
| for word in words: | ||||||||||||||||||||||||||||||
| token = Token() | ||||||||||||||||||||||||||||||
| token.text = word | ||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+34
|
||||||||||||||||||||||||||||||
| # 使用jieba分词 | |
| words = jieba.cut(text) | |
| for word in words: | |
| token = Token() | |
| token.text = word | |
| # 使用jieba.tokenize获取分词及其在原文中的位置 | |
| pos = 0 | |
| for word, start, end in jieba.tokenize(text): | |
| token = Token() | |
| token.text = word | |
| token.pos = pos | |
| token.startchar = start | |
| token.endchar = end | |
| pos += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the import from 'uuslug' (django-uuslug) to 'slugify' (python-slugify) changes the slug generation behavior. The uuslug library is specifically designed for Django and handles slug uniqueness automatically, while python-slugify does not. This could lead to duplicate slug violations or inconsistent URL generation. If this change is intentional, the slug uniqueness logic needs to be implemented manually.