refactor(project): make pyright & ruff happy
This commit is contained in:
@@ -106,9 +106,7 @@ class V1Beatmap(AllStrModel):
|
||||
await session.exec(
|
||||
select(func.count())
|
||||
.select_from(FavouriteBeatmapset)
|
||||
.where(
|
||||
FavouriteBeatmapset.beatmapset_id == db_beatmap.beatmapset.id
|
||||
)
|
||||
.where(FavouriteBeatmapset.beatmapset_id == db_beatmap.beatmapset.id)
|
||||
)
|
||||
).one(),
|
||||
rating=0, # TODO
|
||||
@@ -154,12 +152,8 @@ async def get_beatmaps(
|
||||
beatmapset_id: int | None = Query(None, alias="s", description="谱面集 ID"),
|
||||
beatmap_id: int | None = Query(None, alias="b", description="谱面 ID"),
|
||||
user: str | None = Query(None, alias="u", description="谱师"),
|
||||
type: Literal["string", "id"] | None = Query(
|
||||
None, description="用户类型:string 用户名称 / id 用户 ID"
|
||||
),
|
||||
ruleset_id: int | None = Query(
|
||||
None, alias="m", description="Ruleset ID", ge=0, le=3
|
||||
), # TODO
|
||||
type: Literal["string", "id"] | None = Query(None, description="用户类型:string 用户名称 / id 用户 ID"),
|
||||
ruleset_id: int | None = Query(None, alias="m", description="Ruleset ID", ge=0, le=3), # TODO
|
||||
convert: bool = Query(False, alias="a", description="转谱"), # TODO
|
||||
checksum: str | None = Query(None, alias="h", description="谱面文件 MD5"),
|
||||
limit: int = Query(500, ge=1, le=500, description="返回结果数量限制"),
|
||||
@@ -181,11 +175,7 @@ async def get_beatmaps(
|
||||
else:
|
||||
beatmaps = beatmapset.beatmaps
|
||||
elif user is not None:
|
||||
where = (
|
||||
Beatmapset.user_id == user
|
||||
if type == "id" or user.isdigit()
|
||||
else Beatmapset.creator == user
|
||||
)
|
||||
where = Beatmapset.user_id == user if type == "id" or user.isdigit() else Beatmapset.creator == user
|
||||
beatmapsets = (await session.exec(select(Beatmapset).where(where))).all()
|
||||
for beatmapset in beatmapsets:
|
||||
if len(beatmaps) >= limit:
|
||||
@@ -193,11 +183,7 @@ async def get_beatmaps(
|
||||
beatmaps.extend(beatmapset.beatmaps)
|
||||
elif since is not None:
|
||||
beatmapsets = (
|
||||
await session.exec(
|
||||
select(Beatmapset)
|
||||
.where(col(Beatmapset.ranked_date) > since)
|
||||
.limit(limit)
|
||||
)
|
||||
await session.exec(select(Beatmapset).where(col(Beatmapset.ranked_date) > since).limit(limit))
|
||||
).all()
|
||||
for beatmapset in beatmapsets:
|
||||
if len(beatmaps) >= limit:
|
||||
@@ -214,11 +200,7 @@ async def get_beatmaps(
|
||||
redis,
|
||||
fetcher,
|
||||
)
|
||||
results.append(
|
||||
await V1Beatmap.from_db(
|
||||
session, beatmap, attrs.aim_difficulty, attrs.speed_difficulty
|
||||
)
|
||||
)
|
||||
results.append(await V1Beatmap.from_db(session, beatmap, attrs.aim_difficulty, attrs.speed_difficulty))
|
||||
continue
|
||||
except Exception:
|
||||
...
|
||||
|
||||
@@ -41,9 +41,7 @@ async def download_replay(
|
||||
ge=0,
|
||||
),
|
||||
score_id: int | None = Query(None, alias="s", description="成绩 ID"),
|
||||
type: Literal["string", "id"] | None = Query(
|
||||
None, description="用户类型:string 用户名称 / id 用户 ID"
|
||||
),
|
||||
type: Literal["string", "id"] | None = Query(None, description="用户类型:string 用户名称 / id 用户 ID"),
|
||||
mods: int = Query(0, description="成绩的 MOD"),
|
||||
storage_service: StorageService = Depends(get_storage_service),
|
||||
):
|
||||
@@ -58,13 +56,9 @@ async def download_replay(
|
||||
await session.exec(
|
||||
select(Score).where(
|
||||
Score.beatmap_id == beatmap,
|
||||
Score.user_id == user
|
||||
if type == "id" or user.isdigit()
|
||||
else col(Score.user).has(username=user),
|
||||
Score.user_id == user if type == "id" or user.isdigit() else col(Score.user).has(username=user),
|
||||
Score.mods == mods_,
|
||||
Score.gamemode == GameMode.from_int_extra(ruleset_id)
|
||||
if ruleset_id is not None
|
||||
else True,
|
||||
Score.gamemode == GameMode.from_int_extra(ruleset_id) if ruleset_id is not None else True,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
@@ -73,10 +67,7 @@ async def download_replay(
|
||||
except KeyError:
|
||||
raise HTTPException(status_code=400, detail="Invalid request")
|
||||
|
||||
filepath = (
|
||||
f"replays/{score_record.id}_{score_record.beatmap_id}"
|
||||
f"_{score_record.user_id}_lazer_replay.osr"
|
||||
)
|
||||
filepath = f"replays/{score_record.id}_{score_record.beatmap_id}_{score_record.user_id}_lazer_replay.osr"
|
||||
if not await storage_service.is_exists(filepath):
|
||||
raise HTTPException(status_code=404, detail="Replay file not found")
|
||||
|
||||
@@ -100,6 +91,4 @@ async def download_replay(
|
||||
await session.commit()
|
||||
|
||||
data = await storage_service.read_file(filepath)
|
||||
return ReplayModel(
|
||||
content=base64.b64encode(data).decode("utf-8"), encoding="base64"
|
||||
)
|
||||
return ReplayModel(content=base64.b64encode(data).decode("utf-8"), encoding="base64")
|
||||
|
||||
@@ -8,9 +8,7 @@ from app.dependencies.user import v1_authorize
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel, field_serializer
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/v1", dependencies=[Depends(v1_authorize)], tags=["V1 API"]
|
||||
)
|
||||
router = APIRouter(prefix="/api/v1", dependencies=[Depends(v1_authorize)], tags=["V1 API"])
|
||||
|
||||
|
||||
class AllStrModel(BaseModel):
|
||||
|
||||
@@ -70,9 +70,7 @@ async def get_user_best(
|
||||
session: Database,
|
||||
user: str = Query(..., alias="u", description="用户"),
|
||||
ruleset_id: int = Query(0, alias="m", description="Ruleset ID", ge=0),
|
||||
type: Literal["string", "id"] | None = Query(
|
||||
None, description="用户类型:string 用户名称 / id 用户 ID"
|
||||
),
|
||||
type: Literal["string", "id"] | None = Query(None, description="用户类型:string 用户名称 / id 用户 ID"),
|
||||
limit: int = Query(10, ge=1, le=100, description="返回的成绩数量"),
|
||||
):
|
||||
try:
|
||||
@@ -80,9 +78,7 @@ async def get_user_best(
|
||||
await session.exec(
|
||||
select(Score)
|
||||
.where(
|
||||
Score.user_id == user
|
||||
if type == "id" or user.isdigit()
|
||||
else col(Score.user).has(username=user),
|
||||
Score.user_id == user if type == "id" or user.isdigit() else col(Score.user).has(username=user),
|
||||
Score.gamemode == GameMode.from_int_extra(ruleset_id),
|
||||
exists().where(col(PPBestScore.score_id) == Score.id),
|
||||
)
|
||||
@@ -106,9 +102,7 @@ async def get_user_recent(
|
||||
session: Database,
|
||||
user: str = Query(..., alias="u", description="用户"),
|
||||
ruleset_id: int = Query(0, alias="m", description="Ruleset ID", ge=0),
|
||||
type: Literal["string", "id"] | None = Query(
|
||||
None, description="用户类型:string 用户名称 / id 用户 ID"
|
||||
),
|
||||
type: Literal["string", "id"] | None = Query(None, description="用户类型:string 用户名称 / id 用户 ID"),
|
||||
limit: int = Query(10, ge=1, le=100, description="返回的成绩数量"),
|
||||
):
|
||||
try:
|
||||
@@ -116,9 +110,7 @@ async def get_user_recent(
|
||||
await session.exec(
|
||||
select(Score)
|
||||
.where(
|
||||
Score.user_id == user
|
||||
if type == "id" or user.isdigit()
|
||||
else col(Score.user).has(username=user),
|
||||
Score.user_id == user if type == "id" or user.isdigit() else col(Score.user).has(username=user),
|
||||
Score.gamemode == GameMode.from_int_extra(ruleset_id),
|
||||
Score.ended_at > datetime.now(UTC) - timedelta(hours=24),
|
||||
)
|
||||
@@ -143,9 +135,7 @@ async def get_scores(
|
||||
user: str | None = Query(None, alias="u", description="用户"),
|
||||
beatmap_id: int = Query(alias="b", description="谱面 ID"),
|
||||
ruleset_id: int = Query(0, alias="m", description="Ruleset ID", ge=0),
|
||||
type: Literal["string", "id"] | None = Query(
|
||||
None, description="用户类型:string 用户名称 / id 用户 ID"
|
||||
),
|
||||
type: Literal["string", "id"] | None = Query(None, description="用户类型:string 用户名称 / id 用户 ID"),
|
||||
limit: int = Query(10, ge=1, le=100, description="返回的成绩数量"),
|
||||
mods: int = Query(0, description="成绩的 MOD"),
|
||||
):
|
||||
@@ -157,9 +147,7 @@ async def get_scores(
|
||||
.where(
|
||||
Score.gamemode == GameMode.from_int_extra(ruleset_id),
|
||||
Score.beatmap_id == beatmap_id,
|
||||
Score.user_id == user
|
||||
if type == "id" or user.isdigit()
|
||||
else col(Score.user).has(username=user),
|
||||
Score.user_id == user if type == "id" or user.isdigit() else col(Score.user).has(username=user),
|
||||
)
|
||||
.options(joinedload(Score.beatmap))
|
||||
.order_by(col(Score.classic_total_score).desc())
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from typing import Literal
|
||||
|
||||
@@ -13,7 +12,7 @@ from app.service.user_cache_service import get_user_cache_service
|
||||
|
||||
from .router import AllStrModel, router
|
||||
|
||||
from fastapi import HTTPException, Query
|
||||
from fastapi import BackgroundTasks, HTTPException, Query
|
||||
from sqlmodel import select
|
||||
|
||||
|
||||
@@ -49,9 +48,7 @@ class V1User(AllStrModel):
|
||||
return f"v1_user:{user_id}"
|
||||
|
||||
@classmethod
|
||||
async def from_db(
|
||||
cls, session: Database, db_user: User, ruleset: GameMode | None = None
|
||||
) -> "V1User":
|
||||
async def from_db(cls, session: Database, db_user: User, ruleset: GameMode | None = None) -> "V1User":
|
||||
# 确保 user_id 不为 None
|
||||
if db_user.id is None:
|
||||
raise ValueError("User ID cannot be None")
|
||||
@@ -63,9 +60,7 @@ class V1User(AllStrModel):
|
||||
current_statistics = i
|
||||
break
|
||||
if current_statistics:
|
||||
statistics = await UserStatisticsResp.from_db(
|
||||
current_statistics, session, db_user.country_code
|
||||
)
|
||||
statistics = await UserStatisticsResp.from_db(current_statistics, session, db_user.country_code)
|
||||
else:
|
||||
statistics = None
|
||||
return cls(
|
||||
@@ -78,9 +73,7 @@ class V1User(AllStrModel):
|
||||
playcount=statistics.play_count if statistics else 0,
|
||||
ranked_score=statistics.ranked_score if statistics else 0,
|
||||
total_score=statistics.total_score if statistics else 0,
|
||||
pp_rank=statistics.global_rank
|
||||
if statistics and statistics.global_rank
|
||||
else 0,
|
||||
pp_rank=statistics.global_rank if statistics and statistics.global_rank else 0,
|
||||
level=current_statistics.level_current if current_statistics else 0,
|
||||
pp_raw=statistics.pp if statistics else 0.0,
|
||||
accuracy=statistics.hit_accuracy if statistics else 0,
|
||||
@@ -91,9 +84,7 @@ class V1User(AllStrModel):
|
||||
count_rank_a=current_statistics.grade_a if current_statistics else 0,
|
||||
country=db_user.country_code,
|
||||
total_seconds_played=statistics.play_time if statistics else 0,
|
||||
pp_country_rank=statistics.country_rank
|
||||
if statistics and statistics.country_rank
|
||||
else 0,
|
||||
pp_country_rank=statistics.country_rank if statistics and statistics.country_rank else 0,
|
||||
events=[], # TODO
|
||||
)
|
||||
|
||||
@@ -106,14 +97,11 @@ class V1User(AllStrModel):
|
||||
)
|
||||
async def get_user(
|
||||
session: Database,
|
||||
background_tasks: BackgroundTasks,
|
||||
user: str = Query(..., alias="u", description="用户"),
|
||||
ruleset_id: int | None = Query(None, alias="m", description="Ruleset ID", ge=0),
|
||||
type: Literal["string", "id"] | None = Query(
|
||||
None, description="用户类型:string 用户名称 / id 用户 ID"
|
||||
),
|
||||
event_days: int = Query(
|
||||
default=1, ge=1, le=31, description="从现在起所有事件的最大天数"
|
||||
),
|
||||
type: Literal["string", "id"] | None = Query(None, description="用户类型:string 用户名称 / id 用户 ID"),
|
||||
event_days: int = Query(default=1, ge=1, le=31, description="从现在起所有事件的最大天数"),
|
||||
):
|
||||
redis = get_redis()
|
||||
cache_service = get_user_cache_service(redis)
|
||||
@@ -131,9 +119,7 @@ async def get_user(
|
||||
if is_id_query:
|
||||
try:
|
||||
user_id_for_cache = int(user)
|
||||
cached_v1_user = await cache_service.get_v1_user_from_cache(
|
||||
user_id_for_cache, ruleset
|
||||
)
|
||||
cached_v1_user = await cache_service.get_v1_user_from_cache(user_id_for_cache, ruleset)
|
||||
if cached_v1_user:
|
||||
return [V1User(**cached_v1_user)]
|
||||
except (ValueError, TypeError):
|
||||
@@ -158,9 +144,7 @@ async def get_user(
|
||||
# 异步缓存结果(如果有用户ID)
|
||||
if db_user.id is not None:
|
||||
user_data = v1_user.model_dump()
|
||||
asyncio.create_task(
|
||||
cache_service.cache_v1_user(user_data, db_user.id, ruleset)
|
||||
)
|
||||
background_tasks.add_task(cache_service.cache_v1_user, user_data, db_user.id, ruleset)
|
||||
|
||||
return [v1_user]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user