mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-20 20:39:55 +00:00
style: 移除Python文件中的编码声明并优化代码格式
refactor: 重构前端组件和样式,添加AI助手功能 docs: 更新README文档,添加ruff代码检查说明 feat: 新增AI助手相关API和前端组件 chore: 更新.gitignore文件,添加ruff缓存配置 fix: 修复前端布局和设置相关的问题 perf: 优化代码结构和性能,移除冗余代码 test: 更新测试文件,移除编码声明 build: 更新依赖版本,调整requirements.txt
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import re
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
|
||||
class TimeUtil:
|
||||
"""
|
||||
时间格式化工具类
|
||||
"""
|
||||
|
||||
|
||||
DEFAULT_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
|
||||
|
||||
|
||||
@classmethod
|
||||
def object_format_datetime(cls, obj: Any) -> Any:
|
||||
"""
|
||||
格式化对象中的 datetime 属性为默认字符串格式。
|
||||
|
||||
|
||||
参数:
|
||||
- obj (Any): 输入对象。
|
||||
|
||||
|
||||
返回:
|
||||
- Any: 格式化后的对象。
|
||||
"""
|
||||
@@ -33,10 +33,10 @@ class TimeUtil:
|
||||
def list_format_datetime(cls, lst: list[Any]) -> list[Any]:
|
||||
"""
|
||||
格式化列表内每个对象的 datetime 属性。
|
||||
|
||||
|
||||
参数:
|
||||
- lst (List[Any]): 对象列表。
|
||||
|
||||
|
||||
返回:
|
||||
- list[Any]: 格式化后的对象列表。
|
||||
"""
|
||||
@@ -46,34 +46,34 @@ class TimeUtil:
|
||||
def format_datetime_dict_list(cls, dicts: list[dict]) -> list[dict]:
|
||||
"""
|
||||
递归格式化字典列表中的 datetime 值为默认字符串格式。
|
||||
|
||||
|
||||
参数:
|
||||
- dicts (list[dict]): 字典列表。
|
||||
|
||||
|
||||
返回:
|
||||
- list[dict]: 格式化后的字典列表。
|
||||
"""
|
||||
def _format_value(value: Any) -> Any:
|
||||
if isinstance(value, dict):
|
||||
return {k: _format_value(v) for k, v in value.items()}
|
||||
elif isinstance(value, list):
|
||||
if isinstance(value, list):
|
||||
return [_format_value(item) for item in value]
|
||||
elif isinstance(value, datetime):
|
||||
if isinstance(value, datetime):
|
||||
return value.strftime(cls.DEFAULT_DATETIME_FORMAT)
|
||||
return value
|
||||
|
||||
|
||||
return [_format_value(item) for item in dicts]
|
||||
|
||||
@classmethod
|
||||
def __valid_range(cls, search_str: str, start_range: int, end_range: int) -> bool:
|
||||
"""
|
||||
校验范围字符串是否合法。
|
||||
|
||||
|
||||
参数:
|
||||
- search_str (str): 范围字符串(例如:"1-5")。
|
||||
- start_range (int): 允许的最小范围值。
|
||||
- end_range (int): 允许的最大范围值。
|
||||
|
||||
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
@@ -87,7 +87,7 @@ class TimeUtil:
|
||||
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:
|
||||
"""
|
||||
校验和字符串是否合法。
|
||||
|
||||
|
||||
参数:
|
||||
- search_str (str): 和字符串(例如:"1/5")。
|
||||
- start_range_a (int): 允许的最小范围值A。
|
||||
@@ -95,7 +95,7 @@ class TimeUtil:
|
||||
- end_range_a (int): 允许的最小范围值B。
|
||||
- end_range_b (int): 允许的最大范围值B。
|
||||
- sum_range (int): 允许的最大和值。
|
||||
|
||||
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
@@ -110,7 +110,7 @@ class TimeUtil:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def validate_second_or_minute(cls, second_or_minute: str):
|
||||
def validate_second_or_minute(cls, second_or_minute: str) -> bool:
|
||||
"""
|
||||
校验秒或分钟字段的合法性。
|
||||
|
||||
@@ -120,17 +120,10 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
if (
|
||||
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 True
|
||||
return False
|
||||
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):
|
||||
def validate_hour(cls, hour: str) -> bool:
|
||||
"""
|
||||
校验小时字段的合法性。
|
||||
|
||||
@@ -140,17 +133,10 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
if (
|
||||
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 True
|
||||
return False
|
||||
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):
|
||||
def validate_day(cls, day: str) -> bool:
|
||||
"""
|
||||
校验日期字段的合法性。
|
||||
|
||||
@@ -160,18 +146,10 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
if (
|
||||
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 True
|
||||
return False
|
||||
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):
|
||||
def validate_month(cls, month: str) -> bool:
|
||||
"""
|
||||
校验月份字段的合法性。
|
||||
|
||||
@@ -181,17 +159,10 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
if (
|
||||
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 True
|
||||
return False
|
||||
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):
|
||||
def validate_week(cls, week: str) -> bool:
|
||||
"""
|
||||
校验星期字段的合法性。
|
||||
|
||||
@@ -201,18 +172,10 @@ class TimeUtil:
|
||||
返回:
|
||||
- bool: 校验是否通过。
|
||||
"""
|
||||
if (
|
||||
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 True
|
||||
return False
|
||||
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):
|
||||
def validate_year(cls, year: str) -> bool:
|
||||
"""
|
||||
校验年份字段的合法性。
|
||||
|
||||
@@ -224,19 +187,7 @@ class TimeUtil:
|
||||
"""
|
||||
current_year = int(datetime.now().year)
|
||||
future_years = [current_year + i for i in range(9)]
|
||||
if (
|
||||
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 True
|
||||
return False
|
||||
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