-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52820da
commit 1137866
Showing
22 changed files
with
444 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ SQLAlchemy==2.0.36 | |
typing_extensions==4.12.2 | ||
Werkzeug==3.1.2 | ||
WTForms==3.2.1 | ||
loguru~=0.7.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from uuid import uuid4 | ||
|
||
from core.application.user.ports.services.cryptography import ( | ||
ICryptographyService, | ||
) | ||
from core.domain.user.entities.role import RoleEntity | ||
from core.domain.user.entities.user import UserEntity | ||
|
||
|
||
class UserFactory: | ||
def __init__(self, cryptography_service: ICryptographyService): | ||
self._cryptography_service = cryptography_service | ||
|
||
def create_user( | ||
self, | ||
username: str, | ||
email: str, | ||
password: str, | ||
roles: list[RoleEntity], | ||
) -> UserEntity: | ||
salt = self._cryptography_service.generate_salt() | ||
password_hash = self._cryptography_service.hash_password( | ||
password=password, salt=salt | ||
) | ||
return UserEntity( | ||
user_id=uuid4(), | ||
username=username, | ||
email=email, | ||
password_hash=password_hash, | ||
roles=roles, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from dataclasses import dataclass | ||
from uuid import UUID, uuid4 | ||
|
||
|
||
@dataclass | ||
class RoleEntity: | ||
role_id: UUID | ||
role_name: str | ||
|
||
def __post_init__(self): | ||
self._validate() | ||
|
||
def _validate(self): | ||
if not 3 <= len(self.role_name) <= 64: | ||
raise ValueError("Role name must be between 3 and 64 characters") | ||
|
||
@staticmethod | ||
def create(role_name: str) -> "RoleEntity": | ||
return RoleEntity(role_id=uuid4(), role_name=role_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,22 @@ | ||
from dataclasses import dataclass | ||
from uuid import UUID, uuid4 | ||
|
||
from core.domain.user.entities.role import RoleEntity | ||
|
||
|
||
@dataclass | ||
class UserEntity: | ||
user_id: UUID | ||
username: str | ||
email: str | ||
password_hash: str | ||
roles: list[RoleEntity] | ||
|
||
def __post_init__(self): | ||
self._validate() | ||
|
||
def _validate(self): | ||
if len(self.email) > 100: | ||
raise ValueError("Email is too long") | ||
if 3 > len(self.username) > 64: | ||
if not 3 <= len(self.username) <= 64: | ||
raise ValueError("Username must be between 3 and 64 characters") | ||
|
||
@staticmethod | ||
def create(username: str, email: str, password_hash: str) -> "UserEntity": | ||
return UserEntity( | ||
user_id=uuid4(), | ||
username=username, | ||
email=email, | ||
password_hash=password_hash, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from core.domain.user.exceptions.user_not_found import UserNotFoundException | ||
from core.domain.user.exceptions.user_already_exsist import ( | ||
UserAlreadyExistsException, | ||
) | ||
from core.domain.user.exceptions.role_not_found import RoleNotFoundException | ||
|
||
__all__ = ( | ||
"UserNotFoundException", | ||
"UserAlreadyExistsException", | ||
"RoleNotFoundException", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from core.shared.exceptions import NotFoundException | ||
|
||
|
||
class RoleNotFoundException(NotFoundException): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from core.shared.exceptions import AlreadyExistsException | ||
|
||
|
||
class UserAlreadyExistsException(AlreadyExistsException): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from core.shared.exceptions import NotFoundException | ||
|
||
|
||
class UserNotFoundException(NotFoundException): | ||
pass |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from abc import ABC, abstractmethod | ||
from uuid import UUID | ||
|
||
from core.domain.user.entities.role import RoleEntity | ||
|
||
|
||
class IRoleRepository(ABC): | ||
@abstractmethod | ||
async def commit(self) -> None: ... | ||
|
||
@abstractmethod | ||
async def save(self, role: RoleEntity) -> RoleEntity: ... | ||
|
||
@abstractmethod | ||
async def delete(self, role_id: UUID) -> None: ... | ||
|
||
@abstractmethod | ||
async def get_by_id(self, role_id: UUID) -> RoleEntity: ... | ||
|
||
@abstractmethod | ||
async def get_by_name(self, name: str) -> RoleEntity: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
from core.infrastructure.database.models.base import Base | ||
from core.infrastructure.database.models.user import User | ||
from core.infrastructure.database.models.role import Role | ||
from core.infrastructure.database.models.user_roles import UserRoles | ||
|
||
__all__ = ( | ||
"Base", | ||
"User", | ||
"Role", | ||
"UserRoles", | ||
) |
63 changes: 63 additions & 0 deletions
63
...re/infrastructure/database/migrations/versions/2024_11_07_2140-cb78897a4f9b_role_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Role table. | ||
Revision ID: cb78897a4f9b | ||
Revises: 5fad516883d3 | ||
Create Date: 2024-11-07 21:40:43.650063 | ||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "cb78897a4f9b" | ||
down_revision: Union[str, None] = "5fad516883d3" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"roles", | ||
sa.Column("role_id", sa.UUID(), nullable=False), | ||
sa.Column("role_name", sa.String(length=64), nullable=False), | ||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.PrimaryKeyConstraint("role_id"), | ||
) | ||
op.create_index( | ||
op.f("ix_roles_role_name"), "roles", ["role_name"], unique=True | ||
) | ||
op.create_table( | ||
"user_roles", | ||
sa.Column("user_id", sa.UUID(), nullable=False), | ||
sa.Column("role_id", sa.UUID(), nullable=False), | ||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["role_id"], | ||
["roles.role_id"], | ||
onupdate="CASCADE", | ||
ondelete="RESTRICT", | ||
), | ||
sa.ForeignKeyConstraint( | ||
["user_id"], | ||
["users.user_id"], | ||
onupdate="CASCADE", | ||
ondelete="CASCADE", | ||
), | ||
sa.PrimaryKeyConstraint("user_id", "role_id"), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table("user_roles") | ||
op.drop_index(op.f("ix_roles_role_name"), table_name="roles") | ||
op.drop_table("roles") | ||
# ### end Alembic commands ### |
Oops, something went wrong.