refactor: centralize default end user session ID constant (#25416)

This PR refactors the handling of the default end user session ID by centralizing it as an enum in the models module where the `EndUser` model is defined. This improves code organization and makes the relationship between the constant and the model clearer.

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
This commit is contained in:
Yeuoly
2025-09-12 10:27:16 +08:00
committed by GitHub
parent 394b0ac9c0
commit ec808f3fe8
6 changed files with 17 additions and 23 deletions

View File

@@ -8,11 +8,10 @@ from flask_restx import reqparse
from pydantic import BaseModel
from sqlalchemy.orm import Session
from core.file.constants import DEFAULT_SERVICE_API_USER_ID
from extensions.ext_database import db
from libs.login import current_user
from models.account import Tenant
from models.model import EndUser
from models.model import DefaultEndUserSessionID, EndUser
P = ParamSpec("P")
R = TypeVar("R")
@@ -28,7 +27,7 @@ def get_user(tenant_id: str, user_id: str | None) -> EndUser:
try:
with Session(db.engine) as session:
if not user_id:
user_id = DEFAULT_SERVICE_API_USER_ID
user_id = DefaultEndUserSessionID.DEFAULT_SESSION_ID.value
user_model = (
session.query(EndUser)
@@ -42,7 +41,7 @@ def get_user(tenant_id: str, user_id: str | None) -> EndUser:
user_model = EndUser(
tenant_id=tenant_id,
type="service_api",
is_anonymous=user_id == DEFAULT_SERVICE_API_USER_ID,
is_anonymous=user_id == DefaultEndUserSessionID.DEFAULT_SESSION_ID.value,
session_id=user_id,
)
session.add(user_model)
@@ -73,7 +72,7 @@ def get_user_tenant(view: Optional[Callable[P, R]] = None):
raise ValueError("tenant_id is required")
if not user_id:
user_id = DEFAULT_SERVICE_API_USER_ID
user_id = DefaultEndUserSessionID.DEFAULT_SESSION_ID.value
try:
tenant_model = (