-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
fix: Download template error #2081
Changes from 2 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 |
---|---|---|
|
@@ -65,6 +65,8 @@ | |
embedding_by_document_list | ||
from setting.models import Model | ||
from smartdoc.conf import PROJECT_DIR | ||
from django.utils.translation import gettext_lazy as _, gettext, to_locale | ||
from django.utils.translation import get_language | ||
|
||
parse_qa_handle_list = [XlsParseQAHandle(), CsvParseQAHandle(), XlsxParseQAHandle(), ZipParseQAHandle()] | ||
parse_table_handle_list = [CsvSplitTableHandle(), XlsSplitTableHandle(), XlsxSplitTableHandle()] | ||
|
@@ -240,15 +242,15 @@ def get_request_params_api(): | |
def export(self, with_valid=True): | ||
if with_valid: | ||
self.is_valid(raise_exception=True) | ||
|
||
language = get_language() | ||
if self.data.get('type') == 'csv': | ||
file = open(os.path.join(PROJECT_DIR, "apps", "dataset", 'template', 'csv_template.csv'), "rb") | ||
file = open(os.path.join(PROJECT_DIR, "apps", "dataset", 'template', f'csv_template_{to_locale(language)}.csv'), "rb") | ||
content = file.read() | ||
file.close() | ||
return HttpResponse(content, status=200, headers={'Content-Type': 'text/cxv', | ||
'Content-Disposition': 'attachment; filename="csv_template.csv"'}) | ||
elif self.data.get('type') == 'excel': | ||
file = open(os.path.join(PROJECT_DIR, "apps", "dataset", 'template', 'excel_template.xlsx'), "rb") | ||
file = open(os.path.join(PROJECT_DIR, "apps", "dataset", 'template', f'csv_template_{to_locale(language)}.xlsx'), "rb") | ||
content = file.read() | ||
file.close() | ||
return HttpResponse(content, status=200, headers={'Content-Type': 'application/vnd.ms-excel', | ||
|
@@ -257,16 +259,18 @@ def export(self, with_valid=True): | |
def table_export(self, with_valid=True): | ||
if with_valid: | ||
self.is_valid(raise_exception=True) | ||
|
||
language = get_language() | ||
if self.data.get('type') == 'csv': | ||
file = open(os.path.join(PROJECT_DIR, "apps", "dataset", 'template', _('MaxKB table template.csv')), | ||
"rb") | ||
file = open( | ||
os.path.join(PROJECT_DIR, "apps", "dataset", 'template', f'table_template_{to_locale(language)}.csv'), | ||
"rb") | ||
content = file.read() | ||
file.close() | ||
return HttpResponse(content, status=200, headers={'Content-Type': 'text/cxv', | ||
'Content-Disposition': 'attachment; filename="csv_template.csv"'}) | ||
elif self.data.get('type') == 'excel': | ||
file = open(os.path.join(PROJECT_DIR, "apps", "dataset", 'template', _('MaxKB table template.xlsx')), | ||
file = open(os.path.join(PROJECT_DIR, "apps", "dataset", 'template', | ||
f'table_template_{to_locale(language)}.xlsx'), | ||
"rb") | ||
content = file.read() | ||
file.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several improvements and corrections needed in the provided code:
Here is a corrected version of the code: from django.db.models import QuerySet, Count
from django.db.models.functions import Substr, Reverse
from django.http import HttpResponse
+import os
from drf_yasg import openapi
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
from rest_framework import serializers
def get_request_params_api():
... # Existing code remains unchanged
class ExportSerializer(serializers.Serializer):
type = serializers.CharField()
def export(self, with_valid=True):
if with_valid:
self.is_valid(raise_exception=True)
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
if self.data.get('type') == 'csv':
file_path = os.path.join(PROJECT_DIR, "apps", "dataset", 'template', f'csv_template_{to_locale(get_language())}.csv')
print(f"File path: {file_path}")
with open(file_path, "rb") as file:
content = file.read()
return HttpResponse(content, status=200, headers={
'Content-Type': 'text/csv',
'Content-Disposition': f'attachment; filename="csv_template.csv"'
})
elif self.data.get('type') == 'excel':
file_path = os.path.join(PROJECT_DIR, "apps", "dataset", 'template', f'excel_template_{to_locale(get_language())}.xlsx')
print(f"File path: {file_path}")
with open(file_path, "rb") as file:
content = file.read()
return HttpResponse(content, status=200, headers={
'Content-Type': 'application/vnd.ms-excel',
'Content-Disposition': f'attachment; filename="excel_template.xlsx"'
})
def table_export(self, with_valid=True):
if with_valid:
self.is_valid(raise_exception=True)
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
language = get_language()
if self.data.get('type') == 'csv':
file_path =os.path.join(PROJECT_DIR, "apps", "dataset", 'template', f'table_template_{to_locale(get_current_language())}.csv')
print(f"Table File path: {file_path}")
with open(file_path, "rb") as file:
content = file.read()
return HttpResponse(content, status=200, headers={
'Content-Type': 'text/csv',
'Content-Disposition': f'attachment; filename="table_template.csv"'
})
elif self.data.get('type') == 'excel':
file_path = os.path.join(PROJECT_DIR, "apps", "dataset", 'template', f'table_template_{to_locale(get_current_language())}.xlsx')
print(f"Excel Table File path: {file_path}")
with open(file_path, "rb") as file:
content = file.read()
return HttpResponse(content, status=200, headers={
'Content-Type': 'application/vnd.ms-excel',
'Content-Disposition': f'attachment; filename="table_template.xlsx"'
}) Key Corrections:
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Section title (optional), section content (required, question answer, no more than 4096 characters)), question (optional, one per line in the cell) | ||
MaxKB product introduction, "MaxKB is a knowledge base question and answer system based on the LLM large language model. MaxKB = Max Knowledge Base aims to become the most powerful brain of the enterprise. | ||
Out-of-the-box: supports direct uploading of documents, automatic crawling of online documents, automatic text splitting and vectorization, and a good intelligent Q&A interactive experience; | ||
Seamless embedding: supports rapid embedding into third-party business systems with zero coding; | ||
Multi-model support: Supports docking with mainstream large models, including Ollama local private large models (such as Llama 2, Llama 3, qwen), Tongyi Qianwen, OpenAI, Azure OpenAI, Kimi, Zhipu AI, iFlytek Spark and Baidu Qianfan Large models etc. ","What is MaxKB? | ||
MaxKB product introduction | ||
Large language model supported by MaxKB | ||
MaxKB advantages" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
分段標題(選填),分段內容(必填,問題答案,最長不超過4096個字元)),問題(選填,單元格內一行一個) | ||
MaxKB產品介紹,"MaxKB 是一款基於 LLM 大語言模型的知識庫問答系統。MaxKB = Max Knowledge Base,旨在成為企業的最強大大腦。 | ||
開箱即用:支援直接上傳文檔、自動爬取線上文檔,支援文字自動分割、向量化,智慧問答互動體驗好; | ||
無縫嵌入:支援零編碼快速嵌入到第三方業務系統; | ||
多模型支援:支持對接主流的大模型,包括Ollama 本地私有大模型(如Llama 2、Llama 3、qwen)、通義千問、OpenAI、Azure OpenAI、Kimi、智譜AI、訊飛星火和百度千帆大模型等。 ","MaxKB是什麼? | ||
MaxKB產品介紹 | ||
MaxKB支援的大語言模型 | ||
MaxKB優勢" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Position, reimbursement type, first-tier city reimbursement standard (yuan), second-tier city reimbursement standard (yuan), third-tier city reimbursement standard (yuan) | ||
Ordinary employees, accommodation expenses, 500, 400, 300 | ||
Department head, accommodation fee, 600, 500, 400 | ||
Department director, accommodation fee, 700, 600, 500 | ||
Regional general manager, accommodation fee, 800, 700, 600 | ||
Ordinary employees, food expenses, 50, 40, 30 | ||
Department head, food expenses, 50, 40, 30 | ||
Department director, food expenses, 50, 40, 30 | ||
Regional general manager, food expenses, 50, 40, 30 | ||
Ordinary employees, transportation expenses, 50, 40, 30 | ||
Department head, transportation expenses, 50, 40, 30 | ||
Department director, transportation expenses, 50, 40, 30 | ||
Regional general manager, transportation expenses, 50, 40, 30 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
職務,報銷類型,一線城市報銷標準(元),二線城市報銷標準(元),三線城市報銷標準(元) | ||
普通員工,住宿費,500,400,300 | ||
部門主管,住宿費,600,500,400 | ||
部門總監,住宿費,700,600,500 | ||
區域總經理,住宿費,800,700,600 | ||
普通員工,伙食費,50,40,30 | ||
部門主管,伙食費,50,40,30 | ||
部門總監,伙食費,50,40,30 | ||
區域總經理,伙食費,50,40,30 | ||
普通員工,交通費,50,40,30 | ||
部門主管,交通費,50,40,30 | ||
部門總監,交通費,50,40,30 | ||
區域總經理,交通費,50,40,30 |
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.
get_language()
is called but not used directly in a translation string (_('MaxKB table template.csv')
or'table_template_{}{}'.format(to_locale(language), '.xlsx')
). This can be refactored to use the locale-specific path directly if needed.Overall, the code appears to follow Python best practices and standard conventions.