class Project(Model): id = fields.IntField(pk=True) name = fields.CharField(max_length=128) description = fields.TextField() link = fields.CharField(max_length=64) members = fields.ManyToManyField('models.Member', related_name='projects') leaders = fields.ManyToManyField('models.Leader', related_name='projects') organization = fields.ForeignKeyField('models.Organization', related_name='projects') tags: fields.ManyToManyRelation['Tag']
class TheraChannel(Model): guild_id = fields.BigIntField(pk=True) channel_id = fields.BigIntField(null=False) systems = fields.ManyToManyField('models.TheraEveSystem', related_name='channels') constellations = fields.ManyToManyField('models.TheraEveConstellation', related_name='channels') regions = fields.ManyToManyField('models.TheraEveRegion', related_name='channels') def __str__(self): return f'Thera Channel for <{self.guild_id}>'
class Organization(Model): id = fields.IntField(pk=True) verified = fields.BooleanField(default=False) name = fields.CharField(max_length=128) description = fields.TextField() link = fields.CharField(max_length=64) members = fields.ManyToManyField('models.User', related_name='organizations') leaders = fields.ManyToManyField('models.Leader', related_name='organizations') projects: fields.ReverseRelation['Project']
class Course(models.Model): """The course model.""" id = fields.UUIDField(pk=True) title = fields.CharField(max_length=100) overview = fields.TextField() cover = fields.CharField(null=True, max_length=500) video = fields.CharField(null=True, max_length=500) categories = fields.ManyToManyField("models.Category", related_name="courses") languages = fields.ManyToManyField("models.Language", related_name="courses") level = fields.CharEnumField(enum_type=Level, max_length=100) teacher = fields.ForeignKeyField( model_name="models.Teacher", related_name="courses", on_delete=fields.SET_NULL, null=True, ) price = fields.FloatField(default=0) discount = fields.FloatField(default=0) is_drift = fields.BooleanField(default=True) is_active = fields.BooleanField(default=True) slug = fields.CharField(unique=True, max_length=200) created_at = fields.DatetimeField(auto_now_add=True) updated_at = fields.DatetimeField(auto_now=True) class Meta: """Meta data.""" table = "course" def __str__(self: "Course") -> str: """The string representative for course class.""" return f"{self.title}"
class Session(models.Model): id = fields.BigIntField(pk=True) tutor = fields.ForeignKeyField("models.User", related_name="tutor_sessions") students: fields.ManyToManyRelation["Category"] = fields.ManyToManyField( "models.User", related_name="student_sessions", through="student_session", backward_key="session_id", ) start_time = fields.DatetimeField(null=True) event_id = fields.CharField(max_length=255) def tutor_id(self) -> int: return self.tutor.id def student_ids(self) -> List[int]: try: return [student.id for student in self.students] except NoValuesFetched: return [] class PydanticMeta: exclude = ["tutor", "students"] computed = ("tutor_id", "student_ids")
class Poll(tortoise.Model): id = fields.BigIntField(pk=True) channel_id = fields.BigIntField() message_id = fields.BigIntField() author_id = fields.BigIntField() content = fields.JSONField() is_anonymous = fields.BooleanField() available_choices = fields.IntField() # noinspection PyUnresolvedReferences choices: fields.ManyToManyRelation[ "Response"] = fields.ManyToManyField( # type: ignore "models.Response", related_name="choices") class Meta: table = "polls" def __str__(self): return (f"<Poll id={self.id} " f"channel_id={self.channel_id} " f"message_id={self.message_id} " f"author_id={self.author_id} " f"content={self.content} " f"is_anonymous={self.is_anonymous} " f"available_choices={self.available_choices} " f"choices={self.choices}>") __repr__ = __str__
class User(Model): id = fields.IntField(pk=True) name = fields.CharField(max_length=50, null=False) username = fields.CharField(max_length=20, unique=True, null=False) password = fields.CharField(max_length=100, null=False) created_at = fields.DatetimeField(auto_now_add=True) quizzes: fields.ReverseRelation["Quiz"] answers: fields.ManyToManyRelation["QuizQuestion"] = fields.ManyToManyField( model_name="models.QuizQuestion", related_name="respondents", through="answers", backward_key="respondent_id", forward_key="quiz_question_id", ) async def save(self, *args, **kwargs) -> None: self.password = get_password_hash(self.password) await super().save(*args, **kwargs) class Meta: table = "users" ordering = ["created_at"] class PydanticMeta: exclude = ("created_at",)
class Quiz(Model): id = fields.IntField(pk=True) title = fields.CharField(max_length=50, null=False) description = fields.CharField(max_length=200, null=True) created_at = fields.DatetimeField(auto_now_add=True) questions: fields.ManyToManyRelation["Question"] = fields.ManyToManyField( model_name="models.Question", related_name="quizzes", through="quiz_questions", backward_key="quiz_id", forward_key="question_id", ) creator: fields.ForeignKeyRelation["User"] = fields.ForeignKeyField( model_name="models.User", related_name="quizzes" ) def created_date(self) -> str: """ Formats `created_date` in a more readable way """ return f"{self.created_at:%d/%m/%Y}" class Meta: table = "quizzes" computed = ["created_date"] class PydanticMeta: computed = ["created_date"] exclude = ("created_at", "quiz_questionss", "creator",)
class Facility(models.Model): id = fields.IntField(pk=True) name: str = fields.CharField(max_length=255, null=True) profile_pic: str = fields.CharField(max_length=255, null=True) category: str = fields.CharField(max_length=50, null=True) city = fields.CharField(max_length=255) street = fields.CharField(max_length=255) street2 = fields.CharField(max_length=255) postcode = fields.IntField() class Meta: table = 'facilities' booking: fields.ReverseRelation["Booking"] amenities: fields.ManyToManyRelation['Amenities'] = fields.ManyToManyField( "models.Amenities", related_name="facilities", on_delete=fields.CASCADE) events: fields.ForeignKeyNullableRelation["Event"] user: fields.ForeignKeyRelation[ BusinessDetailModel] = fields.ForeignKeyField( "models.BusinessDetailModel", related_name="facilities", on_delete=fields.CASCADE) def __str__(self): return self.name
class Event(Model): id = fields.BigIntField(pk=True, description="Event ID") name = fields.TextField() tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKeyField( "models.Tournament", db_constraint=False, related_name="events", description="FK to tournament", ) participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField( "models.Team", db_constraint=False, related_name="events", through="teamevents", description="How participants relate", ) modified = fields.DatetimeField(auto_now=True) prize = fields.DecimalField(max_digits=10, decimal_places=2, null=True) token = fields.CharField(max_length=100, description="Unique token", unique=True) key = fields.CharField(max_length=100) class Meta: table_description = "This table contains a list of all the events" unique_together = [("name", "prize"), ["tournament", "key"]]
class User(Base): name = fields.CharField(max_length=255) email = fields.CharField(max_length=255) roles = fields.ManyToManyField('models.Role', related_name='users', through='user_role')
class Season(Base): season_combos: fields.ReverseRelation["SeasonCombo"] iracing_id = fields.CharField(max_length=30, unique=True) series = fields.ForeignKeyField('models.Series', related_name='seasons') cars: fields.ManyToManyRelation["Car"] = fields.ManyToManyField( "models.Car", related_name="seasons", through="season_cars") minimum_team_drivers = fields.IntField(default=1) start_time = fields.DatetimeField() end_time = fields.DatetimeField() season_quarter = fields.IntField(null=True) season_year = fields.IntField(null=True) is_fixed = fields.BooleanField(null=True) is_official = fields.BooleanField(null=True) active = fields.BooleanField(null=True) def current_week(self): current_datetime = datetime.now() utc_time = current_datetime.replace(tzinfo=timezone.utc) return (utc_time - self.start_time).days // 7 async def current_combo(self): return await SeasonCombo.get(season=self, race_week=self.current_week()) async def offset_combo(self, offset): """offset is how far off the current week we want a combo for. so 1 would be 1 week after current, -1 would be the week before current""" try: return await SeasonCombo.get(season=self, race_week=(self.current_week() + offset)) except: return None
class SourceFields(Model): id = fields.IntField(pk=True, source_field="sometable_id", description="Da PK") chars = fields.CharField(max_length=50, source_field="some_chars_table", index=True, description="Some chars") blip = fields.CharField(max_length=50, default="BLIP", source_field="da_blip") fk = fields.ForeignKeyField( "models.SourceFields", related_name="fkrev", null=True, source_field="fk_sometable", description="Tree!", ) rel_to = fields.ManyToManyField( "models.SourceFields", related_name="rel_from", through="sometable_self", forward_key="sts_forward", backward_key="backward_sts", description="M2M to myself", ) class Meta: table = "sometable" unique_together = [["chars", "blip"]] table_description = "Source mapped fields"
class Event(Model): """ Event class instance """ id = fields.IntField(pk=True) name = fields.CharField(max_length=255) event_date = fields.DatetimeField() event_last_updated = fields.DatetimeField(auto_now=True) meetup = fields.ForeignKeyField('models.MeetUp', related_name='events') participants = fields.ManyToManyField('models.Group', related_name='events', through='event_group') class Meta: table = 'event' def __str__(self): if self.name and self.event_date: return f"{self.event_name} {self.event_date}" return self.id def get_participants(self): if self.participants is not None: return [self.participants]
class Import(Model): id = fields.BigIntField(pk=True) created_at = fields.DatetimeField(auto_now_add=True) updated_at = fields.DatetimeField(auto_add=True, null=True) package = fields.ForeignKeyField("distrobuild.Package", on_delete="RESTRICT", related_name="imports") status = fields.CharEnumField(ImportStatus) module = fields.BooleanField(default=False) version = fields.IntField() executor_username = fields.CharField(max_length=255) commits: fields.ManyToManyRelation[ "ImportCommit"] = fields.ManyToManyField("distrobuild.ImportCommit", related_name="imports", forward_key="id", backward_key="import__id", through="import_commits") class Meta: table = "imports" ordering = ["-created_at"] class PydanticMeta: exclude = ("batch_imports", )
class Event(Model): """ Events on the calendar """ event_id = fields.BigIntField(pk=True) #: The name name = fields.TextField() #: What tournaments is a happenin' tournament: fields.ForeignKeyRelation[ "Tournament"] = fields.ForeignKeyField("models.Tournament", related_name="events") reporter: fields.ForeignKeyNullableRelation[ Reporter] = fields.ForeignKeyField("models.Reporter", null=True) participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField( "models.Team", related_name="events", through="event_team", backward_key="idEvent") modified = fields.DatetimeField(auto_now=True) token = fields.TextField(default=generate_token) alias = fields.IntField(null=True) class Meta: ordering = ["name"] def __str__(self): return self.name
class CODPlayer(models.Model): ''' This defines a Player from the COD API ''' id = fields.IntField(pk=True) api_id = fields.CharField(max_length=255, null=True) player_name = fields.CharField(max_length=255) platform_url = fields.CharField(max_length=255, null=True) titles = fields.ManyToManyField('api.CODTitle', through='codplayertitles') platforms = fields.ManyToManyField('api.CODPlatform', through='codplayerplatforms') api_url = fields.CharField(max_length=255, null=True) def __str__(self) -> str: return self.api_id
class StraightFields(Model): eyedee = fields.IntField(pk=True, description="Da PK") chars = fields.CharField(max_length=50, index=True, description="Some chars") blip = fields.CharField(max_length=50, default="BLIP") nullable = fields.CharField(max_length=50, null=True) fk: fields.ForeignKeyNullableRelation[ "StraightFields"] = fields.ForeignKeyField("models.StraightFields", related_name="fkrev", null=True, description="Tree!") fkrev: fields.ReverseRelation["StraightFields"] o2o: fields.OneToOneNullableRelation[ "StraightFields"] = fields.OneToOneField("models.StraightFields", related_name="o2o_rev", null=True, description="Line") o2o_rev: fields.Field rel_to: fields.ManyToManyRelation[ "StraightFields"] = fields.ManyToManyField("models.StraightFields", related_name="rel_from", description="M2M to myself") rel_from: fields.ManyToManyRelation["StraightFields"] class Meta: unique_together = [["chars", "blip"]] table_description = "Straight auto-mapped fields"
class GroupModel(Model): class Meta: table = 'group' id = fields.IntField(pk=True) telegram_id = fields.BigIntField(unique=True, index=True) tracked_users = fields.ManyToManyField('models.TimusUserModel', related_name='tracked_in') leaderboard_message_id = fields.IntField(null=True, default=None)
class Post(models.Model): """ Article class """ author: fields.ForeignKeyRelation['User'] = fields.ForeignKeyField( 'models.User', related_name='posts', on_delete=fields.CASCADE) tag: fields.ManyToManyRelation["Tag"] = fields.ManyToManyField( 'models.Tag', through='post_tag', null=True, related_name='posts') category: fields.ForeignKeyNullableRelation[ BlogCategory] = fields.ForeignKeyField('models.BlogCategory', null=True, related_name='posts', on_delete=fields.SET_NULL) title = fields.CharField(max_length=500) mini_text = fields.TextField(max_length=5000) text = fields.TextField() create_at = fields.DatetimeField(auto_now_add=True) publish_at = fields.DatetimeField(auto_now=True) image = fields.CharField(max_length=500, null=True) # TODO published переименовать на is_published published = fields.BooleanField(default=True) viewed = fields.IntField(default=0) description = fields.TextField(max_length=300) # TODO убрать лишние связи, чтобы не тянуть лишнюю инфу из БД comments: fields.ReverseRelation["Comment"] def __str__(self): return self.title class PydanticMeta: exclude = ('comments', )
class Users(Model): id = fields.IntField(pk=True) user_oauth_token = fields.CharField(255) score = fields.IntField() quizzes = fields.ManyToManyField('models.Quizzes', related_name='users') def __str__(self): return f"User {self.id}: {self.user_oauth_token}"
class Guild(Base): discord_id = fields.CharField(max_length=30, unique=True) favorite_series: fields.ManyToManyRelation[ "Series"] = fields.ManyToManyField("models.Series", related_name="guilds", through="favorite_series") leagues: fields.ManyToManyRelation["League"] drivers: fields.ManyToManyRelation[Driver]
class Tag(Model): id = fields.IntField(pk=True) name = fields.CharField(max_length=64) projects = fields.ManyToManyField('models.Project', related_name='tags') users: fields.ManyToManyRelation['User'] class PydanticMeta: exclude = ['id', 'users']
class Recipe(Model): id = fields.IntField(pk=True) name = fields.TextField() desc = fields.TextField() ingredient = fields.ManyToManyField('models.Ingredient', related_name='ingredients') def __str__(self): return f"{self.name}:{self.desc[:30]}"
class Journal(Model): timestamp = fields.DatetimeField(auto_now_add=True) level = fields.SmallIntField(index=True) text = fields.CharField(max_length=255, index=True) parent = fields.ForeignKeyField("models.Journal", related_name="children", null=True) related = fields.ManyToManyField("models.Journal", related_name="related_from")
class UUIDM2MRelatedSourceModel(Model): id = fields.UUIDField(pk=True, source_field="e") value = fields.TextField(default="test", source_field="f") models = fields.ManyToManyField( "models.UUIDPkSourceModel", related_name="peers", forward_key="e", backward_key="h" ) class Meta: table = "umrsm"
class Team(Model): name = fields.CharField(max_length=50, pk=True, description="The TEAM name (and PK)") key = fields.IntField() manager = fields.ForeignKeyField("models.Team", related_name="team_members", null=True) talks_to = fields.ManyToManyField("models.Team", related_name="gets_talked_to") class Meta: table_description = "The TEAMS!" indexes = [("manager", "key"), ["manager_id", "name"]]
class Profile(models.Model): user = fields.OneToOneField('models.UserModel', related_name='profile') first_name = fields.CharField(max_length=50, null=True) last_name = fields.CharField(max_length=50, null=True) cities = fields.ManyToManyField('models.City', related_name='profiles') class PydanticMeta: exclude = ('user.email', 'user.hashed_password', 'user.is_active', 'user.confirmation')
class Product(BaseModel): categories = fields.ManyToManyField("diff_models.Category") name = fields.CharField(max_length=50) view_num = fields.IntField(description="View Num") sort = fields.IntField() is_reviewed = fields.BooleanField(description="Is Reviewed") image = fields.CharField(max_length=200) body = fields.TextField() created_at = fields.DatetimeField(auto_now_add=True)
class User(Model): username = fields.CharField(max_length=200) data_source_id = fields.CharField(max_length=100) name = fields.CharField(max_length=100) company = fields.CharField(max_length=200, null=True) blog = fields.CharField(max_length=200, null=True) location = fields.CharField(max_length=200, null=True) email = fields.CharField(max_length=200, null=True) bio = fields.CharField(max_length=200, null=True) followers = fields.ManyToManyField('models.User', related_name='user_followers', through='user_followers') followings = fields.ManyToManyField('models.User', related_name='user_following', through='user_followings') def __str__(self): return f'User: {self.username}'