rainycode 1.6.2__tar.gz → 1.6.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. {rainycode-1.6.2 → rainycode-1.6.3}/PKG-INFO +1 -1
  2. {rainycode-1.6.2 → rainycode-1.6.3}/common_depends/auth_depend.py +3 -3
  3. {rainycode-1.6.2 → rainycode-1.6.3}/common_servers/service/file_svc.py +2 -2
  4. rainycode-1.6.3/common_utils/pagination.py +34 -0
  5. {rainycode-1.6.2 → rainycode-1.6.3}/core/start.py +10 -10
  6. {rainycode-1.6.2 → rainycode-1.6.3}/pyproject.toml +1 -1
  7. {rainycode-1.6.2 → rainycode-1.6.3}/rainycode.egg-info/PKG-INFO +1 -1
  8. {rainycode-1.6.2 → rainycode-1.6.3}/rainycode.egg-info/SOURCES.txt +1 -0
  9. {rainycode-1.6.2 → rainycode-1.6.3}/common_base/aiorequests.py +0 -0
  10. {rainycode-1.6.2 → rainycode-1.6.3}/common_base/consts.py +0 -0
  11. {rainycode-1.6.2 → rainycode-1.6.3}/common_base/exception.py +0 -0
  12. {rainycode-1.6.2 → rainycode-1.6.3}/common_base/logging.py +0 -0
  13. {rainycode-1.6.2 → rainycode-1.6.3}/common_base/response.py +0 -0
  14. {rainycode-1.6.2 → rainycode-1.6.3}/common_models/__init__.py +0 -0
  15. {rainycode-1.6.2 → rainycode-1.6.3}/common_models/base_model.py +0 -0
  16. {rainycode-1.6.2 → rainycode-1.6.3}/common_models/file_model.py +0 -0
  17. {rainycode-1.6.2 → rainycode-1.6.3}/common_models/user_model.py +0 -0
  18. {rainycode-1.6.2 → rainycode-1.6.3}/common_models/wechat_model.py +0 -0
  19. {rainycode-1.6.2 → rainycode-1.6.3}/common_servers/api/auth_api.py +0 -0
  20. {rainycode-1.6.2 → rainycode-1.6.3}/common_servers/api/wechat_api.py +0 -0
  21. {rainycode-1.6.2 → rainycode-1.6.3}/common_servers/router.py +0 -0
  22. {rainycode-1.6.2 → rainycode-1.6.3}/common_servers/schemas/auth_schema.py +0 -0
  23. {rainycode-1.6.2 → rainycode-1.6.3}/common_servers/service/auth_svc.py +0 -0
  24. {rainycode-1.6.2 → rainycode-1.6.3}/common_servers/service/wechat_svc.py +0 -0
  25. {rainycode-1.6.2 → rainycode-1.6.3}/common_utils/bcrypt_util.py +0 -0
  26. {rainycode-1.6.2 → rainycode-1.6.3}/common_utils/captcha_util.py +0 -0
  27. {rainycode-1.6.2 → rainycode-1.6.3}/common_utils/datetime_util.py +0 -0
  28. {rainycode-1.6.2 → rainycode-1.6.3}/common_utils/ip_util.py +0 -0
  29. {rainycode-1.6.2 → rainycode-1.6.3}/common_utils/jwt_util.py +0 -0
  30. {rainycode-1.6.2 → rainycode-1.6.3}/common_utils/snowflake_util.py +0 -0
  31. {rainycode-1.6.2 → rainycode-1.6.3}/common_utils/wechat_util.py +0 -0
  32. {rainycode-1.6.2 → rainycode-1.6.3}/core/base_config.py +0 -0
  33. {rainycode-1.6.2 → rainycode-1.6.3}/core/databases/aiodb.py +0 -0
  34. {rainycode-1.6.2 → rainycode-1.6.3}/core/databases/aioredis.py +0 -0
  35. {rainycode-1.6.2 → rainycode-1.6.3}/core/middleware/http_middleware.py +0 -0
  36. {rainycode-1.6.2 → rainycode-1.6.3}/rainycode.egg-info/dependency_links.txt +0 -0
  37. {rainycode-1.6.2 → rainycode-1.6.3}/rainycode.egg-info/requires.txt +0 -0
  38. {rainycode-1.6.2 → rainycode-1.6.3}/rainycode.egg-info/top_level.txt +0 -0
  39. {rainycode-1.6.2 → rainycode-1.6.3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rainycode
3
- Version: 1.6.2
3
+ Version: 1.6.3
4
4
  Summary: FastAPI base modules
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: aerich==0.9.2
@@ -11,7 +11,7 @@ from common_base.exception import CustomException
11
11
  from common_models.user_model import User
12
12
  from common_utils.jwt_util import JwtUtil
13
13
  from core.databases.aioredis import AioRedis
14
- from core.start import base_config
14
+ from core.base_config import base_config
15
15
 
16
16
  oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"/api/{base_config.app_name}/auth/login", auto_error=False)
