diff options
Diffstat (limited to 'db/models.py')
| -rw-r--r-- | db/models.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/db/models.py b/db/models.py index e758671..7e0afb0 100644 --- a/db/models.py +++ b/db/models.py | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | from datetime import datetime, UTC | ||
| 1 | from uuid import uuid4, UUID, uuid7 | 2 | from uuid import uuid4, UUID, uuid7 |
| 2 | 3 | ||
| 3 | from sqlmodel import SQLModel, Field | 4 | from sqlalchemy import DateTime |
| 5 | from sqlmodel import SQLModel, Field, Relationship | ||
| 4 | from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin | 6 | from authlib.integrations.sqla_oauth2 import OAuth2ClientMixin, OAuth2AuthorizationCodeMixin, OAuth2TokenMixin |
| 5 | 7 | ||
| 6 | from authentication.utils import bcrypt_sha256_hash | 8 | from authentication.utils import bcrypt_sha256_hash |
| @@ -8,6 +10,19 @@ from authentication.utils import bcrypt_sha256_hash | |||
| 8 | 10 | ||
| 9 | class BaseModel(SQLModel): | 11 | class BaseModel(SQLModel): |
| 10 | id: UUID | None = Field(primary_key=True, default_factory=uuid7) | 12 | id: UUID | None = Field(primary_key=True, default_factory=uuid7) |
| 13 | created: datetime = Field( | ||
| 14 | default_factory=lambda: datetime.now(tz=UTC), | ||
| 15 | sa_type=DateTime(timezone=True), | ||
| 16 | ) | ||
| 17 | updated: datetime = Field( | ||
| 18 | default=None, | ||
| 19 | nullable=True, | ||
| 20 | sa_column_kwargs={ | ||
| 21 | 'onupdate': lambda: datetime.now(tz=UTC), | ||
| 22 | }, | ||
| 23 | sa_type=DateTime(timezone=True), | ||
| 24 | ) | ||
| 25 | |||
| 11 | 26 | ||
| 12 | class User(BaseModel, table=True): | 27 | class User(BaseModel, table=True): |
| 13 | username: str = Field(unique=True, index=True) | 28 | username: str = Field(unique=True, index=True) |
| @@ -18,6 +33,9 @@ class User(BaseModel, table=True): | |||
| 18 | def set_password(self, value): | 33 | def set_password(self, value): |
| 19 | self.password = bcrypt_sha256_hash(value) | 34 | self.password = bcrypt_sha256_hash(value) |
| 20 | 35 | ||
| 36 | class Session(BaseModel, table=True): | ||
| 37 | user_id: UUID = Field(foreign_key='user.id', exclude=True) | ||
| 38 | user: User = Relationship() | ||
| 21 | 39 | ||
| 22 | class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): | 40 | class ClientApplication(BaseModel, OAuth2ClientMixin, table=True): |
| 23 | name: str | 41 | name: str |
