college-bot-v1/utils/db.py

120 lines
3.3 KiB
Python
Raw Normal View History

2022-10-04 16:20:01 +01:00
import datetime
2022-09-13 20:50:02 +01:00
import uuid
2022-10-04 16:20:01 +01:00
from typing import TYPE_CHECKING, Optional, TypeVar
2022-10-09 19:27:02 +01:00
from enum import IntEnum, auto
2022-09-13 20:50:02 +01:00
import orm
from databases import Database
2022-09-13 21:27:14 +01:00
import os
from pathlib import Path
2022-10-09 19:27:02 +01:00
class Tutors(IntEnum):
JAY = auto()
ZACH = auto()
IAN = auto()
REBECCA = auto()
LUPUPA = auto()
OTHER = -1 # not auto() because if we add more it could mess things up.
2022-09-13 21:27:25 +01:00
os.chdir(Path(__file__).parent.parent)
2022-09-13 20:50:02 +01:00
2022-10-30 16:31:38 +00:00
__all__ = ["registry", "get_or_none", "VerifyCode", "Student", "BannedStudentID", "Assignments", "Tutors"]
T = TypeVar("T")
2022-10-04 16:20:01 +01:00
T_co = TypeVar("T_co", covariant=True)
2022-09-13 20:50:02 +01:00
registry = orm.ModelRegistry(Database("sqlite:///main.db"))
2022-10-04 16:20:01 +01:00
async def get_or_none(model: T, **kw) -> Optional[T_co]:
"""Returns none or the required thing."""
try:
return await model.objects.get(**kw)
except orm.NoMatch:
return
2022-09-13 20:50:02 +01:00
class VerifyCode(orm.Model):
registry = registry
tablename = "codes"
fields = {
"id": orm.Integer(primary_key=True),
"code": orm.String(min_length=8, max_length=64, unique=True),
"bind": orm.BigInteger(),
2022-09-13 21:27:14 +01:00
"student_id": orm.String(min_length=7, max_length=7),
2022-11-23 14:34:58 +00:00
"name": orm.String(min_length=2, max_length=32),
2022-09-13 20:50:02 +01:00
}
if TYPE_CHECKING:
id: int
code: str
bind: int
student_id: str
2022-11-23 14:34:58 +00:00
name: str
2022-09-13 20:50:02 +01:00
class Student(orm.Model):
registry = registry
tablename = "students"
fields = {
"entry_id": orm.UUID(primary_key=True, default=uuid.uuid4),
"id": orm.String(min_length=7, max_length=7, unique=True),
"user_id": orm.BigInteger(unique=True),
2022-11-23 14:34:58 +00:00
"name": orm.String(min_length=2, max_length=32),
2022-09-13 20:50:02 +01:00
}
if TYPE_CHECKING:
entry_id: uuid.UUID
id: str
user_id: int
2022-11-23 14:34:58 +00:00
name: str
2022-10-04 16:20:01 +01:00
class BannedStudentID(orm.Model):
registry = registry
tablename = "banned"
fields = {
"entry_id": orm.UUID(primary_key=True, default=uuid.uuid4),
"student_id": orm.String(min_length=7, max_length=7, unique=True),
"associated_account": orm.BigInteger(default=None),
2022-10-30 16:31:38 +00:00
"banned_at_timestamp": orm.Float(default=lambda: datetime.datetime.utcnow().timestamp()),
2022-10-04 16:20:01 +01:00
}
if TYPE_CHECKING:
entry_id: uuid.UUID
student_id: str
associated_account: Optional[int]
banned_at_timestamp: float
2022-10-09 19:27:02 +01:00
class Assignments(orm.Model):
registry = registry
fields = {
"entry_id": orm.Integer(primary_key=True, default=None),
"created_by": orm.ForeignKey(Student, allow_null=True),
"title": orm.String(min_length=2, max_length=2000),
"classroom": orm.URL(allow_null=True, default=None, max_length=4096),
"shared_doc": orm.URL(allow_null=True, default=None, max_length=4096),
"created_at": orm.Float(default=lambda: datetime.datetime.now().timestamp()),
"due_by": orm.Float(),
"tutor": orm.Enum(Tutors),
"reminders": orm.JSON(default=[]),
"finished": orm.Boolean(default=False),
2022-10-30 16:31:38 +00:00
"submitted": orm.Boolean(default=False),
2022-11-07 11:20:48 +00:00
"assignees": orm.JSON(default=[]),
2022-10-09 19:27:02 +01:00
}
if TYPE_CHECKING:
entry_id: int
created_by: Student
title: str
classroom: Optional[str]
shared_doc: Optional[str]
created_at: float
due_by: float
tutor: Tutors
reminders: list[str]
finished: bool
submitted: bool
2022-11-07 11:20:48 +00:00
assignees: list[int]