17
17
 
@@ -21,7 +21,7 @@ async def get_current_user(request: Request, token: str, security_scopes: Securi
21
21
  """
22
22
  payload = JwtUtil.decode_token(token)
23
23
  if not payload:
24
- raise CustomException(msg=f"无效的令牌 {token}", status_code=401)
24
+ raise CustomException(msg="无效的令牌", status_code=401)
25
25
 
26
26
  # 检查是否在黑名单中
27
27
  if await AioRedis.get(f"{REDIS_PREFIX_BLOCKLIST}{token}"):
@@ -29,7 +29,7 @@ async def get_current_user(request: Request, token: str, security_scopes: Securi
29
29
 
30
30
  user_id = payload.get("sub")
31
31
  if not user_id:
32
- raise CustomException(msg=f"无效的令牌 {token}", status_code=401)
32
+ raise CustomException(msg="无效的令牌", status_code=401)
33
33
 
34
34
  # 1. 尝试从缓存获取
35
35
  user = None
@@ -16,8 +16,8 @@ class FileService:
16
16
  cls,
17
17
  user: User,
18
18
  file,
19
- *,
20
19
  subdir: str,
20
+ upload_root: str = base_config.upload_root,
21
21
  allowed_exts: set[str] = base_config.upload_allowed_image_exts,
22
22
  max_bytes: int = base_config.upload_max_bytes,
23
23
  ) -> dict:
@@ -37,7 +37,7 @@ class FileService:
37
37
 
38
38
  filename = f"{uuid.uuid4().hex}.{ext}"
39
39
  relative = f"{subdir}/{filename}" # 磁盘相对路径 & File.url(不含前缀)
40
- path = Path(base_config.upload_root) / relative
40
+ path = Path(upload_root) / relative
41
41
  path.parent.mkdir(parents=True, exist_ok=True)
42
42
  path.write_bytes(content)
43
43
 
@@ -0,0 +1,34 @@
1
+ class Pagination:
2
+ """
3
+ 对 Tortoise ORM 查询集执行分页,并返回总记录数和当前页数据。
4
+
5
+ 示例:
6
+ qs = Order.filter(couple_id=couple.id).order_by('-id')
7
+ page = await Pagination.page(qs, page_no=page_no, page_size=page_size)
8
+
9
+ 返回结构为 {'items': 当前页模型对象列表, 'count': 符合查询条件的总数}。
10
+ 本类只负责查询计数和 limit/offset,不负责序列化或组装业务字段。
11
+ 调用方应在需要时转换模型对象,并确保查询集具有稳定的排序规则。
12
+ """
13
+ DEFAULT_PAGE_SIZE = 20
14
+ MAX_PAGE_SIZE = 999
15
+
16
+ @classmethod
17
+ async def page(cls, qs, page_no: int | None = None, page_size: int | None = None) -> dict:
18
+ """获取指定页的数据。
19
+
20
+ Args:
21
+ qs: 已完成筛选及排序的 Tortoise ORM 查询集。
22
+ page_no: 页码,从 1 开始;未提供或小于 1 时按第 1 页处理。
23
+ page_size: 每页条数;未提供时使用默认值,并限制在 1 到 MAX_PAGE_SIZE 之间。
24
+
25
+ Returns:
26
+ 包含当前页模型对象列表 ``items`` 和匹配记录总数 ``count`` 的字典。
27
+ """
28
+ page_no = page_no or 1
29
+ page_no = page_no if page_no > 0 else 1
30
+ page_size = page_size or cls.DEFAULT_PAGE_SIZE
31
+ page_size = min(max(page_size, 1), cls.MAX_PAGE_SIZE)
32
+ count = await qs.count()
33
+ items = await qs.limit(page_size).offset((page_no - 1) * page_size)
34
+ return {'items': items, 'count': count}
@@ -13,7 +13,7 @@ from common_base.exception import (
13
13
  ValueExceptionHandler,
14
14
  )
15
15
  from common_base.logging import logger
16
- from core.base_config import BaseConfig, base_config
16
+ from core.base_config import BaseConfig
17
17
  from core.databases.aiodb import AioDb
18
18
  from core.middleware.http_middleware import RequestHttpMiddleware
19
19
  from fastapi import FastAPI, HTTPException
@@ -39,9 +39,9 @@ def start_server(config: BaseConfig):
39
39
  # 注册异常处理
40
40
  register_exception(app)
41
41
  # 注册中间件
42
- register_middlewares(app)
42
+ register_middlewares(app, config)
43
43
  # 注册静态文件
44
- register_files(app)
44
+ register_files(app, config)
45
45
 
46
46
  return app
47
47
 
@@ -79,25 +79,25 @@ def register_exception(app: FastAPI):
79
79
  app.add_exception_handler(Exception, AllExceptionHandler) # pyright: ignore[reportArgumentType]
80
80
 
81
81
 
82
- def register_middlewares(app: FastAPI) -> None:
82
+ def register_middlewares(app: FastAPI, config: BaseConfig) -> None:
83
83
  """
84
84
  注册中间件
85
85
  """
86
86
  app.add_middleware(RequestHttpMiddleware)
87
87
  app.add_middleware(
88
88
  CORSMiddleware,
89
- allow_origins=base_config.cors_origins,
90
- allow_credentials=base_config.cors_allow_credentials,
91
- allow_methods=base_config.cors_allow_methods,
92
- allow_headers=base_config.cors_allow_headers
89
+ allow_origins=config.cors_origins,
90
+ allow_credentials=config.cors_allow_credentials,
91
+ allow_methods=config.cors_allow_methods,
92
+ allow_headers=config.cors_allow_headers
93
93
  )
94
94
 
95
95
 
96
- def register_files(app: FastAPI) -> None:
96
+ def register_files(app: FastAPI, config: BaseConfig) -> None:
97
97
  """
98
98
  注册文件相关配置
99
99
  """
100
- static_dir = base_config.static_root
100
+ static_dir = config.static_root
101
101
  if not os.path.exists(static_dir):
102
102
  os.mkdir(static_dir)
103
103
  app.mount('/static', StaticFiles(directory=static_dir))
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "rainycode"
7
- version = "1.6.2"
7
+ version = "1.6.3"
8
8
  description = "FastAPI base modules"
9
9
  requires-python = ">=3.10"
10
10
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rainycode
3
- Version: 1.6.2
3
+ Version: 1.6.3
4
4
  Summary: FastAPI base modules
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: aerich==0.9.2
@@ -22,6 +22,7 @@ common_utils/captcha_util.py
22
22
  common_utils/datetime_util.py
23
23
  common_utils/ip_util.py
24
24
  common_utils/jwt_util.py
25
+ common_utils/pagination.py
25
26
  common_utils/snowflake_util.py
26
27
  common_utils/wechat_util.py
27
28
  core/base_config.py
File without changes
File without changes