Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions app/api/v1/endpoints/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from app.utils.get_db import get_db
from app.utils.auth import get_current_user
from app.curd.article import crud_upload_to_self_folder, crud_get_self_folders, crud_get_articles_in_folder, crud_self_create_folder, crud_self_article_to_recycle_bin, crud_self_folder_to_recycle_bin, crud_read_article, crud_import_self_folder, crud_export_self_folder,crud_create_tag, crud_delete_tag, crud_get_article_tags, crud_all_tags_order, crud_change_folder_name, crud_change_article_name
from app.curd.article import crud_upload_to_self_folder, crud_get_self_folders, crud_get_articles_in_folder, crud_self_create_folder, crud_self_article_to_recycle_bin, crud_self_folder_to_recycle_bin, crud_read_article, crud_import_self_folder, crud_export_self_folder,crud_create_tag, crud_delete_tag, crud_get_article_tags, crud_all_tags_order, crud_change_folder_name, crud_change_article_name, crud_article_statistic
from app.schemas.article import SelfCreateFolder

router = APIRouter()
Expand All @@ -25,10 +25,9 @@ async def upload_to_self_folder(folder_id: int = Query(...), article: UploadFile
# 新建 Article 记录
article_id = await crud_upload_to_self_folder(name, folder_id, db)

# 存储文件,暂时存储到本地
save_dir = "articles"
os.makedirs(save_dir, exist_ok=True) # 如果目录不存在则创建
save_path = os.path.join(save_dir, f"{article_id}.pdf")
# 存储到云存储位置
os.makedirs("/lhcos-data", exist_ok=True)
save_path = os.path.join("/lhcos-data", f"{article_id}.pdf")
with open(save_path, "wb") as f:
content = await article.read()
f.write(content)
Expand Down Expand Up @@ -82,10 +81,9 @@ async def self_folder_to_recycle_bin(folder_id: int = Query(...), db: AsyncSessi

@router.post("/annotateSelfArticle", response_model="dict")
async def annotate_self_article(article_id: int = Query(...), article: UploadFile = File(...)):
# 存储文件,将新文件暂时存储到本地
save_dir = "articles"
os.makedirs(save_dir, exist_ok=True) # 如果目录不存在则创建
save_path = os.path.join(save_dir, f"{article_id}.pdf")
# 将新文件存储到云存储位置
os.makedirs("/lhcos-data", exist_ok=True)
save_path = os.path.join("/lhcos-data", f"{article_id}.pdf")
with open(save_path, "wb") as f:
content = await article.read()
f.write(content)
Expand All @@ -95,7 +93,7 @@ async def annotate_self_article(article_id: int = Query(...), article: UploadFil
@router.get("/readArticle", response_class=FileResponse)
async def read_article(article_id: int = Query(...), db: AsyncSession = Depends(get_db)):

file_path = f"articles/{article_id}.pdf"
file_path = f"/lhcos-data/{article_id}.pdf"

# 查询文件名
article_name = await crud_read_article(article_id, db)
Expand All @@ -119,14 +117,14 @@ async def import_self_folder(folder_name: str = Query(...), zip: UploadFile = Fi
# 记入数据库
result = await crud_import_self_folder(folder_name, article_names, user_id, db)

# 存储文献,暂时存储到本地
os.makedirs("articles", exist_ok=True)
# 存储文献到云存储
os.makedirs("/lhcos-data", exist_ok=True)
for i in range(0, len(result), 2):
article_id = result[i]
article_name = result[i + 1]
pdf_filename_in_zip = f"{article_name}.pdf"
with zip_file.open(pdf_filename_in_zip) as source_file:
target_path = os.path.join("articles", f"{article_id}.pdf")
target_path = os.path.join("/lhcos-data", f"{article_id}.pdf")
with open(target_path, "wb") as out_file:
out_file.write(source_file.read())

Expand All @@ -141,7 +139,7 @@ async def export_self_folder(background_tasks: BackgroundTasks, folder_id: int =

with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
for article_id, article_name in zip(article_ids, article_names):
pdf_path = os.path.join("articles", f"{article_id}.pdf")
pdf_path = os.path.join("/lhcos-data", f"{article_id}.pdf")
arcname = f"{article_name}.pdf" # 压缩包内的文件名
zipf.write(pdf_path, arcname=arcname)

Expand Down
9 changes: 7 additions & 2 deletions app/api/v1/endpoints/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from app.schemas.auth import UserCreate, UserLogin, UserSendCode, ReFreshToken
from app.core.config import settings
from app.curd.user import get_user_by_email, create_user
from app.curd.article import crud_self_create_folder
from app.curd.article import crud_self_create_folder, crud_article_statistic
from app.utils.get_db import get_db
from app.utils.redis import get_redis_client

Expand Down Expand Up @@ -146,4 +146,9 @@ async def send_code(user_send_code: UserSendCode):
return {"msg": "Verification code sent"}

except aiosmtplib.SMTPException as e:
raise HTTPException(status_code=500, detail=f"Failed to send email: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to send email: {str(e)}")

@router.get("/articleStatistic", response_model="dict")
async def article_statistic(db: AsyncSession = Depends(get_db)):
articles = await crud_article_statistic(db)
return {"articles": articles}
37 changes: 36 additions & 1 deletion app/curd/article.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, delete
from sqlalchemy import func, cast, Date
from datetime import datetime, timedelta
from app.models.model import User, Group, Folder, Article, Note, Tag, user_group

async def crud_upload_to_self_folder(name: str, folder_id: int, db: AsyncSession):
Expand Down Expand Up @@ -148,4 +150,37 @@ async def crud_change_article_name(article_id: int, article_name: str, db: Async

article.name = article_name
await db.commit()
await db.refresh(article)
await db.refresh(article)

async def crud_article_statistic(db: AsyncSession):
# 获取明天日期和7天前的日期
tomorrow = datetime.now().date() + timedelta(days=1)
seven_days_ago = datetime.now().date() - timedelta(days=6)

# 查询近7天内的笔记数目,按日期分组
query = (
select(
cast(Article.create_time, Date).label("date"), # 按日期分组
func.count(Article.id).label("count") # 统计每日期的笔记数
)
.where(
Article.create_time >= seven_days_ago, # 大于等于7天前的0点
Article.create_time < tomorrow # 小于明天0点
)
.group_by(cast(Article.create_time, Date)) # 按日期分组
.order_by(cast(Article.create_time, Date)) # 按日期排序
)

# 执行查询
result = await db.execute(query)
data = result.fetchall()

# 格式化结果为字典列表
articles = [{"date": row.date, "count": row.count} for row in data]

# 若某日期没有记录,则为0
for i in range(0, 7):
if i == len(articles) or articles[i].get("date") != seven_days_ago + timedelta(days=i):
articles.insert(i, {"date": seven_days_ago + timedelta(days=i), "count": 0})

return articles