class TortoiseModel(Model): class Meta: abstract = True id: int = IntField(pk=True) created_at: datetime = DatetimeField(auto_now_add=True) updated_at: datetime = DatetimeField(auto_now=True)
class Post(Model): id = IntField(pk=True) created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) title = CharField(max_length=32) content = TextField(null=True) image_url = TextField() author: ForeignKeyRelation[User] = ForeignKeyField("models.User", "posts") comments = ReverseRelation["Comment"] class Meta: table = "posts" ordering = ["created_at"] class PydanticMeta: exclude = ("author", "comments") def __repr__(self): return ( f"Post({self.id=}, {self.created_at=}, {self.modified_at=}, " f"{self.title=}, {self.content=}, {self.image_url}, {self.author=})" ) def __str__(self): return self.__repr__()
class BaseModel(Model): """ This is the base model for all models in the digicube domain. """ id: IntField = IntField(pk=True, description="Primary key") created_at: DatetimeField = DatetimeField(null=True, auto_now_add=True) modified_at: DatetimeField = DatetimeField(null=True, auto_now=True) class Meta: # pylint: disable=too-few-public-methods,missing-docstring abstract = True
class User(Model): id = IntField(pk=True) name = CharField(max_length=255, unique=True) password = TextField(null=True) given_name = TextField(null=True) family_name = TextField(null=True) email = TextField(null=True) realm = TextField() realmuid = TextField(null=True) time_created = DatetimeField(auto_now_add=True) time_updated = DatetimeField(auto_now=True) groups = ManyToManyField('models.Group', related_name='users')
class Reminder(Model): id = IntField(pk=True, generated=True) info = CharField(max_length=255, null=True) added_dt = DatetimeField() expire_dt = DatetimeField() for_message = OneToOneField('models.Message', related_name='reminder', on_delete=CASCADE, null=False) def __str__(self): return f'{self.__class__}: {self.id}'
class ApiJob(Model): """API job model.""" requestor: ForeignKeyRelation[ApiUser] = ForeignKeyField( "models.ApiUser", related_name="jobs") request_time = DatetimeField(auto_now=True) complete_time = DatetimeField(null=True) in_progress = BooleanField(default=False) detail = TextField(null=True) class Meta: """Tortoise ORM Config.""" table = "api_jobs"
class ClanMember(Model): platform_id = IntField() join_date = DatetimeField() is_active = BooleanField(default=True) last_active = DatetimeField(null=True) is_sherpa = BooleanField(default=False) member_type = IntField(null=True, validators=[ClanMemberRankValidator()]) clan: ForeignKeyRelation[Clan] = ForeignKeyField( "seraphsix.Clan", related_name="members", to_field="id" ) member: ForeignKeyRelation[Member] = ForeignKeyField( "seraphsix.Member", related_name="clans", to_field="id" )
class Game(Model): mode_id = IntField() instance_id = BigIntField(unique=True) date = DatetimeField() reference_id = BigIntField(null=True) class Meta: indexes = ("mode_id", "reference_id")
class User(Model): id = IntField(pk=True) created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) username = CharField(max_length=32, unique=True) mail = CharField(max_length=64, null=True) password = CharField(max_length=64) bio = TextField(null=True) posts = ReverseRelation["Post"] comments = ReverseRelation["Comment"] class Meta: table = "users" class PydanticMeta: exclude = ("password", "access_token", "posts", "comments") def __repr__(self): return ( f"User({self.id=}, {self.created_at=}, {self.modified_at=}, " f"{self.username=}, {self.mail=}, {self.password=},{self.bio=})" ) def __str__(self): return self.__repr__() def verify_password(self, password: str) -> bool: return bcrypt.verify(password, self.password) def access_token(self) -> str: data = { "iat": datetime.utcnow(), "exp": datetime.utcnow() + timedelta(minutes=60), "sub": str(self.id), } token: str = jwt.encode( data, getenv("JWT_SECRET"), algorithm="HS256", ) return token
class Mute(Model): id = IntField(pk=True) moderator = BigIntField() reason = TextField(null=True) start = DatetimeField(auto_now_add=True) end = DatetimeField() active = BooleanField(default=True) user: ForeignKeyRelation[User] = ForeignKeyField("models.User", related_name="mutes") guild: ForeignKeyRelation[Guild] = ForeignKeyField("models.Guild", related_name="mutes") class Meta: table = "mutes" def __str__(self): return ( f"<Mute id:{self.id} moderator:{self.moderator} " f"reason:'{self.reason}' start:{self.start} end:{self.end} " f"active:{self.active} user:{self.user.id} guild:{self.guild.id}>")
class Comment(Model): id = IntField(pk=True) created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) content = TextField() author: ForeignKeyRelation[User] = ForeignKeyField("models.User", "comments") post: ForeignKeyRelation[Post] = ForeignKeyField("models.Post", "comments") class Meta: table = "comments" ordering = ["created_at"] class PydanticMeta: exclude = ("author", "post") def __repr__(self): return ( f"Comment({self.id=}, {self.created_at=}, {self.modified_at=}, " f"{self.content=}, {self.author=}, {self.post=})" ) def __str__(self): return self.__repr__()
class Entry(Model): id = IntField(pk=True, index=True) journal: ForeignKeyRelation[Journal] = ForeignKeyField( "models.Journal", related_name="entries", on_delete=CASCADE) short = TextField(null=False) long = TextField(null=False) # YYYY-MM-DD HH:MM format in UTC timezone date = DatetimeField(null=False) # YYYY-MM-DD or None deleted_on = DateField(null=True) keywords: ReverseRelation["Keyword"]
class Event(Model): id = IntField(pk=True, generated=True) name = CharField(max_length=255, null=True) scheduled_dt = DatetimeField() author_message = OneToOneField('models.Message', related_name='user_event', on_delete=CASCADE, null=False) bot_message = OneToOneField('models.Message', related_name='bot_event', on_delete=CASCADE, null=False) def __str__(self): return f'{self.__class__}: {self.id}'
class Warn(Model): id = IntField(pk=True) moderator = BigIntField() reason = TextField(null=True) when = DatetimeField(auto_now_add=True) user: ForeignKeyRelation[User] = ForeignKeyField("models.User", related_name="warns") guild: ForeignKeyRelation[Guild] = ForeignKeyField("models.Guild", related_name="warns") class Meta: table = "warns" def __str__(self): return (f"<Warn id:{self.id} moderator:{self.moderator} " f"reason:'{self.reason}' datetime:{self.when} " f"user:{self.user.id} guild:{self.guild.id}>")
class User(Model): id = BigIntField(pk=True) exp = IntField(default=0) level = IntField(default=0) balance = IntField(default=0) daily_streak = IntField(default=0) last_daily = DatetimeField(null=True) mutes: ReverseRelation["Mute"] warns: ReverseRelation["Warn"] @property def next_daily(self): if not self.last_daily: return None return datetime.fromtimestamp( (self.last_daily + timedelta(hours=23)).timestamp() ) @property def daily_available(self): if not self.last_daily: return True return datetime.utcnow().timestamp() > self.next_daily.timestamp() @property def daily_streak_expired(self): if not self.last_daily: return None return ( datetime.utcnow().timestamp() > (self.last_daily + timedelta(days=2)).timestamp() ) class Meta: table = "users" def __str__(self): return ( f"<User id:{self.id} exp:{self.exp} " f"level:{self.level} bal:{self.balance}>" )
class User(BaseModel): """User Model""" FIRST_NAME_LENGHT = 20 LOGIN_LENGHT = 20 LAST_NAME_LENGHT = 20 EMAIL_LENGHT = 60 login = CharField(LOGIN_LENGHT, unique=True, description="The login name of the user.") first_name = CharField(FIRST_NAME_LENGHT, null=True) last_name = CharField(LAST_NAME_LENGHT, null=True) email = CharField(EMAIL_LENGHT, null=True) is_active = BooleanField(null=True, default=False) is_verified = BooleanField(null=True, default=False) verified_at = DatetimeField(null=True) password_hash = CharField(256, null=True) last_login_at = DatetimeField(null=True) roles = ManyToManyField("model.Role", related_name="users", through="user_roles") class Meta: # pylint: disable=too-few-public-methods # pylint: disable=missing-docstring table = "user" ordering = ["login", "last_name", "first_name"] class PydanticMeta: include = ( "id", "login", "last_login_at", "first_name", "last_name", "verified_at", "created_at", ) def __str__(self): return "User" @property def password(self): """ Reading the password is forbidden. """ raise EnvironmentError() @property def is_password_set(self): """ Checks, if the password has been set. """ return self.password_hash is not None @password.setter def password(self, password): """Hash a password for storing.""" self.password_hash = generate_password_hash(password) def verify_password(self, password: str) -> None: """ Generate a hashed password and compere it with the stored password hash. """ if not self.is_password_set: return False return check_password_hash(self.password_hash, password)
class ObjectRecognitionEntity(Model): uid = TextField(pk=True) predictions = JSONField() catalog_id = TextField() event_id = TextField() created_time = DatetimeField()
class MeasurementModel(Model): mac = CharField(pk=True, max_length=17, description="MAC address as primary key") weight = FloatField(null=True) temperature_inside = FloatField(null=True) temperature_outside = FloatField(null=True) timestamp = DatetimeField(auto_now=True)