Update
This commit is contained in:
parent
e207bb9cd3
commit
e730820409
@ -2,7 +2,7 @@ from tortoise import fields
|
|||||||
|
|
||||||
from app.schemas.menus import MenuType
|
from app.schemas.menus import MenuType
|
||||||
|
|
||||||
from .base import BaseModel, CustomDatetimeField, TimestampMixin
|
from .base import BaseModel, TimestampMixin
|
||||||
from .enums import MethodType
|
from .enums import MethodType
|
||||||
|
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ class User(BaseModel, TimestampMixin):
|
|||||||
password = fields.CharField(max_length=128, null=True, description="密码")
|
password = fields.CharField(max_length=128, null=True, description="密码")
|
||||||
is_active = fields.BooleanField(default=True, description="是否激活")
|
is_active = fields.BooleanField(default=True, description="是否激活")
|
||||||
is_superuser = fields.BooleanField(default=False, description="是否为超级管理员")
|
is_superuser = fields.BooleanField(default=False, description="是否为超级管理员")
|
||||||
last_login = CustomDatetimeField(null=True, description="最后登录时间")
|
last_login = fields.DatetimeField(null=True, description="最后登录时间")
|
||||||
roles = fields.ManyToManyField("models.Role", related_name="user_roles")
|
roles = fields.ManyToManyField("models.Role", related_name="user_roles")
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
@ -1,16 +1,8 @@
|
|||||||
import datetime
|
from datetime import datetime
|
||||||
import functools
|
|
||||||
from typing import Any, Optional
|
|
||||||
|
|
||||||
from tortoise import fields, models, timezone
|
from tortoise import fields, models
|
||||||
|
|
||||||
try:
|
from app.settings import settings
|
||||||
from ciso8601 import parse_datetime
|
|
||||||
except ImportError: # pragma: nocoverage
|
|
||||||
from iso8601 import parse_date
|
|
||||||
|
|
||||||
parse_datetime = functools.partial(parse_date, default_timezone=None)
|
|
||||||
from tortoise.timezone import get_timezone, localtime
|
|
||||||
|
|
||||||
|
|
||||||
class BaseModel(models.Model):
|
class BaseModel(models.Model):
|
||||||
@ -19,10 +11,17 @@ class BaseModel(models.Model):
|
|||||||
async def to_dict(self, m2m=False):
|
async def to_dict(self, m2m=False):
|
||||||
d = {}
|
d = {}
|
||||||
for field in self._meta.db_fields:
|
for field in self._meta.db_fields:
|
||||||
d[field] = getattr(self, field)
|
value = getattr(self, field)
|
||||||
|
if isinstance(value, datetime):
|
||||||
|
value = value.strftime(settings.DATETIME_FORMAT)
|
||||||
|
d[field] = value
|
||||||
if m2m:
|
if m2m:
|
||||||
for field in self._meta.m2m_fields:
|
for field in self._meta.m2m_fields:
|
||||||
values = await getattr(self, field).all().values()
|
values = [value for value in await getattr(self, field).all().values()]
|
||||||
|
for value in values:
|
||||||
|
value.update(
|
||||||
|
(k, v.strftime(settings.DATETIME_FORMAT)) for k, v in value.items() if isinstance(v, datetime)
|
||||||
|
)
|
||||||
d[field] = values
|
d[field] = values
|
||||||
return d
|
return d
|
||||||
|
|
||||||
@ -34,28 +33,6 @@ class UUIDModel:
|
|||||||
uuid = fields.UUIDField(unique=True, pk=False)
|
uuid = fields.UUIDField(unique=True, pk=False)
|
||||||
|
|
||||||
|
|
||||||
class CustomDatetimeField(fields.DatetimeField):
|
|
||||||
def to_python_value(self, value: Any) -> Optional[datetime.datetime]:
|
|
||||||
if value is None:
|
|
||||||
value = None
|
|
||||||
else:
|
|
||||||
if isinstance(value, datetime.datetime):
|
|
||||||
value = value.strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
elif isinstance(value, int):
|
|
||||||
value = datetime.datetime.fromtimestamp(value)
|
|
||||||
value = value.strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
else:
|
|
||||||
value = parse_datetime(value)
|
|
||||||
if timezone.is_naive(value):
|
|
||||||
value = timezone.make_aware(value, get_timezone())
|
|
||||||
value = value.strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
else:
|
|
||||||
value = localtime(value)
|
|
||||||
value = value.strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
self.validate(value)
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
class TimestampMixin:
|
class TimestampMixin:
|
||||||
created_at = CustomDatetimeField(auto_now_add=True)
|
created_at = fields.DatetimeField(auto_now_add=True)
|
||||||
updated_at = CustomDatetimeField(auto_now=True)
|
updated_at = fields.DatetimeField(auto_now=True)
|
||||||
|
|||||||
@ -53,6 +53,7 @@ class Settings(BaseSettings):
|
|||||||
"use_tz": False,
|
"use_tz": False,
|
||||||
"timezone": "Asia/Shanghai",
|
"timezone": "Asia/Shanghai",
|
||||||
}
|
}
|
||||||
|
DATETIME_FORMAT: str = "%Y-%m-%d %H:%M:%S"
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user