89 lines
3.3 KiB
89 lines
3.3 KiB
from tortoise import fields, models
|
|
from tortoise.indexes import Index
|
|
from tortoise.contrib.pydantic import pydantic_model_creator
|
|
from tortoise.signals import post_save
|
|
from typing import Type
|
|
from helper.tools import genRandomStr
|
|
|
|
class User(models.Model):
|
|
id = fields.UUIDField(pk=True)
|
|
username = fields.CharField(max_length=255, description='账号')
|
|
name = fields.CharField(max_length=255, description='姓名')
|
|
|
|
phone = fields.CharField(null=True, description="手机号", max_length=11)
|
|
email = fields.CharField(max_length=255, null=True, description='邮箱')
|
|
|
|
created_at = fields.DatetimeField(auto_now_add=True)
|
|
updated_at = fields.DatetimeField(auto_now=True)
|
|
|
|
tag = fields.CharField(max_length=255, null=True, description='标签')
|
|
|
|
level = fields.IntField(default=1, description='账号等级')
|
|
is_admin = fields.BooleanField(default=False, description='管理员账号')
|
|
is_staff = fields.BooleanField(default=False, description='员工账号')
|
|
|
|
is_locked = fields.BooleanField(default=False, description='是否锁定')
|
|
is_valid = fields.BooleanField(default=False, description='是否有效')
|
|
is_active = fields.BooleanField(default=False, description='是否删除')
|
|
|
|
class PydanticMeta:
|
|
# Let's exclude the created timestamp
|
|
exclude = ("created_at",)
|
|
|
|
class Meta:
|
|
table = 'users'
|
|
table_description = '用户表'
|
|
|
|
indexes = [
|
|
Index(fields={"username"}, name="usernameidx"),
|
|
Index(fields={"name"}, name="nameidx"),
|
|
Index(fields={"phone"}, name="phoneidx"),
|
|
Index(fields={"email"}, name="emailidx"),
|
|
Index(fields={"is_admin"}, name="is_adminidx"),
|
|
Index(fields={"is_staff"}, name="is_staffidx"),
|
|
Index(fields={"is_locked"}, name="is_lockedidx"),
|
|
Index(fields={"is_valid"}, name="is_valididx"),
|
|
Index(fields={"is_active"}, name="is_activeidx"),
|
|
]
|
|
|
|
def as_json(self):
|
|
return dict(id="{}".format(self.id), username=self.username, name=self.name, is_locked=self.is_locked, phone=self.phone, email=self.email)
|
|
|
|
def __str__(self):
|
|
return f'User(id={self.id} username={self.username}, name={self.name}, is_locked={self.is_locked})'
|
|
|
|
@property
|
|
def nickname(self):
|
|
'''用户昵称'''
|
|
return self.name or self.username
|
|
|
|
User_Pydantic = pydantic_model_creator(User, name="User")
|
|
|
|
# # UserIn_Pydantic = pydantic_model_creator(User, name="UserIn", exclude=("is_active", "is_valid"))
|
|
# UserIn_Pydantic = pydantic_model_creator(User, name="UserIn")
|
|
|
|
# @post_save(User)
|
|
# async def user_post_save(
|
|
# sender: "Type[User]",
|
|
# instance: User,
|
|
# created,
|
|
# using_db,
|
|
# update_fields,
|
|
# ) -> None:
|
|
|
|
# if not instance.email or "null" == instance.email:
|
|
# print("instance.email: ", instance.email)
|
|
# # check duplicated num
|
|
# async def checkNum():
|
|
# email = genRandomStr(4)
|
|
# print("new Email: ", email)
|
|
# if await User.filter(email = email).exists():
|
|
# print("Email exist, ", email)
|
|
# checkNum()
|
|
# else:
|
|
# print("Email not exist, ", email)
|
|
# instance.email = email
|
|
# await instance.save()
|
|
|
|
# await checkNum()
|