forked from erp-dev/erp
test: before all tests
This commit is contained in:
@@ -205,26 +205,41 @@ DATABASES = {
|
||||
# https://docs.djangoproject.com/en/5.2/topics/cache/
|
||||
# Django 5.x 内置 Redis 缓存后端,使用 redis 包(已安装)
|
||||
# 容器内默认使用 redis://redis:6379/0,本地开发可使用 redis://localhost:6379/0
|
||||
CACHES = {
|
||||
'default': {
|
||||
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
|
||||
'LOCATION': env(
|
||||
'CACHE_URL',
|
||||
default='redis://redis:6379/0', # 容器内默认地址
|
||||
),
|
||||
'OPTIONS': {
|
||||
# Redis 连接选项
|
||||
'socket_connect_timeout': 5, # 连接超时(秒)
|
||||
'socket_timeout': 5, # 操作超时(秒)
|
||||
'retry_on_timeout': True, # 超时后重试
|
||||
'health_check_interval': 30, # 健康检查间隔(秒)
|
||||
},
|
||||
# 键前缀,避免不同项目/环境之间的键冲突
|
||||
'KEY_PREFIX': env('CACHE_KEY_PREFIX', default='flower'),
|
||||
# 默认过期时间(秒),None 表示永不过期(由具体使用场景决定)
|
||||
'TIMEOUT': env.int('CACHE_DEFAULT_TIMEOUT', default=300), # 默认 5 分钟
|
||||
|
||||
# 检测是否在测试环境中
|
||||
import sys
|
||||
TESTING = 'test' in sys.argv
|
||||
|
||||
if TESTING:
|
||||
# 测试环境使用内存缓存,避免 Redis 连接问题
|
||||
CACHES = {
|
||||
'default': {
|
||||
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
|
||||
'LOCATION': 'unique-testing-cache',
|
||||
}
|
||||
}
|
||||
else:
|
||||
# 生产/开发环境使用 Redis
|
||||
CACHES = {
|
||||
'default': {
|
||||
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
|
||||
'LOCATION': env(
|
||||
'CACHE_URL',
|
||||
default='redis://redis:6379/0', # 容器内默认地址
|
||||
),
|
||||
'OPTIONS': {
|
||||
# Redis 连接选项
|
||||
'socket_connect_timeout': 5, # 连接超时(秒)
|
||||
'socket_timeout': 5, # 操作超时(秒)
|
||||
'retry_on_timeout': True, # 超时后重试
|
||||
'health_check_interval': 30, # 健康检查间隔(秒)
|
||||
},
|
||||
# 键前缀,避免不同项目/环境之间的键冲突
|
||||
'KEY_PREFIX': env('CACHE_KEY_PREFIX', default='flower'),
|
||||
# 默认过期时间(秒),None 表示永不过期(由具体使用场景决定)
|
||||
'TIMEOUT': env.int('CACHE_DEFAULT_TIMEOUT', default=300), # 默认 5 分钟
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
|
||||
49
flower/viewsets.py
Normal file
49
flower/viewsets.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
自定义 ViewSet 基类和分页类
|
||||
|
||||
提供统一的分页限制,防止前端无限制请求大量数据。
|
||||
"""
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.pagination import LimitOffsetPagination
|
||||
|
||||
|
||||
class LimitedLimitOffsetPagination(LimitOffsetPagination):
|
||||
"""
|
||||
带最大限制的 LimitOffsetPagination
|
||||
|
||||
- 默认每页返回 100 条
|
||||
- 最大每页返回 100 条
|
||||
- 防止前端请求过多数据
|
||||
"""
|
||||
default_limit = 100
|
||||
max_limit = 100
|
||||
|
||||
|
||||
class LimitedModelViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
带分页限制的 ModelViewSet 基类
|
||||
|
||||
自动应用 LimitedLimitOffsetPagination,限制每次最多返回 100 条数据。
|
||||
所有项目中的 ViewSet 应继承此类以保持一致的分页行为。
|
||||
"""
|
||||
pagination_class = LimitedLimitOffsetPagination
|
||||
|
||||
|
||||
class LimitedReadOnlyModelViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
"""
|
||||
带分页限制的 ReadOnlyModelViewSet 基类
|
||||
|
||||
自动应用 LimitedLimitOffsetPagination,限制每次最多返回 100 条数据。
|
||||
用于只读场景。
|
||||
"""
|
||||
pagination_class = LimitedLimitOffsetPagination
|
||||
|
||||
|
||||
class LimitedGenericViewSet(viewsets.GenericViewSet):
|
||||
"""
|
||||
带分页限制的 GenericViewSet 基类
|
||||
|
||||
自动应用 LimitedLimitOffsetPagination,限制每次最多返回 100 条数据。
|
||||
用于需要自定义 actions 的场景。
|
||||
"""
|
||||
pagination_class = LimitedLimitOffsetPagination
|
||||
Reference in New Issue
Block a user