summaryrefslogtreecommitdiffstats
path: root/db/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'db/models.py')
-rw-r--r--db/models.py28
1 files changed, 22 insertions, 6 deletions
diff --git a/db/models.py b/db/models.py
index b66f5bc..e758671 100644
--- a/db/models.py
+++ b/db/models.py
@@ -1,13 +1,29 @@
1from uuid import uuid4, UUID 1from uuid import uuid4, UUID, uuid7
2 2
3from sqlmodel import SQLModel, Field 3from sqlmodel import SQLModel, Field
4from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin
4 5
6from authentication.utils import bcrypt_sha256_hash
5 7
6class BaseModel(SQLModel):
7 id: int | None = Field(primary_key=True, default=None)
8 8
9class ExternalModelMixin: 9class BaseModel(SQLModel):
10 external_id: UUID = Field(index=True, unique=True, default_factory=uuid4) 10 id: UUID | None = Field(primary_key=True, default_factory=uuid7)
11 11
12class User(BaseModel, ExternalModelMixin, table=True): 12class User(BaseModel, table=True):
13 username: str = Field(unique=True, index=True) 13 username: str = Field(unique=True, index=True)
14 password: str = Field(nullable=False)
15
16 email: str
17
18 def set_password(self, value):
19 self.password = bcrypt_sha256_hash(value)
20
21
22class ClientApplication(BaseModel, OAuth2ClientMixin, table=True):
23 name: str
24
25class AuthCode(BaseModel, OAuth2AuthorizationCodeMixin, table=True):
26 user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE")
27
28class AuthToken(BaseModel, OAuth2TokenMixin, table=True):
29 user_id: UUID = Field(foreign_key="user.id", ondelete="CASCADE")