-
Notifications
You must be signed in to change notification settings - Fork 3
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
6d2c8d9
commit 3c26e97
Showing
20 changed files
with
217 additions
and
7 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
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
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
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
Empty file.
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,9 @@ | ||
from server.domain.common.types import ID | ||
from server.seedwork.domain.entities import Entity | ||
|
||
from ..organizations.types import Siret | ||
|
||
|
||
class Catalog(Entity): | ||
id: ID | ||
organization_siret: Siret |
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
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
Empty file.
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,37 @@ | ||
import datetime as dt | ||
from typing import TYPE_CHECKING, List | ||
|
||
from sqlalchemy import CHAR, Column, DateTime, ForeignKey, func | ||
from sqlalchemy.dialects.postgresql import UUID | ||
from sqlalchemy.orm import relationship | ||
|
||
from server.domain.common.types import ID | ||
from server.domain.organizations.types import Siret | ||
|
||
from ..database import Base | ||
|
||
if TYPE_CHECKING: | ||
from ..catalog_records.repositories import CatalogRecordModel | ||
from ..organizations.models import OrganizationModel | ||
|
||
|
||
class CatalogModel(Base): | ||
__tablename__ = "catalog" | ||
|
||
id: ID = Column(UUID(as_uuid=True), primary_key=True) | ||
created_at: dt.datetime = Column( | ||
DateTime(timezone=True), server_default=func.clock_timestamp(), nullable=False | ||
) | ||
organization_siret: Siret = Column( | ||
CHAR(14), ForeignKey("organization.siret"), nullable=False | ||
) | ||
organization: "OrganizationModel" = relationship( | ||
"OrganizationModel", | ||
back_populates="catalog", | ||
cascade="delete", | ||
) | ||
|
||
catalog_records: List["CatalogRecordModel"] = relationship( | ||
"CatalogRecordModel", | ||
back_populates="catalog", | ||
) |
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,7 @@ | ||
from server.seedwork.application.modules import Module | ||
|
||
from . import models # noqa # Trigger SQLAlchemy table discovery. | ||
|
||
|
||
class CatalogsModule(Module): | ||
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,20 @@ | ||
from server.domain.catalogs.entities import Catalog | ||
|
||
from .models import CatalogModel | ||
|
||
|
||
def make_entity(instance: CatalogModel) -> Catalog: | ||
return Catalog( | ||
id=instance.id, | ||
organization_siret=instance.organization_siret, | ||
) | ||
|
||
|
||
def make_instance(entity: Catalog) -> CatalogModel: | ||
return CatalogModel( | ||
**entity.dict( | ||
exclude={ | ||
"created_at", # Managed by DB | ||
} | ||
), | ||
) |
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,12 +1,29 @@ | ||
from typing import TYPE_CHECKING, List | ||
|
||
from sqlalchemy import CHAR, Column, String | ||
from sqlalchemy.orm import relationship | ||
|
||
from server.domain.organizations.types import Siret | ||
|
||
from ..database import Base | ||
|
||
if TYPE_CHECKING: | ||
from ..auth.repositories import UserModel | ||
from ..catalogs.models import CatalogModel | ||
|
||
|
||
class OrganizationModel(Base): | ||
__tablename__ = "organization" | ||
|
||
siret: Siret = Column(CHAR(14), primary_key=True) | ||
name: str = Column(String(), nullable=False) | ||
|
||
catalog: "CatalogModel" = relationship( | ||
"CatalogModel", | ||
back_populates="organization", | ||
uselist=False, | ||
) | ||
users: List["UserModel"] = relationship( | ||
"UserModel", | ||
back_populates="organization", | ||
) |
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 @@ | ||
"""add-catalog | ||
Revision ID: cfb6eef87415 | ||
Revises: d9ea6ea6708f | ||
Create Date: 2022-07-19 11:54:40.398243 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "cfb6eef87415" | ||
down_revision = "d9ea6ea6708f" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"catalog", | ||
sa.Column( | ||
"id", | ||
postgresql.UUID(as_uuid=True), | ||
server_default=sa.text("uuid_generate_v4()"), | ||
nullable=False, | ||
), | ||
sa.Column( | ||
"created_at", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("clock_timestamp()"), | ||
nullable=False, | ||
), | ||
sa.Column("organization_siret", sa.CHAR(length=14), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["organization_siret"], | ||
["organization.siret"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
# Create initial organization. | ||
op.execute( | ||
"INSERT INTO organization (siret, name) " | ||
"VALUES ('00000000000000', 'Organisation par défaut');" | ||
) | ||
|
||
# Create its catalog. | ||
op.execute("INSERT INTO catalog (organization_siret) VALUES ('00000000000000');") | ||
|
||
# Add all users to the initial organization. | ||
op.add_column("user", sa.Column("organization_siret", sa.CHAR(14))) | ||
op.execute("UPDATE \"user\" SET organization_siret = '00000000000000';") | ||
op.alter_column("user", "organization_siret", nullable=False) | ||
|
||
# Add all catalog records to the initial catalog. | ||
op.add_column("catalog_record", sa.Column("organization_siret", sa.CHAR(14))) | ||
op.execute("UPDATE catalog_record SET organization_siret = '00000000000000';") | ||
op.alter_column("catalog_record", "organization_siret", nullable=False) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table("catalog") |
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 |
---|---|---|
|
@@ -86,7 +86,12 @@ async def test_create_user( | |
user = response.json() | ||
id_ = user["id"] | ||
assert isinstance(id_, str) | ||
assert user == {"id": id_, "email": "[email protected]", "role": "USER"} | ||
assert user == { | ||
"id": id_, | ||
"organization_siret": "0" * 14, | ||
"email": "[email protected]", | ||
"role": "USER", | ||
} | ||
|
||
|
||
@pytest.mark.asyncio | ||
|
@@ -106,6 +111,7 @@ async def test_login(client: httpx.AsyncClient, temp_user: TestUser) -> None: | |
user = response.json() | ||
assert user == { | ||
"id": str(temp_user.id), | ||
"organization_siret": "0" * 14, | ||
"email": temp_user.email, | ||
"role": temp_user.role.value, | ||
"api_token": temp_user.api_token, | ||
|
Oops, something went wrong.