mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-20 20:39:55 +00:00
style: 统一代码格式和字符串引号使用
refactor: 优化代码结构和可读性 feat: 添加http_limit模块实现请求限制功能 fix: 修复异步任务中使用time.sleep的问题 chore: 更新依赖项并添加pytest测试框架 docs: 更新项目描述信息 perf: 优化Redis序列化方式使用JSON替代pickle test: 添加测试相关配置和依赖
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import re
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
@@ -9,7 +8,7 @@ class TimeUtil:
|
||||
时间格式化工具类
|
||||
"""
|
||||
|
||||
DEFAULT_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
|
||||
DEFAULT_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
@classmethod
|
||||
def object_format_datetime(cls, obj: Any) -> Any:
|
||||
@@ -23,7 +22,7 @@ class TimeUtil:
|
||||
- Any: 格式化后的对象。
|
||||
"""
|
||||
for attr in dir(obj):
|
||||
if not attr.startswith('_'): # 跳过私有属性
|
||||
if not attr.startswith("_"): # 跳过私有属性
|
||||
value = getattr(obj, attr)
|
||||
if isinstance(value, datetime):
|
||||
setattr(obj, attr, value.strftime(cls.DEFAULT_DATETIME_FORMAT))
|
||||
@@ -53,6 +52,7 @@ class TimeUtil:
|
||||
返回:
|
||||
- list[dict]: 格式化后的字典列表。
|
||||
"""
|
||||
|
||||
def _format_value(value: Any) -> Any:
|
||||
if isinstance(value, dict):
|
||||
return {k: _format_value(v) for k, v in value.items()}
|
||||
@@ -77,14 +77,22 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
match = re.match(r'^(\d+)-(\d+)$', search_str)
|
||||
match = re.match(r"^(\d+)-(\d+)$", search_str)
|
||||
if match:
|
||||
start, end = int(match.group(1)), int(match.group(2))
|
||||
return start_range <= start < end <= end_range
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def __valid_sum(cls, search_str: str, start_range_a: int, start_range_b: int, end_range_a: int, end_range_b: int, sum_range: int) -> bool:
|
||||
def __valid_sum(
|
||||
cls,
|
||||
search_str: str,
|
||||
start_range_a: int,
|
||||
start_range_b: int,
|
||||
end_range_a: int,
|
||||
end_range_b: int,
|
||||
sum_range: int,
|
||||
) -> bool:
|
||||
"""
|
||||
校验和字符串是否合法。
|
||||
|
||||
@@ -99,7 +107,7 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
match = re.match(r'^(\d+)/(\d+)$', search_str)
|
||||
match = re.match(r"^(\d+)/(\d+)$", search_str)
|
||||
if match:
|
||||
start, end = int(match.group(1)), int(match.group(2))
|
||||
return (
|
||||
@@ -120,7 +128,12 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
return bool(second_or_minute == '*' or ('-' in second_or_minute and cls.__valid_range(second_or_minute, 0, 59)) or ('/' in second_or_minute and cls.__valid_sum(second_or_minute, 0, 58, 1, 59, 59)) or re.match(r'^(?:[0-5]?\d|59)(?:,[0-5]?\d|59)*$', second_or_minute))
|
||||
return bool(
|
||||
second_or_minute == "*"
|
||||
or ("-" in second_or_minute and cls.__valid_range(second_or_minute, 0, 59))
|
||||
or ("/" in second_or_minute and cls.__valid_sum(second_or_minute, 0, 58, 1, 59, 59))
|
||||
or re.match(r"^(?:[0-5]?\d|59)(?:,[0-5]?\d|59)*$", second_or_minute)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def validate_hour(cls, hour: str) -> bool:
|
||||
@@ -133,7 +146,12 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
return bool(hour == '*' or ('-' in hour and cls.__valid_range(hour, 0, 23)) or ('/' in hour and cls.__valid_sum(hour, 0, 22, 1, 23, 23)) or re.match(r'^(?:0|[1-9]|1\d|2[0-3])(?:,(?:0|[1-9]|1\d|2[0-3]))*$', hour))
|
||||
return bool(
|
||||
hour == "*"
|
||||
or ("-" in hour and cls.__valid_range(hour, 0, 23))
|
||||
or ("/" in hour and cls.__valid_sum(hour, 0, 22, 1, 23, 23))
|
||||
or re.match(r"^(?:0|[1-9]|1\d|2[0-3])(?:,(?:0|[1-9]|1\d|2[0-3]))*$", hour)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def validate_day(cls, day: str) -> bool:
|
||||
@@ -146,7 +164,16 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
return bool(day in ['*', '?', 'L'] or ('-' in day and cls.__valid_range(day, 1, 31)) or ('/' in day and cls.__valid_sum(day, 1, 30, 1, 30, 31)) or ('W' in day and re.match(r'^(?:[1-9]|1\d|2\d|3[01])W$', day)) or re.match(r'^(?:0|[1-9]|1\d|2[0-9]|3[0-1])(?:,(?:0|[1-9]|1\d|2[0-9]|3[0-1]))*$', day))
|
||||
return bool(
|
||||
day in ["*", "?", "L"]
|
||||
or ("-" in day and cls.__valid_range(day, 1, 31))
|
||||
or ("/" in day and cls.__valid_sum(day, 1, 30, 1, 30, 31))
|
||||
or ("W" in day and re.match(r"^(?:[1-9]|1\d|2\d|3[01])W$", day))
|
||||
or re.match(
|
||||
r"^(?:0|[1-9]|1\d|2[0-9]|3[0-1])(?:,(?:0|[1-9]|1\d|2[0-9]|3[0-1]))*$",
|
||||
day,
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def validate_month(cls, month: str) -> bool:
|
||||
@@ -159,7 +186,12 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
return bool(month == '*' or ('-' in month and cls.__valid_range(month, 1, 12)) or ('/' in month and cls.__valid_sum(month, 1, 11, 1, 11, 12)) or re.match(r'^(?:0|[1-9]|1[0-2])(?:,(?:0|[1-9]|1[0-2]))*$', month))
|
||||
return bool(
|
||||
month == "*"
|
||||
or ("-" in month and cls.__valid_range(month, 1, 12))
|
||||
or ("/" in month and cls.__valid_sum(month, 1, 11, 1, 11, 12))
|
||||
or re.match(r"^(?:0|[1-9]|1[0-2])(?:,(?:0|[1-9]|1[0-2]))*$", month)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def validate_week(cls, week: str) -> bool:
|
||||
@@ -172,7 +204,13 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
return bool(week in ['*', '?'] or ('-' in week and cls.__valid_range(week, 1, 7)) or ('#' in week and re.match(r'^[1-7]#[1-4]$', week)) or ('L' in week and re.match(r'^[1-7]L$', week)) or re.match(r'^[1-7](?:(,[1-7]))*$', week))
|
||||
return bool(
|
||||
week in ["*", "?"]
|
||||
or ("-" in week and cls.__valid_range(week, 1, 7))
|
||||
or ("#" in week and re.match(r"^[1-7]#[1-4]$", week))
|
||||
or ("L" in week and re.match(r"^[1-7]L$", week))
|
||||
or re.match(r"^[1-7](?:(,[1-7]))*$", week)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def validate_year(cls, year: str) -> bool:
|
||||
@@ -187,7 +225,23 @@ class TimeUtil:
|
||||
"""
|
||||
current_year = int(datetime.now().year)
|
||||
future_years = [current_year + i for i in range(9)]
|
||||
return bool(year == '*' or ('-' in year and cls.__valid_range(year, current_year, 2099)) or ('/' in year and cls.__valid_sum(year, current_year, 2098, 1, 2099 - current_year, 2099)) or ('#' in year and re.match(r'^[1-7]#[1-4]$', year)) or ('L' in year and re.match(r'^[1-7]L$', year)) or ((len(year) == 4 or ',' in year) and all(int(item) in future_years and current_year <= int(item) <= 2099 for item in year.split(','))))
|
||||
return bool(
|
||||
year == "*"
|
||||
or ("-" in year and cls.__valid_range(year, current_year, 2099))
|
||||
or (
|
||||
"/" in year
|
||||
and cls.__valid_sum(year, current_year, 2098, 1, 2099 - current_year, 2099)
|
||||
)
|
||||
or ("#" in year and re.match(r"^[1-7]#[1-4]$", year))
|
||||
or ("L" in year and re.match(r"^[1-7]L$", year))
|
||||
or (
|
||||
(len(year) == 4 or "," in year)
|
||||
and all(
|
||||
int(item) in future_years and current_year <= int(item) <= 2099
|
||||
for item in year.split(",")
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def validate_cron_expression(cls, cron_expression: str):
|
||||
|
||||
Reference in New Issue
Block a user