class User(UserMixin, BaseModel): """用户model""" username = CharField(unique=True) # 用户名 password = CharField() # 密码 email = CharField(unique=True) # 邮箱 phone = CharField(unique=True) # 电话 status = BooleanField(default=True) # 是否启用状态 admin = BooleanField(default=False) # 是否超级管理员 api_username = CharField(unique=True, null=True) # API用户名 api_password = CharField(null=True) # API密码 create_datetime = DateTimeField(default=datetime.now) # 创建时间 session_token = CharField( unique=True, default=session_token_generate) # 随机session token @property def password_hash(self): """'password'不可读""" raise AttributeError('密码为只读属性') @password_hash.setter def password_hash(self, raw_password): """输出密码散列值""" self.password = generate_password_hash(raw_password) def verify_password(self, raw_password): """检查登录密码是否正确""" return check_password_hash(self.password, raw_password) def verify_api_password(self, api_password): """检查api密码是否正确""" return bool(self.api_password == api_password) @staticmethod def generate_init_password(): """生成用户初始化密码""" raw_password = ''.join( random.sample(string.ascii_letters + string.digits, 10)) return raw_password def is_admin(self): """检查用户是否超级管理员""" return self.admin def is_active(self): """检查用户是否启用状态""" return self.status def is_anonymous(self): """对是否匿名返回‘False’""" return False def get_id(self): return self.session_token
def Migrate(self): migrator = SqliteMigrator(self._db) migrate( migrator.drop_not_null('Contract', 'Account_id'), migrator.add_column('Address', 'IsWatchOnly', BooleanField(default=False)), )
class Notifier(BaseModel): """通知人model""" notify_name = CharField(unique=True) # 通知人姓名 notify_email = CharField(unique=True) # 邮件地址 notify_tel = CharField(unique=True) # 电话号码 status = BooleanField(default=True) # 生效失效标识 create_datetime = DateTimeField(default=datetime.now) # 创建时间
class TaskMonitor(BaseModel): """任务model""" name = CharField(unique=True) # 监控任务名 url = CharField(unique=True) # 监控URL period = CharField() # 循环周期 grace_time = IntegerField(default=0) # 超时时间,以分钟为单位 status = BooleanField(default=True) # 生效失效标识 last_check_time = DateTimeField(null=True) # 上次任务检查时间 next_check_time = DateTimeField(null=True) # 下次任务检查时间 warning = BooleanField(default=False) # 是否处于告警状态 create_datetime = DateTimeField(default=datetime.now) # 创建时间 business = ForeignKeyField(Business, related_name='biz') # 所属业务 @staticmethod def gen_uuid(): """生成监控任务唯一ID""" cronid = str(uuid.uuid1()) return cronid
class _OldUser(BaseModel, UserMixin): class Meta: table_name = 'users' created_by = ForeignKeyField('self', null=True, backref='children', column_name='created_by') role = ForeignKeyField(RoleModel, backref='roles') name = CharField() last_name = CharField() email = CharField(unique=True) password = CharField(null=False) genre = FixedCharField(max_length=1, choices=(( 'm', 'male', ), ('f', 'female')), null=True) birth_date = DateField() active = BooleanField(default=True) created_at = TimestampField(default=None) updated_at = TimestampField() deleted_at = TimestampField(default=None, null=True)
class BusinessNew(BaseModel): """业务model""" business_name = CharField(unique=True) # 业务名 status = BooleanField(default=True) # 生效失效标识
# model定义 class BusinessNew(BaseModel): """业务model""" business_name = CharField(unique=True) # 业务名 status = BooleanField(default=True) # 生效失效标识 @manager.command def create(): """新建表(注意在此处创建的新表需要复制model定义到models.py)""" DB.create_tables([BusinessNew]) # 字段类型定义 status_field = BooleanField(default=False) notifier_id_field = ForeignKeyField(Notifier, to_field=Notifier.id, default=1, null=True) @manager.command def update(): """修改表(注意在此处所做变要同步相关model定义到models.py)""" with DB.transaction(): # 使‘migrate’运行在事务中 migrate( MIGRATOR.add_column('business', 'status999', status_field), # 增加列 MIGRATOR.add_column('business', 'status', status_field), # 增加列 MIGRATOR.add_column('businessnotifier', 'notifier_id', notifier_id_field), # 增加外键列 MIGRATOR.drop_column('business', 'status'), # 删除列 MIGRATOR.rename_column('business', 'status', 'status_mod'), # 重命名列 MIGRATOR.drop_not_null('permission', 'perm_list'), # 允许为空 MIGRATOR.add_not_null('permission', 'perm_list'), # 不允许为空
from database.DatabaseConnector import Infraction connection = MySQLDatabase( Configuration.get_master_var("DATABASE_NAME"), user=Configuration.get_master_var("DATABASE_USER"), password=Configuration.get_master_var("DATABASE_PASS"), host=Configuration.get_master_var("DATABASE_HOST"), port=Configuration.get_master_var("DATABASE_PORT"), use_unicode=True, charset="utf8mb4") #make connection migrator = MySQLMigrator(connection) #run everything in a transaction so we don't turn the database into 💩 if something goes wrong with connection.atomic(): #fields to add end = TimestampField(null=True) active = BooleanField(default=True) #add fields migrate( migrator.add_column("infraction", "end", end), migrator.add_column("infraction", "active", active), migrator.rename_column("infraction", "timestamp", "start"), ) #some infractions are not active anymore Infraction.update( active=False).where((Infraction.type == "Mute") | (Infraction.type == "Kick")).execute()
class Business(BaseModel): """业务model""" business_name = CharField(unique=True) # 业务名 status = BooleanField(default=True) # 生效失效标识 create_datetime = DateTimeField(default=datetime.now) # 创建时间
def add_enabled(): migrator = SqliteMigrator(db) migrate(migrator.add_column("chat", "enabled", BooleanField(default=True)))
from model import Bot, User migrator = SqliteMigrator(appglobals.db) # migrate( # migrator.add_column("bot", "userbot", BooleanField(default=False)) # # migrator.rename_column("transaction", "document", "document_id"), # # migrator.rename_column("document", "user", "user_id"), # ) # # print('Setting all bots to userbot=False.......') # for b in Bot.select(): # b.userbot = False # b.save() try: User.botlist_user_instance().delete_instance() except: pass migrate( # migrator.rename_column("bot", "manybot", "botbuilder"), migrator.add_column("bot", "botbuilder", BooleanField(default=False)) # migrator.rename_column("document", "user", "user_id"), ) print('Setting all bots to botbuilder=False.......') for b in Bot.select(): b.botbuilder = False b.save()