расширение выгрузки
All checks were successful
continuous-integration/drone/push Build is passing

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-05-01 23:33:40 +10:00
parent 994479fd9d
commit 8f86c51d19
3 changed files with 52 additions and 3 deletions

View File

@@ -114,6 +114,7 @@ def setup_routes(app: FastAPI) -> None:
async def download_all(dates: DownloadRange, background_tasks: BackgroundTasks):
date_start = dates.data_start
date_finish = dates.data_finish
field_name = getattr(dates, 'field_name', 'status') # Поле для фильтрации (по умолчанию 'status')
try:
start_date = datetime.strptime(date_start, "%Y-%m-%d")
@@ -124,8 +125,17 @@ def setup_routes(app: FastAPI) -> None:
if start_date > finish_date:
return {"error": "Дата начала не может быть позже даты окончания"}
all_files = []
# 1. Получаем список заголовков из БД
try:
titles_from_db = wp.get_articles_by_filter(field_name, date_start, date_finish)
except Exception as e:
return {"error": f"Ошибка при получении данных из БД: {e}"}
if not titles_from_db:
return {"error": "Нет статей с выбранным фильтром за указанный период", "field_name": field_name}
# 2. Собираем все файлы .docx за период
all_files = []
current_date = start_date
while current_date <= finish_date:
date_path = current_date.strftime("%Y/%m/%d")
@@ -134,12 +144,18 @@ def setup_routes(app: FastAPI) -> None:
if os.path.exists(full_dir_path):
for file in os.listdir(full_dir_path):
if file.endswith('.docx'):
all_files.append(os.path.join(full_dir_path, file))
# 3. Фильтруем: добавляем только если название файла совпадает с заголовком из БД
file_title = file[:-5] # убираем расширение .docx
if file_title in titles_from_db:
all_files.append(os.path.join(full_dir_path, file))
current_date += timedelta(days=1)
if not all_files:
return {"error": "Файлы не найдены за указанный период", "date_start": date_start, "date_finish": date_finish}
return {"error": "Файлы не найдены за указанный период",
"date_start": date_start,
"date_finish": date_finish,
"titles_found": len(titles_from_db)}
archive_name = f"documents_{date_start}_{date_finish}.zip"
archive_path = os.path.join(DOCUMENTS_DIR, archive_name)

View File

@@ -31,3 +31,4 @@ class DownloadRange(BaseModel):
"""Диапазон дат для скачивания файлов"""
data_start: str
data_finish: str
field_name: str = "status"