didier/database/models.py

145 lines
4.7 KiB
Python
Raw Normal View History

from __future__ import annotations
2022-06-19 00:36:38 +02:00
from datetime import datetime
2022-06-30 19:42:48 +02:00
from typing import Optional
2022-06-19 00:36:38 +02:00
2022-07-11 22:23:38 +02:00
from sqlalchemy import BigInteger, Boolean, Column, DateTime, ForeignKey, Integer, Text
2022-06-17 01:36:47 +02:00
from sqlalchemy.orm import declarative_base, relationship
Base = declarative_base()
2022-06-17 01:36:47 +02:00
2022-07-11 22:23:38 +02:00
__all__ = [
"Base",
"Bank",
"CustomCommand",
"CustomCommandAlias",
"NightlyData",
"UforaAnnouncement",
"UforaCourse",
"UforaCourseAlias",
"User",
]
2022-06-30 19:42:48 +02:00
class Bank(Base):
"""A user's currency information"""
__tablename__ = "bank"
bank_id: int = Column(Integer, primary_key=True)
user_id: int = Column(BigInteger, ForeignKey("users.user_id"))
dinks: int = Column(BigInteger, server_default="0", nullable=False)
invested: int = Column(BigInteger, server_default="0", nullable=False)
2022-06-30 21:17:48 +02:00
2022-06-30 19:42:48 +02:00
# Interest rate
interest_level: int = Column(Integer, server_default="1", nullable=False)
2022-06-30 19:42:48 +02:00
# Maximum amount that can be stored in the bank
capacity_level: int = Column(Integer, server_default="1", nullable=False)
2022-06-30 19:42:48 +02:00
# Maximum amount that can be robbed
rob_level: int = Column(Integer, server_default="1", nullable=False)
2022-06-30 19:42:48 +02:00
user: User = relationship("User", uselist=False, back_populates="bank", lazy="selectin")
2022-06-21 23:58:21 +02:00
class CustomCommand(Base):
"""Custom commands to fill the hole Dyno couldn't"""
__tablename__ = "custom_commands"
command_id: int = Column(Integer, primary_key=True)
name: str = Column(Text, nullable=False, unique=True)
indexed_name: str = Column(Text, nullable=False, index=True)
response: str = Column(Text, nullable=False)
aliases: list[CustomCommandAlias] = relationship(
"CustomCommandAlias", back_populates="command", uselist=True, cascade="all, delete-orphan", lazy="selectin"
)
class CustomCommandAlias(Base):
"""Aliases for custom commands"""
__tablename__ = "custom_command_aliases"
alias_id: int = Column(Integer, primary_key=True)
alias: str = Column(Text, nullable=False, unique=True)
indexed_alias: str = Column(Text, nullable=False, index=True)
command_id: int = Column(Integer, ForeignKey("custom_commands.command_id"))
command: CustomCommand = relationship("CustomCommand", back_populates="aliases", uselist=False, lazy="selectin")
2022-06-30 19:42:48 +02:00
class NightlyData(Base):
"""Data for a user's Nightly stats"""
__tablename__ = "nightly_data"
nightly_id: int = Column(Integer, primary_key=True)
user_id: int = Column(BigInteger, ForeignKey("users.user_id"))
last_nightly: Optional[datetime] = Column(DateTime(timezone=True), nullable=True)
count: int = Column(Integer, server_default="0", nullable=False)
2022-06-30 19:42:48 +02:00
user: User = relationship("User", back_populates="nightly_data", uselist=False, lazy="selectin")
2022-06-17 01:36:47 +02:00
class UforaCourse(Base):
"""A course on Ufora"""
__tablename__ = "ufora_courses"
course_id: int = Column(Integer, primary_key=True)
name: str = Column(Text, nullable=False, unique=True)
code: str = Column(Text, nullable=False, unique=True)
year: int = Column(Integer, nullable=False)
log_announcements: bool = Column(Boolean, server_default="0", nullable=False)
2022-06-17 01:36:47 +02:00
announcements: list[UforaAnnouncement] = relationship(
2022-06-19 00:23:25 +02:00
"UforaAnnouncement", back_populates="course", cascade="all, delete-orphan", lazy="selectin"
2022-06-17 01:36:47 +02:00
)
aliases: list[UforaCourseAlias] = relationship(
2022-06-19 00:23:25 +02:00
"UforaCourseAlias", back_populates="course", cascade="all, delete-orphan", lazy="selectin"
2022-06-17 01:36:47 +02:00
)
class UforaCourseAlias(Base):
"""An alias for a course on Ufora that we use to refer to them"""
__tablename__ = "ufora_course_aliases"
alias_id: int = Column(Integer, primary_key=True)
alias: str = Column(Text, nullable=False, unique=True)
course_id: int = Column(Integer, ForeignKey("ufora_courses.course_id"))
2022-06-19 00:23:25 +02:00
course: UforaCourse = relationship("UforaCourse", back_populates="aliases", uselist=False, lazy="selectin")
2022-06-17 01:36:47 +02:00
class UforaAnnouncement(Base):
"""An announcement sent on Ufora"""
__tablename__ = "ufora_announcements"
2022-06-30 19:42:48 +02:00
announcement_id: int = Column(Integer, primary_key=True)
course_id: int = Column(Integer, ForeignKey("ufora_courses.course_id"))
2022-06-19 00:36:38 +02:00
publication_date: datetime = Column(DateTime(timezone=True))
2022-06-17 01:36:47 +02:00
2022-06-19 00:23:25 +02:00
course: UforaCourse = relationship("UforaCourse", back_populates="announcements", uselist=False, lazy="selectin")
2022-06-30 19:42:48 +02:00
class User(Base):
"""A Didier user"""
__tablename__ = "users"
user_id: int = Column(BigInteger, primary_key=True)
bank: Bank = relationship(
"Bank", back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
)
nightly_data: NightlyData = relationship(
"NightlyData", back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
)