diff --git a/src/migrations/versions/439a77f8a4d4_.py b/src/migrations/versions/439a77f8a4d4_.py new file mode 100644 index 0000000..84c6829 --- /dev/null +++ b/src/migrations/versions/439a77f8a4d4_.py @@ -0,0 +1,69 @@ +"""empty message + +Revision ID: 439a77f8a4d4 +Revises: 5e60c8fbc553 +Create Date: 2026-07-22 15:19:15.853280 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '439a77f8a4d4' +down_revision: Union[str, Sequence[str], None] = '5e60c8fbc553' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('markets', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=64), nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('pk_markets')), + sa.UniqueConstraint('name', name=op.f('uq_markets_name')) + ) + with op.batch_alter_table('markets', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_markets_id'), ['id'], unique=False) + + with op.batch_alter_table('reports', schema=None) as batch_op: + batch_op.add_column(sa.Column('doc_id', sa.Uuid(), nullable=False)) + batch_op.add_column(sa.Column('uploaded_at', sa.TIMESTAMP(), nullable=False)) + batch_op.add_column(sa.Column('doc_date', sa.TIMESTAMP(), nullable=False)) + batch_op.add_column(sa.Column('status', sa.String(length=64), nullable=False)) + batch_op.add_column(sa.Column('user_id', sa.Uuid(), nullable=False)) + batch_op.add_column(sa.Column('market_id', sa.Integer(), nullable=False)) + batch_op.create_index(batch_op.f('ix_reports_market_id'), ['market_id'], unique=False) + batch_op.create_index(batch_op.f('ix_reports_user_id'), ['user_id'], unique=False) + batch_op.create_unique_constraint(batch_op.f('uq_reports_doc_id'), ['doc_id']) + batch_op.create_foreign_key(batch_op.f('fk_reports_user_id_users'), 'users', ['user_id'], ['id'], ondelete='CASCADE') + batch_op.create_foreign_key(batch_op.f('fk_reports_market_id_markets'), 'markets', ['market_id'], ['id'], ondelete='CASCADE') + + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('reports', schema=None) as batch_op: + batch_op.drop_constraint(batch_op.f('fk_reports_market_id_markets'), type_='foreignkey') + batch_op.drop_constraint(batch_op.f('fk_reports_user_id_users'), type_='foreignkey') + batch_op.drop_constraint(batch_op.f('uq_reports_doc_id'), type_='unique') + batch_op.drop_index(batch_op.f('ix_reports_user_id')) + batch_op.drop_index(batch_op.f('ix_reports_market_id')) + batch_op.drop_column('market_id') + batch_op.drop_column('user_id') + batch_op.drop_column('status') + batch_op.drop_column('doc_date') + batch_op.drop_column('uploaded_at') + batch_op.drop_column('doc_id') + + with op.batch_alter_table('markets', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_markets_id')) + + op.drop_table('markets') + # ### end Alembic commands ### diff --git a/src/migrations/versions/75074097a2a3_.py b/src/migrations/versions/75074097a2a3_.py new file mode 100644 index 0000000..c7d7781 --- /dev/null +++ b/src/migrations/versions/75074097a2a3_.py @@ -0,0 +1,103 @@ +"""empty message + +Revision ID: 75074097a2a3 +Revises: 8c300c4d43ea +Create Date: 2026-07-22 16:11:08.077455 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '75074097a2a3' +down_revision: Union[str, Sequence[str], None] = '8c300c4d43ea' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('accountant_settings', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=64), nullable=False), + sa.Column('database_key', sa.Uuid(), nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('pk_accountant_settings')), + sa.UniqueConstraint('database_key', name=op.f('uq_accountant_settings_database_key')) + ) + with op.batch_alter_table('accountant_settings', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_accountant_settings_id'), ['id'], unique=False) + batch_op.create_index(batch_op.f('ix_accountant_settings_name'), ['name'], unique=True) + + op.create_table('doc_types', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=64), nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('pk_doc_types')) + ) + with op.batch_alter_table('doc_types', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_doc_types_id'), ['id'], unique=False) + batch_op.create_index(batch_op.f('ix_doc_types_name'), ['name'], unique=True) + + op.create_table('fabric', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=64), nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('pk_fabric')) + ) + with op.batch_alter_table('fabric', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_fabric_id'), ['id'], unique=False) + batch_op.create_index(batch_op.f('ix_fabric_name'), ['name'], unique=True) + + op.create_table('goods', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('article', sa.String(length=16), nullable=False), + sa.Column('price', sa.Numeric(precision=10, scale=2), nullable=False), + sa.Column('tnvd', sa.Integer(), nullable=False), + sa.Column('doc_type_id', sa.Integer(), nullable=False), + sa.Column('fabric_id', sa.Integer(), nullable=False), + sa.Column('status', sa.Boolean(), nullable=False), + sa.ForeignKeyConstraint(['doc_type_id'], ['doc_types.id'], name=op.f('fk_goods_doc_type_id_doc_types'), ondelete='CASCADE'), + sa.ForeignKeyConstraint(['fabric_id'], ['fabric.id'], name=op.f('fk_goods_fabric_id_fabric'), ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id', name=op.f('pk_goods')) + ) + with op.batch_alter_table('goods', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_goods_article'), ['article'], unique=True) + batch_op.create_index(batch_op.f('ix_goods_doc_type_id'), ['doc_type_id'], unique=False) + batch_op.create_index(batch_op.f('ix_goods_fabric_id'), ['fabric_id'], unique=False) + batch_op.create_index(batch_op.f('ix_goods_id'), ['id'], unique=False) + batch_op.create_index(batch_op.f('ix_goods_price'), ['price'], unique=False) + batch_op.create_index(batch_op.f('ix_goods_tnvd'), ['tnvd'], unique=True) + + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('goods', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_goods_tnvd')) + batch_op.drop_index(batch_op.f('ix_goods_price')) + batch_op.drop_index(batch_op.f('ix_goods_id')) + batch_op.drop_index(batch_op.f('ix_goods_fabric_id')) + batch_op.drop_index(batch_op.f('ix_goods_doc_type_id')) + batch_op.drop_index(batch_op.f('ix_goods_article')) + + op.drop_table('goods') + with op.batch_alter_table('fabric', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_fabric_name')) + batch_op.drop_index(batch_op.f('ix_fabric_id')) + + op.drop_table('fabric') + with op.batch_alter_table('doc_types', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_doc_types_name')) + batch_op.drop_index(batch_op.f('ix_doc_types_id')) + + op.drop_table('doc_types') + with op.batch_alter_table('accountant_settings', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_accountant_settings_name')) + batch_op.drop_index(batch_op.f('ix_accountant_settings_id')) + + op.drop_table('accountant_settings') + # ### end Alembic commands ### diff --git a/src/migrations/versions/8c300c4d43ea_.py b/src/migrations/versions/8c300c4d43ea_.py new file mode 100644 index 0000000..a314882 --- /dev/null +++ b/src/migrations/versions/8c300c4d43ea_.py @@ -0,0 +1,40 @@ +"""empty message + +Revision ID: 8c300c4d43ea +Revises: 439a77f8a4d4 +Create Date: 2026-07-22 15:20:34.871724 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '8c300c4d43ea' +down_revision: Union[str, Sequence[str], None] = '439a77f8a4d4' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('reports', schema=None) as batch_op: + batch_op.alter_column('uploaded_at', + existing_type=sa.TIMESTAMP(), + nullable=True) + + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('reports', schema=None) as batch_op: + batch_op.alter_column('uploaded_at', + existing_type=sa.TIMESTAMP(), + nullable=False) + + # ### end Alembic commands ### diff --git a/src/models/database_models/__init__.py b/src/models/database_models/__init__.py index 9982f85..de245d1 100644 --- a/src/models/database_models/__init__.py +++ b/src/models/database_models/__init__.py @@ -1,2 +1,4 @@ -from .files import Stored -from .model import Model \ No newline at end of file +from .files import Stored, Markets +from .model import Model +from .goods import Nomenclature, DocumentTypes, Fabric +from .accountant import AccountantSettings \ No newline at end of file diff --git a/src/models/database_models/accountant.py b/src/models/database_models/accountant.py new file mode 100644 index 0000000..bee4313 --- /dev/null +++ b/src/models/database_models/accountant.py @@ -0,0 +1,14 @@ +from src.models.database_models.model import Model +from sqlalchemy import String +from sqlalchemy.orm import Mapped, mapped_column +from uuid import UUID, uuid1 + +class AccountantSettings(Model): + __tablename__="accountant_settings" + + id:Mapped[int]=mapped_column(primary_key=True, index=True) + name:Mapped[str]=mapped_column(String(64),unique=True, index=True) + database_key:Mapped[UUID]=mapped_column(default=uuid1, unique=True) + + def __repr__(self) -> str: + return f'ID: {self.id}, Name: {self.name}' \ No newline at end of file diff --git a/src/models/database_models/files.py b/src/models/database_models/files.py index 388859e..6e396a7 100644 --- a/src/models/database_models/files.py +++ b/src/models/database_models/files.py @@ -1,14 +1,43 @@ from datetime import datetime -from sqlalchemy import TIMESTAMP, String, func -from sqlalchemy.orm import Mapped, mapped_column +from sqlalchemy import TIMESTAMP, String, func, ForeignKey +from sqlalchemy.orm import Mapped, mapped_column, relationship from src.models.database_models.model import Model +from uuid import UUID, uuid1 + class Stored(Model): __tablename__="reports" id:Mapped[int]=mapped_column(primary_key=True, index=True) + doc_id:Mapped[UUID]=mapped_column(default=uuid1, unique=True) filename:Mapped[str]=mapped_column(String(255), index=True) created_at:Mapped[datetime]=mapped_column(TIMESTAMP, server_default=func.now()) + uploaded_at:Mapped[datetime]=mapped_column(TIMESTAMP,onupdate=func.now(), nullable=True) + doc_date:Mapped[datetime]=mapped_column(TIMESTAMP) + status:Mapped[str]=mapped_column(String(64)) + user_id:Mapped[UUID]=mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True) + market_id:Mapped[int]=mapped_column(ForeignKey("markets.id", ondelete="CASCADE"),index=True) + + user:Mapped["User"]=relationship(back_populates="report") + market:Mapped["Markets"]=relationship(back_populates="report") def __repr__(self) -> str: - return f'ID: {self.id}, Filename: {self.filename}' \ No newline at end of file + return f'ID: {self.id}, Filename: {self.filename}, Status: {self.status}' + +class Markets(Model): + __tablename__="markets" + + id:Mapped[int]=mapped_column(primary_key=True, index=True) + name:Mapped[str]=mapped_column(String(64), unique=True) + + report:Mapped[list["Stored"]]=relationship(back_populates="market") + def __repr__(self) -> str: + return f'ID: {self.id}, Name: {self.name}' + + + + + + + + \ No newline at end of file diff --git a/src/models/database_models/goods.py b/src/models/database_models/goods.py new file mode 100644 index 0000000..4f9b1c5 --- /dev/null +++ b/src/models/database_models/goods.py @@ -0,0 +1,42 @@ +from decimal import Decimal +from src.models.database_models.model import Model +from sqlalchemy import Boolean, ForeignKey, Integer, Numeric, String +from sqlalchemy.orm import Mapped, mapped_column, relationship + +class Nomenclature(Model): + __tablename__="goods" + + id:Mapped[int]=mapped_column(primary_key=True, index=True) + article:Mapped[str]=mapped_column(String(16), unique=True, index=True) + price:Mapped[Decimal]=mapped_column(Numeric(10, 2), index=True) + tnvd:Mapped[int]=mapped_column(Integer, index=True, unique=True) + doc_type_id:Mapped[int]=mapped_column(ForeignKey("doc_types.id", ondelete="CASCADE"), index=True) + fabric_id:Mapped[int]=mapped_column(ForeignKey("fabric.id", ondelete="CASCADE"), index=True) + status:Mapped[bool]=mapped_column(Boolean) + + doc_type:Mapped["DocumentTypes"]=relationship(back_populates="good") + fabric:Mapped["Fabric"]=relationship(back_populates="good") + + def __repr__(self) -> str: + return f'ID: {self.id}, Article: {self.article}, Price: {self.price}' +class DocumentTypes(Model): + __tablename__="doc_types" + + id:Mapped[int]=mapped_column(primary_key=True, index=True) + name:Mapped[str]=mapped_column(String(64), unique=True, index=True) + + good:Mapped[list["Nomenclature"]]=relationship(back_populates="doc_type") + + def __repr__(self) -> str: + return f'ID: {self.id}, Name:{self.name}' + +class Fabric(Model): + __tablename__="fabric" + + id:Mapped[int]=mapped_column(primary_key=True, index=True) + name:Mapped[str]=mapped_column(String(64),unique=True, index=True) + + + good:Mapped[list["Nomenclature"]]=relationship(back_populates="fabric") + def __repr__(self) -> str: + return f"ID: {self.id}, Name: {self.name}" \ No newline at end of file diff --git a/src/models/database_models/model.py b/src/models/database_models/model.py index eaad3dd..2253a7b 100644 --- a/src/models/database_models/model.py +++ b/src/models/database_models/model.py @@ -30,6 +30,7 @@ class User(Model): direct_permissions:Mapped[list['Permissions']]=relationship(secondary="user_direct_permissions", back_populates="users_direct") refresh_token:Mapped[list['RefreshTokens']]=relationship(back_populates="user") + report:Mapped[list["Stored"]]=relationship(back_populates="user") def __repr__(self) -> str: return f"ID: {self.id}, Name: {self.first_name}, Status: {self.status}" diff --git a/src/models/pydantic_models/accountant.py b/src/models/pydantic_models/accountant.py new file mode 100644 index 0000000..947a8f0 --- /dev/null +++ b/src/models/pydantic_models/accountant.py @@ -0,0 +1,9 @@ +from typing import Annotated +from src.models.pydantic_models.model import Base +from pydantic import Field +from uuid import UUID +class AccountantCreate(Base): + + name:Annotated[str, Field(...,max_length=64,description="name of the accountant setting")] + database_key:Annotated[UUID, Field(..., description="database key from the 1C server")] + \ No newline at end of file diff --git a/src/models/pydantic_models/files.py b/src/models/pydantic_models/files.py new file mode 100644 index 0000000..9d2c454 --- /dev/null +++ b/src/models/pydantic_models/files.py @@ -0,0 +1,26 @@ +from datetime import datetime +from typing import Annotated +from uuid import UUID +from src.models.pydantic_models.model import Base +from pydantic import Field + +class ReportCreate(Base): + filename:Annotated[str,Field(..., min_length=2, max_length=255, description="name of the report")] + doc_date:Annotated[datetime, Field(..., description="ts of the report")] + status:Annotated[str, Field(..., max_length=64, description="status of the report (uploaded,finalized, failed)")] + user:Annotated[UUID, Field(..., description="id of the user author")] + market:Annotated[str, Field(..., max_length=64,description="name of the market")] + +class ReportOut(Base): + filename:Annotated[str,Field(..., min_length=2, max_length=255, description="name of the report")] + doc_date:Annotated[datetime, Field(..., description="ts of the report")] + status:Annotated[str, Field(..., max_length=64, description="status of the report (uploaded,finalized, failed)")] + user:Annotated[UUID, Field(..., description="id of the user author")] + market:Annotated[str, Field(..., max_length=64, description="name of the market")] + + +class MarketCreate(Base): + name:Annotated[str, Field(..., max_length=64,description="certificate or declaration")] + +class MarketOut(Base): + name:Annotated[str, Field(..., max_length=64,description="certificate or declaration")] \ No newline at end of file diff --git a/src/models/pydantic_models/goods.py b/src/models/pydantic_models/goods.py new file mode 100644 index 0000000..af479ea --- /dev/null +++ b/src/models/pydantic_models/goods.py @@ -0,0 +1,35 @@ +from decimal import Decimal +from src.models.pydantic_models.model import Base +from pydantic import Field +from typing import Annotated + +class NomenclatureCreate(Base): + + article:Annotated[str, Field(...,min_length=5,max_length=16, description="name of the article")] + price:Annotated[Decimal, Field(..., max_digits=12, description="price of the article")] + tnvd:Annotated[int, Field(..., max_digits=10, description="tnvd from the Markirovka.crpt")] + status:Annotated[bool, Field(..., description="status of the article")] + + doc_type:Annotated[str, Field(..., description="document type (declaration or certificate)")] + fabric:Annotated[str, Field(..., description="fabric of the product")] + +class NomenclatureOut(Base): + article:Annotated[str, Field(..., min_length=5,max_length=16, description="name of the article")] + price:Annotated[Decimal, Field(..., max_digits=12,description="price of the article")] + tnvd:Annotated[int, Field(..., max_digits=10,description="tnvd from the Markirovka.crpt")] + status:Annotated[bool, Field(..., description="status of the article")] + + doc_type:Annotated[str, Field(...,max_length=64, description="document type (declaration or certificate)")] + fabric:Annotated[str, Field(...,max_length=64, description="fabric of the product")] + +class DocumentTypeCreate(Base): + name:Annotated[str, Field(...,max_length=64, description="certificate or declaration")] + +class DocumentTypeOut(Base): + name:Annotated[str, Field(..., max_length=64,description="certificate or declaration")] + +class FabricCreate(Base): + name:Annotated[str, Field(..., max_length=64,description="certificate or declaration")] + +class FabricOut(Base): + name:Annotated[str, Field(..., max_length=64,description="certificate or declaration")] \ No newline at end of file diff --git a/src/models/pydantic_models/model.py b/src/models/pydantic_models/model.py index 7298bff..0ade6d0 100644 --- a/src/models/pydantic_models/model.py +++ b/src/models/pydantic_models/model.py @@ -40,8 +40,7 @@ class UserUpdate(Base): permissions:Annotated[list[str]|None, Field(None, description="permissions of the user")] permission_groups:Annotated[list[str]|None, Field(None, description="permissions groups of the user")] -class Permissions(Base): - id:int +class PermissionsCreate(Base): permission:Annotated[str, Field(..., max_length=30, description="permission name")] @@ -49,8 +48,8 @@ class PermissionsOut(Base): permission:Annotated[str, Field(..., max_length=30, description="permission name")] -class PermissionsGroups(Base): - id:int +class PermissionsGroupsCreate(Base): + group:Annotated[str, Field(..., max_length=255, description="group name for the permissions")] @@ -59,21 +58,20 @@ class PermissionsGroupsOut(Base): group:Annotated[str, Field(..., max_length=255, description="group name for the permissions")] -class RefreshTokens(Base): +class RefreshTokensCreate(Base): user_id:Annotated[UUID, Field(..., description="foreign key for the user")] - token_hash:Annotated[str, Field(..., description="token hash")] - device_info:Annotated[str, Field(..., description="User device info")] - ip_address:Annotated[str, Field(..., description="ip v4/v6 of the user")] + token_hash:Annotated[str, Field(...,max_length=255, description="token hash")] + device_info:Annotated[str, Field(...,max_length=255, description="User device info")] + ip_address:Annotated[str, Field(...,max_length=45, description="ip v4/v6 of the user")] is_revoked:Annotated[bool|None, Field(None, description="revoke token if logout was made")] expires_at:Annotated[datetime, Field(..., description="when token is going to be expired")] class RefreshTokensOut(Base): - - id:int + user_id:Annotated[UUID, Field(..., description="foreign key for the user")] - token_hash:Annotated[str, Field(..., description="token hash")] - device_info:Annotated[str, Field(..., description="User device info")] - ip_address:Annotated[str, Field(..., description="ip v4/v6 of the user")] + token_hash:Annotated[str, Field(..., max_length=255,description="token hash")] + device_info:Annotated[str, Field(..., max_length=255, description="User device info")] + ip_address:Annotated[str, Field(...,max_length=45, description="ip v4/v6 of the user")] is_revoked:Annotated[bool|None, Field(None, description="revoke token if logout was made")] expires_at:Annotated[datetime, Field(..., description="when token is going to be expired")]