- 新增图像生成接口,支持试用、积分和自定义API Key模式 - 实现生成图片结果异步上传至MinIO存储,带重试机制 - 优化积分预扣除和异常退还逻辑,保障用户积分准确 - 添加获取生成历史记录接口,支持时间范围和分页 - 提供本地字典配置接口,支持模型、比例、提示模板和尺寸 - 实现图片批量上传接口,支持S3兼容对象存储 feat(admin): 增加管理员角色管理与权限分配接口 - 实现角色列表查询、角色创建、更新及删除功能 - 增加权限列表查询接口 - 实现用户角色分配接口,便于统一管理用户权限 - 增加系统字典增删查改接口,支持分类过滤和排序 - 权限控制全面覆盖管理接口,保证安全访问 feat(auth): 完善用户登录注册及权限相关接口与页面 - 实现手机号验证码发送及校验功能,保障注册安全 - 支持手机号注册、登录及退出接口,集成日志记录 - 增加修改密码功能,验证原密码后更新 - 提供动态导航菜单接口,基于权限展示不同菜单 - 实现管理界面路由及日志、角色、字典管理页面访问权限控制 - 添加系统日志查询接口,支持关键词和等级筛选 feat(app): 初始化Flask应用并配置蓝图与数据库 - 创建应用程序工厂,加载配置,初始化数据库和Redis客户端 - 注册认证、API及管理员蓝图,整合路由 - 根路由渲染主页模板 - 应用上下文中自动创建数据库表,保证运行环境准备完毕 feat(database): 提供数据库创建与迁移支持脚本 - 新增数据库创建脚本,支持自动检测是否已存在 - 添加数据库表初始化脚本,支持创建和删除所有表 - 实现RBAC权限初始化,包含基础权限和角色创建 - 新增字段手动修复脚本,添加用户API Key和积分字段 - 强制迁移脚本支持清理连接和修复表结构,初始化默认数据及角色分配 feat(config): 新增系统配置参数 - 配置数据库、Redis、Session和MinIO相关参数 - 添加AI接口地址及试用Key配置 - 集成阿里云短信服务配置及开发模式相关参数 feat(extensions): 初始化数据库、Redis和MinIO客户端 - 创建全局SQLAlchemy数据库实例和Redis客户端 - 配置基于boto3的MinIO兼容S3客户端 chore(logs): 添加示例系统日志文件 - 记录用户请求、验证码发送成功与失败的日志信息
303 lines
9.0 KiB
Python
303 lines
9.0 KiB
Python
# orm/identity.py
|
|
# Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
|
|
# <see AUTHORS file>
|
|
#
|
|
# This module is part of SQLAlchemy and is released under
|
|
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from typing import cast
|
|
from typing import Dict
|
|
from typing import Iterable
|
|
from typing import Iterator
|
|
from typing import List
|
|
from typing import NoReturn
|
|
from typing import Optional
|
|
from typing import Set
|
|
from typing import Tuple
|
|
from typing import TYPE_CHECKING
|
|
from typing import TypeVar
|
|
import weakref
|
|
|
|
from . import util as orm_util
|
|
from .. import exc as sa_exc
|
|
|
|
if TYPE_CHECKING:
|
|
from ._typing import _IdentityKeyType
|
|
from .state import InstanceState
|
|
|
|
|
|
_T = TypeVar("_T", bound=Any)
|
|
|
|
_O = TypeVar("_O", bound=object)
|
|
|
|
|
|
class IdentityMap:
|
|
_wr: weakref.ref[IdentityMap]
|
|
|
|
_dict: Dict[_IdentityKeyType[Any], Any]
|
|
_modified: Set[InstanceState[Any]]
|
|
|
|
def __init__(self) -> None:
|
|
self._dict = {}
|
|
self._modified = set()
|
|
self._wr = weakref.ref(self)
|
|
|
|
def _kill(self) -> None:
|
|
self._add_unpresent = _killed # type: ignore
|
|
|
|
def all_states(self) -> List[InstanceState[Any]]:
|
|
raise NotImplementedError()
|
|
|
|
def contains_state(self, state: InstanceState[Any]) -> bool:
|
|
raise NotImplementedError()
|
|
|
|
def __contains__(self, key: _IdentityKeyType[Any]) -> bool:
|
|
raise NotImplementedError()
|
|
|
|
def safe_discard(self, state: InstanceState[Any]) -> None:
|
|
raise NotImplementedError()
|
|
|
|
def __getitem__(self, key: _IdentityKeyType[_O]) -> _O:
|
|
raise NotImplementedError()
|
|
|
|
def get(
|
|
self, key: _IdentityKeyType[_O], default: Optional[_O] = None
|
|
) -> Optional[_O]:
|
|
raise NotImplementedError()
|
|
|
|
def fast_get_state(
|
|
self, key: _IdentityKeyType[_O]
|
|
) -> Optional[InstanceState[_O]]:
|
|
raise NotImplementedError()
|
|
|
|
def keys(self) -> Iterable[_IdentityKeyType[Any]]:
|
|
return self._dict.keys()
|
|
|
|
def values(self) -> Iterable[object]:
|
|
raise NotImplementedError()
|
|
|
|
def replace(self, state: InstanceState[_O]) -> Optional[InstanceState[_O]]:
|
|
raise NotImplementedError()
|
|
|
|
def add(self, state: InstanceState[Any]) -> bool:
|
|
raise NotImplementedError()
|
|
|
|
def _fast_discard(self, state: InstanceState[Any]) -> None:
|
|
raise NotImplementedError()
|
|
|
|
def _add_unpresent(
|
|
self, state: InstanceState[Any], key: _IdentityKeyType[Any]
|
|
) -> None:
|
|
"""optional inlined form of add() which can assume item isn't present
|
|
in the map"""
|
|
self.add(state)
|
|
|
|
def _manage_incoming_state(self, state: InstanceState[Any]) -> None:
|
|
state._instance_dict = self._wr
|
|
|
|
if state.modified:
|
|
self._modified.add(state)
|
|
|
|
def _manage_removed_state(self, state: InstanceState[Any]) -> None:
|
|
del state._instance_dict
|
|
if state.modified:
|
|
self._modified.discard(state)
|
|
|
|
def _dirty_states(self) -> Set[InstanceState[Any]]:
|
|
return self._modified
|
|
|
|
def check_modified(self) -> bool:
|
|
"""return True if any InstanceStates present have been marked
|
|
as 'modified'.
|
|
|
|
"""
|
|
return bool(self._modified)
|
|
|
|
def has_key(self, key: _IdentityKeyType[Any]) -> bool:
|
|
return key in self
|
|
|
|
def __len__(self) -> int:
|
|
return len(self._dict)
|
|
|
|
|
|
class WeakInstanceDict(IdentityMap):
|
|
_dict: Dict[_IdentityKeyType[Any], InstanceState[Any]]
|
|
|
|
def __getitem__(self, key: _IdentityKeyType[_O]) -> _O:
|
|
state = cast("InstanceState[_O]", self._dict[key])
|
|
o = state.obj()
|
|
if o is None:
|
|
raise KeyError(key)
|
|
return o
|
|
|
|
def __contains__(self, key: _IdentityKeyType[Any]) -> bool:
|
|
try:
|
|
if key in self._dict:
|
|
state = self._dict[key]
|
|
o = state.obj()
|
|
else:
|
|
return False
|
|
except KeyError:
|
|
return False
|
|
else:
|
|
return o is not None
|
|
|
|
def contains_state(self, state: InstanceState[Any]) -> bool:
|
|
if state.key in self._dict:
|
|
if TYPE_CHECKING:
|
|
assert state.key is not None
|
|
try:
|
|
return self._dict[state.key] is state
|
|
except KeyError:
|
|
return False
|
|
else:
|
|
return False
|
|
|
|
def replace(
|
|
self, state: InstanceState[Any]
|
|
) -> Optional[InstanceState[Any]]:
|
|
assert state.key is not None
|
|
if state.key in self._dict:
|
|
try:
|
|
existing = existing_non_none = self._dict[state.key]
|
|
except KeyError:
|
|
# catch gc removed the key after we just checked for it
|
|
existing = None
|
|
else:
|
|
if existing_non_none is not state:
|
|
self._manage_removed_state(existing_non_none)
|
|
else:
|
|
return None
|
|
else:
|
|
existing = None
|
|
|
|
self._dict[state.key] = state
|
|
self._manage_incoming_state(state)
|
|
return existing
|
|
|
|
def add(self, state: InstanceState[Any]) -> bool:
|
|
key = state.key
|
|
assert key is not None
|
|
# inline of self.__contains__
|
|
if key in self._dict:
|
|
try:
|
|
existing_state = self._dict[key]
|
|
except KeyError:
|
|
# catch gc removed the key after we just checked for it
|
|
pass
|
|
else:
|
|
if existing_state is not state:
|
|
o = existing_state.obj()
|
|
if o is not None:
|
|
raise sa_exc.InvalidRequestError(
|
|
"Can't attach instance "
|
|
"%s; another instance with key %s is already "
|
|
"present in this session."
|
|
% (orm_util.state_str(state), state.key)
|
|
)
|
|
else:
|
|
return False
|
|
self._dict[key] = state
|
|
self._manage_incoming_state(state)
|
|
return True
|
|
|
|
def _add_unpresent(
|
|
self, state: InstanceState[Any], key: _IdentityKeyType[Any]
|
|
) -> None:
|
|
# inlined form of add() called by loading.py
|
|
self._dict[key] = state
|
|
state._instance_dict = self._wr
|
|
|
|
def fast_get_state(
|
|
self, key: _IdentityKeyType[_O]
|
|
) -> Optional[InstanceState[_O]]:
|
|
return self._dict.get(key)
|
|
|
|
def get(
|
|
self, key: _IdentityKeyType[_O], default: Optional[_O] = None
|
|
) -> Optional[_O]:
|
|
if key not in self._dict:
|
|
return default
|
|
try:
|
|
state = cast("InstanceState[_O]", self._dict[key])
|
|
except KeyError:
|
|
# catch gc removed the key after we just checked for it
|
|
return default
|
|
else:
|
|
o = state.obj()
|
|
if o is None:
|
|
return default
|
|
return o
|
|
|
|
def items(self) -> List[Tuple[_IdentityKeyType[Any], InstanceState[Any]]]:
|
|
values = self.all_states()
|
|
result = []
|
|
for state in values:
|
|
value = state.obj()
|
|
key = state.key
|
|
assert key is not None
|
|
if value is not None:
|
|
result.append((key, value))
|
|
return result
|
|
|
|
def values(self) -> List[object]:
|
|
values = self.all_states()
|
|
result = []
|
|
for state in values:
|
|
value = state.obj()
|
|
if value is not None:
|
|
result.append(value)
|
|
|
|
return result
|
|
|
|
def __iter__(self) -> Iterator[_IdentityKeyType[Any]]:
|
|
return iter(self.keys())
|
|
|
|
def all_states(self) -> List[InstanceState[Any]]:
|
|
return list(self._dict.values())
|
|
|
|
def _fast_discard(self, state: InstanceState[Any]) -> None:
|
|
# used by InstanceState for state being
|
|
# GC'ed, inlines _managed_removed_state
|
|
key = state.key
|
|
assert key is not None
|
|
try:
|
|
st = self._dict[key]
|
|
except KeyError:
|
|
# catch gc removed the key after we just checked for it
|
|
pass
|
|
else:
|
|
if st is state:
|
|
self._dict.pop(key, None)
|
|
|
|
def discard(self, state: InstanceState[Any]) -> None:
|
|
self.safe_discard(state)
|
|
|
|
def safe_discard(self, state: InstanceState[Any]) -> None:
|
|
key = state.key
|
|
if key in self._dict:
|
|
assert key is not None
|
|
try:
|
|
st = self._dict[key]
|
|
except KeyError:
|
|
# catch gc removed the key after we just checked for it
|
|
pass
|
|
else:
|
|
if st is state:
|
|
self._dict.pop(key, None)
|
|
self._manage_removed_state(state)
|
|
|
|
|
|
def _killed(state: InstanceState[Any], key: _IdentityKeyType[Any]) -> NoReturn:
|
|
# external function to avoid creating cycles when assigned to
|
|
# the IdentityMap
|
|
raise sa_exc.InvalidRequestError(
|
|
"Object %s cannot be converted to 'persistent' state, as this "
|
|
"identity map is no longer valid. Has the owning Session "
|
|
"been closed?" % orm_util.state_str(state),
|
|
code="lkrp",
|
|
)
|