This commit is contained in:
26
backend/.pre-commit-config.yaml
Normal file
26
backend/.pre-commit-config.yaml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
default_language_version:
|
||||||
|
python: python3.13
|
||||||
|
|
||||||
|
repos:
|
||||||
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
rev: v6.0.0
|
||||||
|
hooks:
|
||||||
|
- id: check-added-large-files
|
||||||
|
- id: trailing-whitespace
|
||||||
|
- id: check-ast
|
||||||
|
- id: check-builtin-literals
|
||||||
|
- id: check-docstring-first
|
||||||
|
- id: check-yaml
|
||||||
|
- id: check-toml
|
||||||
|
- id: mixed-line-ending
|
||||||
|
- id: end-of-file-fixer
|
||||||
|
- repo: local
|
||||||
|
hooks:
|
||||||
|
- id: check-pylint
|
||||||
|
name: check-pylint
|
||||||
|
entry: pylint -d R0801,R0903,W0511,W0603,C0103,R0902
|
||||||
|
language: system
|
||||||
|
types: [python]
|
||||||
|
pass_filenames: false
|
||||||
|
args:
|
||||||
|
- backend
|
||||||
@@ -35,6 +35,12 @@ hatch run pytest
|
|||||||
hatch run pytest --cov=src -vv
|
hatch run pytest --cov=src -vv
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Autoformat
|
||||||
|
```console
|
||||||
|
find -type f -name '*.py' ! -path 'alembic/*' -exec autopep8 --in-place --aggressive --aggressive '{}' \;
|
||||||
|
pylint -d R0801,R0903,W0511,W0603,C0103,R0902 .
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
`backend` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
`backend` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
||||||
|
|||||||
@@ -25,7 +25,8 @@ target_metadata = SQLModel.metadata
|
|||||||
|
|
||||||
# other values from the config, defined by the needs of env.py,
|
# other values from the config, defined by the needs of env.py,
|
||||||
# can be acquired:
|
# can be acquired:
|
||||||
config.set_main_option("sqlalchemy.url", f'postgresql://{settings.db_user}:{settings.db_pass}@{settings.db_host}:5432/{settings.db_name}')
|
config.set_main_option(
|
||||||
|
"sqlalchemy.url", f'postgresql://{settings.db_user}:{settings.db_pass}@{settings.db_host}:5432/{settings.db_name}')
|
||||||
# ... etc.
|
# ... etc.
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,12 @@ depends_on: Union[str, Sequence[str], None] = None
|
|||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
"""Upgrade schema."""
|
"""Upgrade schema."""
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
op.add_column('paymentmethod', sa.Column('max', sa.Integer(), nullable=True))
|
op.add_column(
|
||||||
|
'paymentmethod',
|
||||||
|
sa.Column(
|
||||||
|
'max',
|
||||||
|
sa.Integer(),
|
||||||
|
nullable=True))
|
||||||
# ### end Alembic commands ###
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,117 +22,121 @@ depends_on: Union[str, Sequence[str], None] = None
|
|||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
"""Upgrade schema."""
|
"""Upgrade schema."""
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
op.create_table('contracttype',
|
op.create_table(
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
'contracttype',
|
||||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column(
|
||||||
sa.PrimaryKeyConstraint('id')
|
'id',
|
||||||
)
|
sa.Integer(),
|
||||||
op.create_table('productor',
|
nullable=False),
|
||||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column(
|
||||||
sa.Column('address', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
'name',
|
||||||
sa.Column('type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sqlmodel.sql.sqltypes.AutoString(),
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
nullable=False),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id'))
|
||||||
)
|
op.create_table(
|
||||||
|
'productor', sa.Column(
|
||||||
|
'name', sqlmodel.sql.sqltypes.AutoString(), nullable=False), sa.Column(
|
||||||
|
'address', sqlmodel.sql.sqltypes.AutoString(), nullable=False), sa.Column(
|
||||||
|
'type', sqlmodel.sql.sqltypes.AutoString(), nullable=False), sa.Column(
|
||||||
|
'id', sa.Integer(), nullable=False), sa.PrimaryKeyConstraint('id'))
|
||||||
op.create_table('template',
|
op.create_table('template',
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('user',
|
op.create_table(
|
||||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
'user', sa.Column(
|
||||||
sa.Column('email', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
'name', sqlmodel.sql.sqltypes.AutoString(), nullable=False), sa.Column(
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
'email', sqlmodel.sql.sqltypes.AutoString(), nullable=False), sa.Column(
|
||||||
sa.PrimaryKeyConstraint('id')
|
'id', sa.Integer(), nullable=False), sa.PrimaryKeyConstraint('id'))
|
||||||
)
|
|
||||||
op.create_table('form',
|
op.create_table('form',
|
||||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('productor_id', sa.Integer(), nullable=True),
|
sa.Column('productor_id', sa.Integer(), nullable=True),
|
||||||
sa.Column('referer_id', sa.Integer(), nullable=True),
|
sa.Column('referer_id', sa.Integer(), nullable=True),
|
||||||
sa.Column('season', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('season', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('start', sa.Date(), nullable=False),
|
sa.Column('start', sa.Date(), nullable=False),
|
||||||
sa.Column('end', sa.Date(), nullable=False),
|
sa.Column('end', sa.Date(), nullable=False),
|
||||||
sa.Column('minimum_shipment_value', sa.Float(), nullable=True),
|
sa.Column('minimum_shipment_value', sa.Float(), nullable=True),
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.ForeignKeyConstraint(['productor_id'], ['productor.id'], ),
|
sa.ForeignKeyConstraint(['productor_id'], ['productor.id'], ),
|
||||||
sa.ForeignKeyConstraint(['referer_id'], ['user.id'], ),
|
sa.ForeignKeyConstraint(['referer_id'], ['user.id'], ),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('paymentmethod',
|
op.create_table('paymentmethod',
|
||||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('details', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('details', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.Column('productor_id', sa.Integer(), nullable=False),
|
sa.Column('productor_id', sa.Integer(), nullable=False),
|
||||||
sa.ForeignKeyConstraint(['productor_id'], ['productor.id'], ondelete='CASCADE'),
|
sa.ForeignKeyConstraint(['productor_id'], ['productor.id'], ondelete='CASCADE'),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('product',
|
op.create_table('product',
|
||||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('unit', sa.Enum('GRAMS', 'KILO', 'PIECE', name='unit'), nullable=False),
|
sa.Column('unit', sa.Enum('GRAMS', 'KILO', 'PIECE', name='unit'), nullable=False),
|
||||||
sa.Column('price', sa.Float(), nullable=True),
|
sa.Column('price', sa.Float(), nullable=True),
|
||||||
sa.Column('price_kg', sa.Float(), nullable=True),
|
sa.Column('price_kg', sa.Float(), nullable=True),
|
||||||
sa.Column('quantity', sa.Float(), nullable=True),
|
sa.Column('quantity', sa.Float(), nullable=True),
|
||||||
sa.Column('quantity_unit', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
sa.Column('quantity_unit', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||||
sa.Column('type', sa.Enum('OCCASIONAL', 'RECCURENT', name='producttype'), nullable=False),
|
sa.Column('type', sa.Enum('OCCASIONAL', 'RECCURENT', name='producttype'), nullable=False),
|
||||||
sa.Column('productor_id', sa.Integer(), nullable=True),
|
sa.Column('productor_id', sa.Integer(), nullable=True),
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.ForeignKeyConstraint(['productor_id'], ['productor.id'], ),
|
sa.ForeignKeyConstraint(['productor_id'], ['productor.id'], ),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('usercontracttypelink',
|
op.create_table(
|
||||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
'usercontracttypelink', sa.Column(
|
||||||
sa.Column('contract_type_id', sa.Integer(), nullable=False),
|
'user_id', sa.Integer(), nullable=False), sa.Column(
|
||||||
sa.ForeignKeyConstraint(['contract_type_id'], ['contracttype.id'], ),
|
'contract_type_id', sa.Integer(), nullable=False), sa.ForeignKeyConstraint(
|
||||||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
['contract_type_id'], ['contracttype.id'], ), sa.ForeignKeyConstraint(
|
||||||
sa.PrimaryKeyConstraint('user_id', 'contract_type_id')
|
['user_id'], ['user.id'], ), sa.PrimaryKeyConstraint(
|
||||||
)
|
'user_id', 'contract_type_id'))
|
||||||
op.create_table('contract',
|
op.create_table('contract',
|
||||||
sa.Column('firstname', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('firstname', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('lastname', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('lastname', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('email', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('email', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('phone', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('phone', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('payment_method', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('payment_method', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('cheque_quantity', sa.Integer(), nullable=False),
|
sa.Column('cheque_quantity', sa.Integer(), nullable=False),
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.Column('form_id', sa.Integer(), nullable=False),
|
sa.Column('form_id', sa.Integer(), nullable=False),
|
||||||
sa.Column('file', sa.LargeBinary(), nullable=True),
|
sa.Column('file', sa.LargeBinary(), nullable=True),
|
||||||
sa.Column('total_price', sa.Float(), nullable=True),
|
sa.Column('total_price', sa.Float(), nullable=True),
|
||||||
sa.ForeignKeyConstraint(['form_id'], ['form.id'], ondelete='CASCADE'),
|
sa.ForeignKeyConstraint(['form_id'], ['form.id'], ondelete='CASCADE'),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('shipment',
|
op.create_table('shipment',
|
||||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('date', sa.Date(), nullable=False),
|
sa.Column('date', sa.Date(), nullable=False),
|
||||||
sa.Column('form_id', sa.Integer(), nullable=True),
|
sa.Column('form_id', sa.Integer(), nullable=True),
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.ForeignKeyConstraint(['form_id'], ['form.id'], ondelete='CASCADE'),
|
sa.ForeignKeyConstraint(['form_id'], ['form.id'], ondelete='CASCADE'),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('cheque',
|
op.create_table('cheque',
|
||||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('value', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
sa.Column('value', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.Column('contract_id', sa.Integer(), nullable=False),
|
sa.Column('contract_id', sa.Integer(), nullable=False),
|
||||||
sa.ForeignKeyConstraint(['contract_id'], ['contract.id'], ondelete='CASCADE'),
|
sa.ForeignKeyConstraint(['contract_id'], ['contract.id'], ondelete='CASCADE'),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('contractproduct',
|
op.create_table('contractproduct',
|
||||||
sa.Column('product_id', sa.Integer(), nullable=False),
|
sa.Column('product_id', sa.Integer(), nullable=False),
|
||||||
sa.Column('shipment_id', sa.Integer(), nullable=True),
|
sa.Column('shipment_id', sa.Integer(), nullable=True),
|
||||||
sa.Column('quantity', sa.Float(), nullable=False),
|
sa.Column('quantity', sa.Float(), nullable=False),
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.Column('contract_id', sa.Integer(), nullable=False),
|
sa.Column('contract_id', sa.Integer(), nullable=False),
|
||||||
sa.ForeignKeyConstraint(['contract_id'], ['contract.id'], ondelete='CASCADE'),
|
sa.ForeignKeyConstraint(['contract_id'], ['contract.id'], ondelete='CASCADE'),
|
||||||
sa.ForeignKeyConstraint(['product_id'], ['product.id'], ondelete='CASCADE'),
|
sa.ForeignKeyConstraint(['product_id'], ['product.id'], ondelete='CASCADE'),
|
||||||
sa.ForeignKeyConstraint(['shipment_id'], ['shipment.id'], ondelete='CASCADE'),
|
sa.ForeignKeyConstraint(['shipment_id'], ['shipment.id'], ondelete='CASCADE'),
|
||||||
sa.PrimaryKeyConstraint('id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
op.create_table('shipmentproductlink',
|
op.create_table('shipmentproductlink',
|
||||||
sa.Column('shipment_id', sa.Integer(), nullable=False),
|
sa.Column('shipment_id', sa.Integer(), nullable=False),
|
||||||
sa.Column('product_id', sa.Integer(), nullable=False),
|
sa.Column('product_id', sa.Integer(), nullable=False),
|
||||||
sa.ForeignKeyConstraint(['product_id'], ['product.id'], ),
|
sa.ForeignKeyConstraint(['product_id'], ['product.id'], ),
|
||||||
sa.ForeignKeyConstraint(['shipment_id'], ['shipment.id'], ),
|
sa.ForeignKeyConstraint(['shipment_id'], ['shipment.id'], ),
|
||||||
sa.PrimaryKeyConstraint('shipment_id', 'product_id')
|
sa.PrimaryKeyConstraint('shipment_id', 'product_id')
|
||||||
)
|
)
|
||||||
# ### end Alembic commands ###
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,14 @@ depends_on: Union[str, Sequence[str], None] = None
|
|||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
"""Upgrade schema."""
|
"""Upgrade schema."""
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
op.add_column('form', sa.Column('visible', sa.Boolean(), nullable=False, default=False, server_default="False"))
|
op.add_column(
|
||||||
|
'form',
|
||||||
|
sa.Column(
|
||||||
|
'visible',
|
||||||
|
sa.Boolean(),
|
||||||
|
nullable=False,
|
||||||
|
default=False,
|
||||||
|
server_default="False"))
|
||||||
# ### end Alembic commands ###
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ dependencies = [
|
|||||||
"pytest",
|
"pytest",
|
||||||
"pytest-cov",
|
"pytest-cov",
|
||||||
"pytest-mock",
|
"pytest-mock",
|
||||||
|
"autopep8",
|
||||||
|
"prek",
|
||||||
|
"pylint",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.urls]
|
[project.urls]
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
alembic==1.18.4
|
||||||
|
annotated-doc==0.0.4
|
||||||
|
annotated-types==0.7.0
|
||||||
|
anyio==4.12.1
|
||||||
|
astroid==4.0.4
|
||||||
|
autopep8==2.3.2
|
||||||
|
brotli==1.2.0
|
||||||
|
certifi==2026.2.25
|
||||||
|
cffi==2.0.0
|
||||||
|
charset-normalizer==3.4.4
|
||||||
|
click==8.3.1
|
||||||
|
coverage==7.13.4
|
||||||
|
cryptography==46.0.5
|
||||||
|
cssselect2==0.9.0
|
||||||
|
dill==0.4.1
|
||||||
|
dnspython==2.8.0
|
||||||
|
email-validator==2.3.0
|
||||||
|
fastapi==0.135.1
|
||||||
|
fastapi-cli==0.0.24
|
||||||
|
fastapi-cloud-cli==0.14.0
|
||||||
|
fastar==0.8.0
|
||||||
|
fonttools==4.61.1
|
||||||
|
greenlet==3.3.2
|
||||||
|
h11==0.16.0
|
||||||
|
httpcore==1.0.9
|
||||||
|
httptools==0.7.1
|
||||||
|
httpx==0.28.1
|
||||||
|
idna==3.11
|
||||||
|
iniconfig==2.3.0
|
||||||
|
isort==8.0.1
|
||||||
|
Jinja2==3.1.6
|
||||||
|
lxml==6.0.2
|
||||||
|
Mako==1.3.10
|
||||||
|
markdown-it-py==4.0.0
|
||||||
|
MarkupSafe==3.0.3
|
||||||
|
mccabe==0.7.0
|
||||||
|
mdurl==0.1.2
|
||||||
|
odfdo==3.21.0
|
||||||
|
packaging==26.0
|
||||||
|
pillow==12.1.1
|
||||||
|
platformdirs==4.9.2
|
||||||
|
pluggy==1.6.0
|
||||||
|
prek==0.3.4
|
||||||
|
psycopg2-binary==2.9.11
|
||||||
|
pycodestyle==2.14.0
|
||||||
|
pycparser==3.0
|
||||||
|
pydantic==2.12.5
|
||||||
|
pydantic-extra-types==2.11.0
|
||||||
|
pydantic-settings==2.13.1
|
||||||
|
pydantic_core==2.41.5
|
||||||
|
pydyf==0.12.1
|
||||||
|
Pygments==2.19.2
|
||||||
|
PyJWT==2.11.0
|
||||||
|
pylint==4.0.5
|
||||||
|
pyphen==0.17.2
|
||||||
|
pytest==9.0.2
|
||||||
|
pytest-cov==7.0.0
|
||||||
|
pytest-mock==3.15.1
|
||||||
|
python-dotenv==1.2.2
|
||||||
|
python-multipart==0.0.22
|
||||||
|
PyYAML==6.0.3
|
||||||
|
requests==2.32.5
|
||||||
|
rich==14.3.3
|
||||||
|
rich-toolkit==0.19.7
|
||||||
|
rignore==0.7.6
|
||||||
|
sentry-sdk==2.53.0
|
||||||
|
shellingham==1.5.4
|
||||||
|
SQLAlchemy==2.0.47
|
||||||
|
sqlmodel==0.0.37
|
||||||
|
starlette==0.52.1
|
||||||
|
tinycss2==1.5.1
|
||||||
|
tinyhtml5==2.0.0
|
||||||
|
tomlkit==0.14.0
|
||||||
|
typer==0.24.1
|
||||||
|
typing-inspection==0.4.2
|
||||||
|
typing_extensions==4.15.0
|
||||||
|
urllib3==2.6.3
|
||||||
|
uvicorn==0.41.0
|
||||||
|
uvloop==0.22.1
|
||||||
|
watchfiles==1.1.1
|
||||||
|
weasyprint==68.1
|
||||||
|
webencodings==0.5.1
|
||||||
|
websockets==16.0
|
||||||
|
zopfli==0.4.1
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ router = APIRouter(prefix='/auth')
|
|||||||
jwk_client = PyJWKClient(JWKS_URL)
|
jwk_client = PyJWKClient(JWKS_URL)
|
||||||
security = HTTPBearer()
|
security = HTTPBearer()
|
||||||
|
|
||||||
|
|
||||||
@router.get('/logout')
|
@router.get('/logout')
|
||||||
def logout():
|
def logout():
|
||||||
params = {
|
params = {
|
||||||
@@ -59,9 +60,11 @@ def login():
|
|||||||
'redirect_uri': settings.keycloak_redirect_uri,
|
'redirect_uri': settings.keycloak_redirect_uri,
|
||||||
'state': state,
|
'state': state,
|
||||||
}
|
}
|
||||||
request_url = requests.Request('GET', AUTH_URL, params=params).prepare().url
|
request_url = requests.Request(
|
||||||
|
'GET', AUTH_URL, params=params).prepare().url
|
||||||
return RedirectResponse(request_url)
|
return RedirectResponse(request_url)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/callback')
|
@router.get('/callback')
|
||||||
def callback(code: str, session: Session = Depends(get_session)):
|
def callback(code: str, session: Session = Depends(get_session)):
|
||||||
data = {
|
data = {
|
||||||
@@ -85,7 +88,9 @@ def callback(code: str, session: Session = Depends(get_session)):
|
|||||||
|
|
||||||
id_token = token_data['id_token']
|
id_token = token_data['id_token']
|
||||||
decoded_token = jwt.decode(id_token, options={'verify_signature': False})
|
decoded_token = jwt.decode(id_token, options={'verify_signature': False})
|
||||||
decoded_access_token = jwt.decode(token_data['access_token'], options={'verify_signature': False})
|
decoded_access_token = jwt.decode(
|
||||||
|
token_data['access_token'], options={
|
||||||
|
'verify_signature': False})
|
||||||
resource_access = decoded_access_token.get('resource_access')
|
resource_access = decoded_access_token.get('resource_access')
|
||||||
if not resource_access:
|
if not resource_access:
|
||||||
data = {
|
data = {
|
||||||
@@ -141,6 +146,7 @@ def callback(code: str, session: Session = Depends(get_session)):
|
|||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def verify_token(token: str):
|
def verify_token(token: str):
|
||||||
try:
|
try:
|
||||||
signing_key = jwk_client.get_signing_key_from_jwt(token)
|
signing_key = jwk_client.get_signing_key_from_jwt(token)
|
||||||
@@ -154,28 +160,37 @@ def verify_token(token: str):
|
|||||||
)
|
)
|
||||||
return decoded
|
return decoded
|
||||||
except jwt.ExpiredSignatureError:
|
except jwt.ExpiredSignatureError:
|
||||||
raise HTTPException(status_code=401, detail=messages.Messages.tokenexipired)
|
raise HTTPException(status_code=401,
|
||||||
|
detail=messages.Messages.tokenexipired)
|
||||||
except jwt.InvalidTokenError:
|
except jwt.InvalidTokenError:
|
||||||
raise HTTPException(status_code=401, detail=messages.Messages.invalidtoken)
|
raise HTTPException(
|
||||||
|
status_code=401,
|
||||||
|
detail=messages.Messages.invalidtoken)
|
||||||
|
|
||||||
|
|
||||||
def get_current_user(request: Request, session: Session = Depends(get_session)):
|
def get_current_user(
|
||||||
|
request: Request,
|
||||||
|
session: Session = Depends(get_session)):
|
||||||
access_token = request.cookies.get('access_token')
|
access_token = request.cookies.get('access_token')
|
||||||
if not access_token:
|
if not access_token:
|
||||||
raise HTTPException(status_code=401, detail=messages.Messages.notauthenticated)
|
raise HTTPException(status_code=401,
|
||||||
|
detail=messages.Messages.notauthenticated)
|
||||||
payload = verify_token(access_token)
|
payload = verify_token(access_token)
|
||||||
if not payload:
|
if not payload:
|
||||||
raise HTTPException(status_code=401, detail='aze')
|
raise HTTPException(status_code=401, detail='aze')
|
||||||
email = payload.get('email')
|
email = payload.get('email')
|
||||||
|
|
||||||
if not email:
|
if not email:
|
||||||
raise HTTPException(status_code=401, detail=messages.Messages.notauthenticated)
|
raise HTTPException(status_code=401,
|
||||||
|
detail=messages.Messages.notauthenticated)
|
||||||
|
|
||||||
user = session.exec(select(User).where(User.email == email)).first()
|
user = session.exec(select(User).where(User.email == email)).first()
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=401, detail=messages.Messages.not_found('user'))
|
raise HTTPException(status_code=401,
|
||||||
|
detail=messages.Messages.not_found('user'))
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
@router.post('/refresh')
|
@router.post('/refresh')
|
||||||
def refresh_token(refresh_token: Annotated[str | None, Cookie()] = None):
|
def refresh_token(refresh_token: Annotated[str | None, Cookie()] = None):
|
||||||
refresh = refresh_token
|
refresh = refresh_token
|
||||||
@@ -223,6 +238,7 @@ def refresh_token(refresh_token: Annotated[str | None, Cookie()] = None):
|
|||||||
)
|
)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
@router.get('/user/me')
|
@router.get('/user/me')
|
||||||
def me(user: UserPublic = Depends(get_current_user)):
|
def me(user: UserPublic = Depends(get_current_user)):
|
||||||
if not user:
|
if not user:
|
||||||
|
|||||||
@@ -1,18 +1,27 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
"""Router for contract resource"""
|
||||||
from fastapi.responses import StreamingResponse
|
|
||||||
from src.database import get_session
|
|
||||||
from sqlmodel import Session
|
|
||||||
from src.contracts.generate_contract import generate_html_contract, generate_recap
|
|
||||||
from src.auth.auth import get_current_user
|
|
||||||
import src.models as models
|
|
||||||
import src.messages as messages
|
|
||||||
import src.contracts.service as service
|
|
||||||
import src.forms.service as form_service
|
|
||||||
import io
|
import io
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
|
import src.contracts.service as service
|
||||||
|
import src.forms.service as form_service
|
||||||
|
import src.messages as messages
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from fastapi.responses import StreamingResponse
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
|
from src.auth.auth import get_current_user
|
||||||
|
from src.contracts.generate_contract import (generate_html_contract,
|
||||||
|
generate_recap)
|
||||||
|
from src.database import get_session
|
||||||
|
|
||||||
router = APIRouter(prefix='/contracts')
|
router = APIRouter(prefix='/contracts')
|
||||||
|
|
||||||
def compute_recurrent_prices(products_quantities: list[dict], nb_shipment: int):
|
|
||||||
|
def compute_recurrent_prices(
|
||||||
|
products_quantities: list[dict],
|
||||||
|
nb_shipment: int
|
||||||
|
):
|
||||||
|
"""Compute price for recurrent products"""
|
||||||
result = 0
|
result = 0
|
||||||
for product_quantity in products_quantities:
|
for product_quantity in products_quantities:
|
||||||
product = product_quantity['product']
|
product = product_quantity['product']
|
||||||
@@ -20,25 +29,45 @@ def compute_recurrent_prices(products_quantities: list[dict], nb_shipment: int):
|
|||||||
result += compute_product_price(product, quantity, nb_shipment)
|
result += compute_product_price(product, quantity, nb_shipment)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def compute_occasional_prices(occasionals: list[dict]):
|
def compute_occasional_prices(occasionals: list[dict]):
|
||||||
|
"""Compute prices for occassional products"""
|
||||||
result = 0
|
result = 0
|
||||||
for occasional in occasionals:
|
for occasional in occasionals:
|
||||||
result += occasional['price']
|
result += occasional['price']
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def compute_product_price(product: models.Product, quantity: int, nb_shipment: int = 1):
|
|
||||||
product_quantity_unit = 1 if product.unit == models.Unit.KILO else 1000
|
def compute_product_price(
|
||||||
final_quantity = quantity if product.price else quantity / product_quantity_unit
|
product: models.Product,
|
||||||
final_price = product.price if product.price else product.price_kg
|
quantity: int,
|
||||||
|
nb_shipment: int = 1
|
||||||
|
):
|
||||||
|
"""Compute price for a product"""
|
||||||
|
product_quantity_unit = (
|
||||||
|
1 if product.unit == models.Unit.KILO else 1000
|
||||||
|
)
|
||||||
|
final_quantity = (
|
||||||
|
quantity if product.price else quantity / product_quantity_unit
|
||||||
|
)
|
||||||
|
final_price = (
|
||||||
|
product.price if product.price else product.price_kg
|
||||||
|
)
|
||||||
return final_price * final_quantity * nb_shipment
|
return final_price * final_quantity * nb_shipment
|
||||||
|
|
||||||
|
|
||||||
def find_dict_in_list(lst, key, value):
|
def find_dict_in_list(lst, key, value):
|
||||||
|
"""Find the index of a dictionnary in a list of dictionnaries given a key
|
||||||
|
and a value.
|
||||||
|
"""
|
||||||
for i, dic in enumerate(lst):
|
for i, dic in enumerate(lst):
|
||||||
if dic[key].id == value:
|
if dic[key].id == value:
|
||||||
return i
|
return i
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
|
||||||
def create_occasional_dict(contract_products: list[models.ContractProduct]):
|
def create_occasional_dict(contract_products: list[models.ContractProduct]):
|
||||||
|
"""Create a dictionnary of occasional products"""
|
||||||
result = []
|
result = []
|
||||||
for contract_product in contract_products:
|
for contract_product in contract_products:
|
||||||
existing_id = find_dict_in_list(
|
existing_id = find_dict_in_list(
|
||||||
@@ -69,18 +98,46 @@ def create_occasional_dict(contract_products: list[models.ContractProduct]):
|
|||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('')
|
@router.post('')
|
||||||
async def create_contract(
|
async def create_contract(
|
||||||
contract: models.ContractCreate,
|
contract: models.ContractCreate,
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
):
|
):
|
||||||
|
"""Create contract route"""
|
||||||
new_contract = service.create_one(session, contract)
|
new_contract = service.create_one(session, contract)
|
||||||
occasional_contract_products = list(filter(lambda contract_product: contract_product.product.type == models.ProductType.OCCASIONAL, new_contract.products))
|
occasional_contract_products = list(
|
||||||
|
filter(
|
||||||
|
lambda contract_product: (
|
||||||
|
contract_product.product.type == models.ProductType.OCCASIONAL
|
||||||
|
),
|
||||||
|
new_contract.products
|
||||||
|
)
|
||||||
|
)
|
||||||
occasionals = create_occasional_dict(occasional_contract_products)
|
occasionals = create_occasional_dict(occasional_contract_products)
|
||||||
recurrents = list(map(lambda x: {"product": x.product, "quantity": x.quantity}, filter(lambda contract_product: contract_product.product.type == models.ProductType.RECCURENT, new_contract.products)))
|
recurrents = list(
|
||||||
recurrent_price = compute_recurrent_prices(recurrents, len(new_contract.form.shipments))
|
map(
|
||||||
|
lambda x: {'product': x.product, 'quantity': x.quantity},
|
||||||
|
filter(
|
||||||
|
lambda contract_product: (
|
||||||
|
contract_product.product.type ==
|
||||||
|
models.ProductType.RECCURENT
|
||||||
|
),
|
||||||
|
new_contract.products
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
recurrent_price = compute_recurrent_prices(
|
||||||
|
recurrents,
|
||||||
|
len(new_contract.form.shipments)
|
||||||
|
)
|
||||||
price = recurrent_price + compute_occasional_prices(occasionals)
|
price = recurrent_price + compute_occasional_prices(occasionals)
|
||||||
cheques = list(map(lambda x: {"name": x.name, "value": x.value}, new_contract.cheques))
|
cheques = list(
|
||||||
|
map(
|
||||||
|
lambda x: {'name': x.name, 'value': x.value},
|
||||||
|
new_contract.cheques
|
||||||
|
)
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
pdf_bytes = generate_html_contract(
|
pdf_bytes = generate_html_contract(
|
||||||
new_contract,
|
new_contract,
|
||||||
@@ -91,43 +148,63 @@ async def create_contract(
|
|||||||
'{:10.2f}'.format(price)
|
'{:10.2f}'.format(price)
|
||||||
)
|
)
|
||||||
pdf_file = io.BytesIO(pdf_bytes)
|
pdf_file = io.BytesIO(pdf_bytes)
|
||||||
contract_id = f'{new_contract.firstname}_{new_contract.lastname}_{new_contract.form.productor.type}_{new_contract.form.season}'
|
contract_id = (
|
||||||
|
f'{new_contract.firstname}_'
|
||||||
|
f'{new_contract.lastname}_'
|
||||||
|
f'{new_contract.form.productor.type}_'
|
||||||
|
f'{new_contract.form.season}'
|
||||||
|
)
|
||||||
service.add_contract_file(session, new_contract.id, pdf_bytes, price)
|
service.add_contract_file(session, new_contract.id, pdf_bytes, price)
|
||||||
except Exception:
|
except Exception as error:
|
||||||
raise HTTPException(status_code=400, detail=messages.pdferror)
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail=messages.pdferror
|
||||||
|
) from error
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
pdf_file,
|
pdf_file,
|
||||||
media_type='application/pdf',
|
media_type='application/pdf',
|
||||||
headers={
|
headers={
|
||||||
'Content-Disposition': f'attachment; filename=contract_{contract_id}.pdf'
|
'Content-Disposition': (
|
||||||
|
f'attachment; filename=contract_{contract_id}.pdf'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{form_id}/base')
|
@router.get('/{form_id}/base')
|
||||||
async def get_base_contract_template(
|
async def get_base_contract_template(
|
||||||
form_id: int,
|
form_id: int,
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
):
|
):
|
||||||
|
"""Get contract template route"""
|
||||||
form = form_service.get_one(session, form_id)
|
form = form_service.get_one(session, form_id)
|
||||||
recurrents = list(map(lambda x: {"product": x, "quantity": None}, filter(lambda product: product.type == models.ProductType.RECCURENT, form.productor.products)))
|
recurrents = [
|
||||||
|
{'product': product, 'quantity': None}
|
||||||
|
for product in form.productor.products
|
||||||
|
if product.type == models.ProductType.RECCURENT
|
||||||
|
]
|
||||||
occasionals = [{
|
occasionals = [{
|
||||||
'shipment': sh,
|
'shipment': sh,
|
||||||
'price': None,
|
'price': None,
|
||||||
'products': [{'product': pr, 'quantity': None} for pr in sh.products]
|
'products': [{'product': pr, 'quantity': None} for pr in sh.products]
|
||||||
} for sh in form.shipments]
|
} for sh in form.shipments]
|
||||||
empty_contract = models.ContractPublic(
|
empty_contract = models.ContractPublic(
|
||||||
firstname="",
|
firstname='',
|
||||||
form=form,
|
form=form,
|
||||||
lastname="",
|
lastname='',
|
||||||
email="",
|
email='',
|
||||||
phone="",
|
phone='',
|
||||||
products=[],
|
products=[],
|
||||||
payment_method="cheque",
|
payment_method='cheque',
|
||||||
cheque_quantity=3,
|
cheque_quantity=3,
|
||||||
total_price=0,
|
total_price=0,
|
||||||
id=1
|
id=1
|
||||||
)
|
)
|
||||||
cheques = [{"name": None, "value": None}, {"name": None, "value": None}, {"name": None, "value": None}]
|
cheques = [
|
||||||
|
{'name': None, 'value': None},
|
||||||
|
{'name': None, 'value': None},
|
||||||
|
{'name': None, 'value': None}
|
||||||
|
]
|
||||||
try:
|
try:
|
||||||
pdf_bytes = generate_html_contract(
|
pdf_bytes = generate_html_contract(
|
||||||
empty_contract,
|
empty_contract,
|
||||||
@@ -136,38 +213,60 @@ async def get_base_contract_template(
|
|||||||
recurrents,
|
recurrents,
|
||||||
)
|
)
|
||||||
pdf_file = io.BytesIO(pdf_bytes)
|
pdf_file = io.BytesIO(pdf_bytes)
|
||||||
contract_id = f'{empty_contract.form.productor.type}_{empty_contract.form.season}'
|
contract_id = (
|
||||||
except Exception as e:
|
f'{empty_contract.form.productor.type}_'
|
||||||
print(e)
|
f'{empty_contract.form.season}'
|
||||||
raise HTTPException(status_code=400, detail=messages.pdferror)
|
)
|
||||||
|
except Exception as error:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400,
|
||||||
|
detail=messages.pdferror
|
||||||
|
) from error
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
pdf_file,
|
pdf_file,
|
||||||
media_type='application/pdf',
|
media_type='application/pdf',
|
||||||
headers={
|
headers={
|
||||||
'Content-Disposition': f'attachment; filename=contract_{contract_id}.pdf'
|
'Content-Disposition': (
|
||||||
|
f'attachment; filename=contract_{contract_id}.pdf'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('', response_model=list[models.ContractPublic])
|
@router.get('', response_model=list[models.ContractPublic])
|
||||||
def get_contracts(
|
def get_contracts(
|
||||||
forms: list[str] = Query([]),
|
forms: list[str] = Query([]),
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
user: models.User = Depends(get_current_user)
|
user: models.User = Depends(get_current_user)
|
||||||
):
|
):
|
||||||
|
"""Get all contracts route"""
|
||||||
return service.get_all(session, user, forms)
|
return service.get_all(session, user, forms)
|
||||||
|
|
||||||
@router.get('/{id}/file')
|
|
||||||
|
@router.get('/{_id}/file')
|
||||||
def get_contract_file(
|
def get_contract_file(
|
||||||
id: int,
|
_id: int,
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
user: models.User = Depends(get_current_user)
|
user: models.User = Depends(get_current_user)
|
||||||
):
|
):
|
||||||
if not service.is_allowed(session, user, id):
|
"""Get a contract file (in pdf) route"""
|
||||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contract', 'get'))
|
if not service.is_allowed(session, user, _id):
|
||||||
contract = service.get_one(session, id)
|
raise HTTPException(
|
||||||
|
status_code=403,
|
||||||
|
detail=messages.Messages.not_allowed('contract', 'get')
|
||||||
|
)
|
||||||
|
contract = service.get_one(session, _id)
|
||||||
if contract is None:
|
if contract is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('contract'))
|
raise HTTPException(
|
||||||
filename = f'{contract.form.name.replace(' ', '_')}_{contract.form.season}_{contract.firstname}-{contract.lastname}'
|
status_code=404,
|
||||||
|
detail=messages.Messages.not_found('contract')
|
||||||
|
)
|
||||||
|
filename = (
|
||||||
|
f'{contract.form.name.replace(' ', '_')}_'
|
||||||
|
f'{contract.form.season}_'
|
||||||
|
f'{contract.firstname}_'
|
||||||
|
f'{contract.lastname}'
|
||||||
|
)
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
io.BytesIO(contract.file),
|
io.BytesIO(contract.file),
|
||||||
media_type='application/pdf',
|
media_type='application/pdf',
|
||||||
@@ -176,23 +275,37 @@ def get_contract_file(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{form_id}/files')
|
@router.get('/{form_id}/files')
|
||||||
def get_contract_files(
|
def get_contract_files(
|
||||||
form_id: int,
|
form_id: int,
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
user: models.User = Depends(get_current_user)
|
user: models.User = Depends(get_current_user)
|
||||||
):
|
):
|
||||||
|
"""Get all contract files for a given form"""
|
||||||
if not form_service.is_allowed(session, user, form_id):
|
if not form_service.is_allowed(session, user, form_id):
|
||||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contracts', 'get'))
|
raise HTTPException(
|
||||||
|
status_code=403,
|
||||||
|
detail=messages.Messages.not_allowed('contracts', 'get')
|
||||||
|
)
|
||||||
form = form_service.get_one(session, form_id=form_id)
|
form = form_service.get_one(session, form_id=form_id)
|
||||||
contracts = service.get_all(session, user, forms=[form.name])
|
contracts = service.get_all(session, user, forms=[form.name])
|
||||||
zipped_contracts = io.BytesIO()
|
zipped_contracts = io.BytesIO()
|
||||||
with zipfile.ZipFile(zipped_contracts, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
|
with zipfile.ZipFile(
|
||||||
|
zipped_contracts,
|
||||||
|
'a',
|
||||||
|
zipfile.ZIP_DEFLATED,
|
||||||
|
False
|
||||||
|
) as zip_file:
|
||||||
for contract in contracts:
|
for contract in contracts:
|
||||||
contract_filename = f'{contract.form.name.replace(' ', '_')}_{contract.form.season}_{contract.firstname}-{contract.lastname}.pdf'
|
contract_filename = (
|
||||||
|
f'{contract.form.name.replace(' ', '_')}_'
|
||||||
|
f'{contract.form.season}_'
|
||||||
|
f'{contract.firstname}_'
|
||||||
|
f'{contract.lastname}'
|
||||||
|
)
|
||||||
zip_file.writestr(contract_filename, contract.file)
|
zip_file.writestr(contract_filename, contract.file)
|
||||||
|
filename = f'{form.name.replace(' ', '_')}_{form.season}'
|
||||||
filename = f'{form.name.replace(" ", "_")}_{form.season}'
|
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
io.BytesIO(zipped_contracts.getvalue()),
|
io.BytesIO(zipped_contracts.getvalue()),
|
||||||
media_type='application/zip',
|
media_type='application/zip',
|
||||||
@@ -201,39 +314,69 @@ def get_contract_files(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{form_id}/recap')
|
@router.get('/{form_id}/recap')
|
||||||
def get_contract_recap(
|
def get_contract_recap(
|
||||||
form_id: int,
|
form_id: int,
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
user: models.User = Depends(get_current_user)
|
user: models.User = Depends(get_current_user)
|
||||||
):
|
):
|
||||||
|
"""Get a contract recap for a given form"""
|
||||||
if not form_service.is_allowed(session, user, form_id):
|
if not form_service.is_allowed(session, user, form_id):
|
||||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contract recap', 'get'))
|
raise HTTPException(
|
||||||
|
status_code=403,
|
||||||
|
detail=messages.Messages.not_allowed('contract recap', 'get')
|
||||||
|
)
|
||||||
form = form_service.get_one(session, form_id=form_id)
|
form = form_service.get_one(session, form_id=form_id)
|
||||||
contracts = service.get_all(session, user, forms=[form.name])
|
contracts = service.get_all(session, user, forms=[form.name])
|
||||||
|
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
io.BytesIO(generate_recap(contracts, form)),
|
io.BytesIO(generate_recap(contracts, form)),
|
||||||
media_type='application/zip',
|
media_type='application/zip',
|
||||||
headers={
|
headers={
|
||||||
'Content-Disposition': f'attachment; filename=filename.ods'
|
'Content-Disposition': (
|
||||||
|
'attachment; filename=filename.ods'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@router.get('/{id}', response_model=models.ContractPublic)
|
|
||||||
def get_contract(id: int, session: Session = Depends(get_session), user: models.User = Depends(get_current_user)):
|
@router.get('/{_id}', response_model=models.ContractPublic)
|
||||||
if not service.is_allowed(session, user, id):
|
def get_contract(
|
||||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contract', 'get'))
|
_id: int,
|
||||||
result = service.get_one(session, id)
|
session: Session = Depends(get_session),
|
||||||
|
user: models.User = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
"""Get a contract route"""
|
||||||
|
if not service.is_allowed(session, user, _id):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=403,
|
||||||
|
detail=messages.Messages.not_allowed('contract', 'get')
|
||||||
|
)
|
||||||
|
result = service.get_one(session, _id)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('contract'))
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=messages.Messages.not_found('contract')
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@router.delete('/{id}', response_model=models.ContractPublic)
|
|
||||||
def delete_contract(id: int, session: Session = Depends(get_session), user: models.User = Depends(get_current_user)):
|
@router.delete('/{_id}', response_model=models.ContractPublic)
|
||||||
if not service.is_allowed(session, user, id):
|
def delete_contract(
|
||||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contract', 'delete'))
|
_id: int,
|
||||||
result = service.delete_one(session, id)
|
session: Session = Depends(get_session),
|
||||||
|
user: models.User = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
"""Delete contract route"""
|
||||||
|
if not service.is_allowed(session, user, _id):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=403,
|
||||||
|
detail=messages.Messages.not_allowed('contract', 'delete')
|
||||||
|
)
|
||||||
|
result = service.delete_one(session, _id)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('contract'))
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=messages.Messages.not_found('contract')
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
|
|
||||||
|
import html
|
||||||
|
import io
|
||||||
|
import pathlib
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
import src.models as models
|
from odfdo import Cell, Document, Row, Table
|
||||||
import html
|
from src import models
|
||||||
from weasyprint import HTML
|
from weasyprint import HTML
|
||||||
import io
|
|
||||||
|
|
||||||
import pathlib
|
|
||||||
|
|
||||||
def generate_html_contract(
|
def generate_html_contract(
|
||||||
contract: models.Contract,
|
contract: models.Contract,
|
||||||
@@ -17,7 +19,8 @@ def generate_html_contract(
|
|||||||
):
|
):
|
||||||
template_dir = pathlib.Path("./src/contracts/templates").resolve()
|
template_dir = pathlib.Path("./src/contracts/templates").resolve()
|
||||||
template_loader = jinja2.FileSystemLoader(searchpath=template_dir)
|
template_loader = jinja2.FileSystemLoader(searchpath=template_dir)
|
||||||
template_env = jinja2.Environment(loader=template_loader, autoescape=jinja2.select_autoescape(["html", "xml"]))
|
template_env = jinja2.Environment(
|
||||||
|
loader=template_loader, autoescape=jinja2.select_autoescape(["html", "xml"]))
|
||||||
template_file = "layout.html"
|
template_file = "layout.html"
|
||||||
template = template_env.get_template(template_file)
|
template = template_env.get_template(template_file)
|
||||||
output_text = template.render(
|
output_text = template.render(
|
||||||
@@ -28,41 +31,36 @@ def generate_html_contract(
|
|||||||
referer_email=contract.form.referer.email,
|
referer_email=contract.form.referer.email,
|
||||||
productor_name=contract.form.productor.name,
|
productor_name=contract.form.productor.name,
|
||||||
productor_address=contract.form.productor.address,
|
productor_address=contract.form.productor.address,
|
||||||
payment_methods_map={"cheque": "Ordre du chèque", "transfer": "virements"},
|
payment_methods_map={
|
||||||
|
"cheque": "Ordre du chèque",
|
||||||
|
"transfer": "virements"},
|
||||||
productor_payment_methods=contract.form.productor.payment_methods,
|
productor_payment_methods=contract.form.productor.payment_methods,
|
||||||
member_name=f'{html.escape(contract.firstname)} {html.escape(contract.lastname)}',
|
member_name=f'{
|
||||||
member_email=html.escape(contract.email),
|
html.escape(
|
||||||
member_phone=html.escape(contract.phone),
|
contract.firstname)} {
|
||||||
|
html.escape(
|
||||||
|
contract.lastname)}',
|
||||||
|
member_email=html.escape(
|
||||||
|
contract.email),
|
||||||
|
member_phone=html.escape(
|
||||||
|
contract.phone),
|
||||||
contract_start_date=contract.form.start,
|
contract_start_date=contract.form.start,
|
||||||
contract_end_date=contract.form.end,
|
contract_end_date=contract.form.end,
|
||||||
occasionals=occasionals,
|
occasionals=occasionals,
|
||||||
recurrents=reccurents,
|
recurrents=reccurents,
|
||||||
recurrent_price=recurrent_price,
|
recurrent_price=recurrent_price,
|
||||||
total_price=total_price,
|
total_price=total_price,
|
||||||
contract_payment_method={"cheque": "chèque", "transfer": "virements"}[contract.payment_method],
|
contract_payment_method={
|
||||||
cheques=cheques
|
"cheque": "chèque",
|
||||||
)
|
"transfer": "virements"}[
|
||||||
# options = {
|
contract.payment_method],
|
||||||
# 'page-size': 'Letter',
|
cheques=cheques)
|
||||||
# 'margin-top': '0.5in',
|
|
||||||
# 'margin-right': '0.5in',
|
|
||||||
# 'margin-bottom': '0.5in',
|
|
||||||
# 'margin-left': '0.5in',
|
|
||||||
# 'encoding': "UTF-8",
|
|
||||||
# 'print-media-type': True,
|
|
||||||
# "disable-javascript": True,
|
|
||||||
# "disable-external-links": True,
|
|
||||||
# 'enable-local-file-access': False,
|
|
||||||
# "disable-local-file-access": True,
|
|
||||||
# "no-images": True,
|
|
||||||
# }
|
|
||||||
|
|
||||||
return HTML(
|
return HTML(
|
||||||
string=output_text,
|
string=output_text,
|
||||||
base_url=template_dir,
|
base_url=template_dir,
|
||||||
).write_pdf()
|
).write_pdf()
|
||||||
|
|
||||||
from odfdo import Document, Table, Row, Cell
|
|
||||||
|
|
||||||
def generate_recap(
|
def generate_recap(
|
||||||
contracts: list[models.Contract],
|
contracts: list[models.Contract],
|
||||||
@@ -81,4 +79,3 @@ def generate_recap(
|
|||||||
doc.save(buffer)
|
doc.save(buffer)
|
||||||
|
|
||||||
return buffer.getvalue()
|
return buffer.getvalue()
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,57 @@
|
|||||||
|
"""Contract service responsible for read, create, update and delete contracts"""
|
||||||
|
from sqlalchemy.orm import selectinload
|
||||||
from sqlmodel import Session, select
|
from sqlmodel import Session, select
|
||||||
import src.models as models
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def get_all(
|
def get_all(
|
||||||
session: Session,
|
session: Session,
|
||||||
user: models.User,
|
user: models.User,
|
||||||
forms: list[str] = [],
|
forms: list[str] | None = None,
|
||||||
form_id: int | None = None,
|
form_id: int | None = None,
|
||||||
) -> list[models.ContractPublic]:
|
) -> list[models.ContractPublic]:
|
||||||
statement = select(models.Contract)\
|
"""Get all contracts"""
|
||||||
.join(models.Form, models.Contract.form_id == models.Form.id)\
|
statement = (
|
||||||
.join(models.Productor, models.Form.productor_id == models.Productor.id)\
|
select(models.Contract)
|
||||||
.where(models.Productor.type.in_([r.name for r in user.roles]))\
|
.join(
|
||||||
|
models.Form,
|
||||||
|
models.Contract.form_id == models.Form.id
|
||||||
|
)
|
||||||
|
.join(
|
||||||
|
models.Productor,
|
||||||
|
models.Form.productor_id == models.Productor.id
|
||||||
|
)
|
||||||
|
.where(
|
||||||
|
models.Productor.type.in_(
|
||||||
|
[r.name for r in user.roles]
|
||||||
|
)
|
||||||
|
)
|
||||||
.distinct()
|
.distinct()
|
||||||
if len(forms) > 0:
|
)
|
||||||
|
if forms:
|
||||||
statement = statement.where(models.Form.name.in_(forms))
|
statement = statement.where(models.Form.name.in_(forms))
|
||||||
if form_id:
|
if form_id:
|
||||||
statement = statement.where(models.Form.id == form_id)
|
statement = statement.where(models.Form.id == form_id)
|
||||||
return session.exec(statement.order_by(models.Contract.id)).all()
|
return session.exec(statement.order_by(models.Contract.id)).all()
|
||||||
|
|
||||||
def get_one(session: Session, contract_id: int) -> models.ContractPublic:
|
|
||||||
|
def get_one(
|
||||||
|
session: Session,
|
||||||
|
contract_id: int
|
||||||
|
) -> models.ContractPublic:
|
||||||
|
"""Get one contract"""
|
||||||
return session.get(models.Contract, contract_id)
|
return session.get(models.Contract, contract_id)
|
||||||
|
|
||||||
def create_one(session: Session, contract: models.ContractCreate) -> models.ContractPublic:
|
|
||||||
contract_create = contract.model_dump(exclude_unset=True, exclude=["products", "cheques"])
|
def create_one(
|
||||||
|
session: Session,
|
||||||
|
contract: models.ContractCreate
|
||||||
|
) -> models.ContractPublic:
|
||||||
|
"""Create one contract"""
|
||||||
|
contract_create = contract.model_dump(
|
||||||
|
exclude_unset=True,
|
||||||
|
exclude=["products", "cheques"]
|
||||||
|
)
|
||||||
new_contract = models.Contract(**contract_create)
|
new_contract = models.Contract(**contract_create)
|
||||||
|
|
||||||
new_contract.cheques = [
|
new_contract.cheques = [
|
||||||
@@ -45,10 +74,27 @@ def create_one(session: Session, contract: models.ContractCreate) -> models.Cont
|
|||||||
session.add(new_contract)
|
session.add(new_contract)
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(new_contract)
|
session.refresh(new_contract)
|
||||||
return new_contract
|
|
||||||
|
|
||||||
def add_contract_file(session: Session, id: int, file: bytes, price: float):
|
statement = (
|
||||||
statement = select(models.Contract).where(models.Contract.id == id)
|
select(models.Contract)
|
||||||
|
.where(models.Contract.id == new_contract.id)
|
||||||
|
.options(
|
||||||
|
selectinload(models.Contract.form)
|
||||||
|
.selectinload(models.Form.productor)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
return session.exec(statement).one()
|
||||||
|
|
||||||
|
|
||||||
|
def add_contract_file(
|
||||||
|
session: Session,
|
||||||
|
_id: int,
|
||||||
|
file: bytes,
|
||||||
|
price: float
|
||||||
|
):
|
||||||
|
"""Add a file to an existing contract"""
|
||||||
|
statement = select(models.Contract).where(models.Contract.id == _id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
contract = result.first()
|
contract = result.first()
|
||||||
contract.total_price = price
|
contract.total_price = price
|
||||||
@@ -58,8 +104,14 @@ def add_contract_file(session: Session, id: int, file: bytes, price: float):
|
|||||||
session.refresh(contract)
|
session.refresh(contract)
|
||||||
return contract
|
return contract
|
||||||
|
|
||||||
def update_one(session: Session, id: int, contract: models.ContractUpdate) -> models.ContractPublic:
|
|
||||||
statement = select(models.Contract).where(models.Contract.id == id)
|
def update_one(
|
||||||
|
session: Session,
|
||||||
|
_id: int,
|
||||||
|
contract: models.ContractUpdate
|
||||||
|
) -> models.ContractPublic:
|
||||||
|
"""Update one contract"""
|
||||||
|
statement = select(models.Contract).where(models.Contract.id == _id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
new_contract = result.first()
|
new_contract = result.first()
|
||||||
if not new_contract:
|
if not new_contract:
|
||||||
@@ -72,8 +124,13 @@ def update_one(session: Session, id: int, contract: models.ContractUpdate) -> mo
|
|||||||
session.refresh(new_contract)
|
session.refresh(new_contract)
|
||||||
return new_contract
|
return new_contract
|
||||||
|
|
||||||
def delete_one(session: Session, id: int) -> models.ContractPublic:
|
|
||||||
statement = select(models.Contract).where(models.Contract.id == id)
|
def delete_one(
|
||||||
|
session: Session,
|
||||||
|
_id: int
|
||||||
|
) -> models.ContractPublic:
|
||||||
|
"""Delete one contract"""
|
||||||
|
statement = select(models.Contract).where(models.Contract.id == _id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
contract = result.first()
|
contract = result.first()
|
||||||
if not contract:
|
if not contract:
|
||||||
@@ -83,11 +140,29 @@ def delete_one(session: Session, id: int) -> models.ContractPublic:
|
|||||||
session.commit()
|
session.commit()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def is_allowed(session: Session, user: models.User, id: int) -> bool:
|
|
||||||
statement = select(models.Contract)\
|
def is_allowed(
|
||||||
.join(models.Form, models.Contract.form_id == models.Form.id)\
|
session: Session,
|
||||||
.join(models.Productor, models.Form.productor_id == models.Productor.id)\
|
user: models.User,
|
||||||
.where(models.Contract.id == id)\
|
_id: int
|
||||||
.where(models.Productor.type.in_([r.name for r in user.roles]))\
|
) -> bool:
|
||||||
|
"""Determine if a user is allowed to access a contract by id"""
|
||||||
|
statement = (
|
||||||
|
select(models.Contract)
|
||||||
|
.join(
|
||||||
|
models.Form,
|
||||||
|
models.Contract.form_id == models.Form.id
|
||||||
|
)
|
||||||
|
.join(
|
||||||
|
models.Productor,
|
||||||
|
models.Form.productor_id == models.Productor.id
|
||||||
|
)
|
||||||
|
.where(models.Contract.id == _id)
|
||||||
|
.where(
|
||||||
|
models.Productor.type.in_(
|
||||||
|
[r.name for r in user.roles]
|
||||||
|
)
|
||||||
|
)
|
||||||
.distinct()
|
.distinct()
|
||||||
|
)
|
||||||
return len(session.exec(statement).all()) > 0
|
return len(session.exec(statement).all()) > 0
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
from sqlmodel import create_engine, SQLModel, Session
|
from sqlmodel import Session, SQLModel, create_engine
|
||||||
from src.settings import settings
|
from src.settings import settings
|
||||||
|
|
||||||
engine = create_engine(f'postgresql://{settings.db_user}:{settings.db_pass}@{settings.db_host}:5432/{settings.db_name}')
|
engine = create_engine(
|
||||||
|
f'postgresql://{settings.db_user}:{settings.db_pass}@{settings.db_host}:5432/{settings.db_name}')
|
||||||
|
|
||||||
|
|
||||||
def get_session():
|
def get_session():
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
yield session
|
yield session
|
||||||
|
|
||||||
|
|
||||||
def create_all_tables():
|
def create_all_tables():
|
||||||
SQLModel.metadata.create_all(engine)
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|||||||
@@ -1,16 +1,25 @@
|
|||||||
|
"""Forms module exceptions"""
|
||||||
|
import logging
|
||||||
|
|
||||||
class FormServiceError(Exception):
|
class FormServiceError(Exception):
|
||||||
|
"""Form service exception"""
|
||||||
def __init__(self, message: str):
|
def __init__(self, message: str):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
logging.error('FormService : %s', message)
|
||||||
|
|
||||||
|
|
||||||
class UserNotFoundError(FormServiceError):
|
class UserNotFoundError(FormServiceError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ProductorNotFoundError(FormServiceError):
|
class ProductorNotFoundError(FormServiceError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class FormNotFoundError(FormServiceError):
|
class FormNotFoundError(FormServiceError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class FormCreateError(FormServiceError):
|
class FormCreateError(FormServiceError):
|
||||||
def __init__(self, message: str, field: str | None = None):
|
def __init__(self, message: str, field: str | None = None):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
|
||||||
import src.messages as messages
|
|
||||||
import src.models as models
|
|
||||||
from src.database import get_session
|
|
||||||
from sqlmodel import Session
|
|
||||||
import src.forms.service as service
|
|
||||||
import src.forms.exceptions as exceptions
|
import src.forms.exceptions as exceptions
|
||||||
|
import src.forms.service as service
|
||||||
|
import src.messages as messages
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
from src.auth.auth import get_current_user
|
from src.auth.auth import get_current_user
|
||||||
|
from src.database import get_session
|
||||||
|
|
||||||
router = APIRouter(prefix='/forms')
|
router = APIRouter(prefix='/forms')
|
||||||
|
|
||||||
|
|
||||||
@router.get('', response_model=list[models.FormPublic])
|
@router.get('', response_model=list[models.FormPublic])
|
||||||
async def get_forms(
|
async def get_forms(
|
||||||
seasons: list[str] = Query([]),
|
seasons: list[str] = Query([]),
|
||||||
@@ -18,6 +19,7 @@ async def get_forms(
|
|||||||
):
|
):
|
||||||
return service.get_all(session, seasons, productors, current_season)
|
return service.get_all(session, seasons, productors, current_season)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/referents', response_model=list[models.FormPublic])
|
@router.get('/referents', response_model=list[models.FormPublic])
|
||||||
async def get_forms_filtered(
|
async def get_forms_filtered(
|
||||||
seasons: list[str] = Query([]),
|
seasons: list[str] = Query([]),
|
||||||
@@ -28,13 +30,18 @@ async def get_forms_filtered(
|
|||||||
):
|
):
|
||||||
return service.get_all(session, seasons, productors, current_season, user)
|
return service.get_all(session, seasons, productors, current_season, user)
|
||||||
|
|
||||||
@router.get('/{id}', response_model=models.FormPublic)
|
|
||||||
async def get_form(id: int, session: Session = Depends(get_session)):
|
@router.get('/{_id}', response_model=models.FormPublic)
|
||||||
result = service.get_one(session, id)
|
async def get_form(_id: int, session: Session = Depends(get_session)):
|
||||||
|
result = service.get_one(session, _id)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('form'))
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=messages.Messages.not_found('form')
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('', response_model=models.FormPublic)
|
@router.post('', response_model=models.FormPublic)
|
||||||
async def create_form(
|
async def create_form(
|
||||||
form: models.FormCreate,
|
form: models.FormCreate,
|
||||||
@@ -44,37 +51,39 @@ async def create_form(
|
|||||||
try:
|
try:
|
||||||
form = service.create_one(session, form)
|
form = service.create_one(session, form)
|
||||||
except exceptions.ProductorNotFoundError as error:
|
except exceptions.ProductorNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
except exceptions.UserNotFoundError as error:
|
except exceptions.UserNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
except exceptions.FormCreateError as error:
|
except exceptions.FormCreateError as error:
|
||||||
raise HTTPException(status_code=400, detail=str(error))
|
raise HTTPException(status_code=400, detail=str(error)) from error
|
||||||
return form
|
return form
|
||||||
|
|
||||||
@router.put('/{id}', response_model=models.FormPublic)
|
|
||||||
|
@router.put('/{_id}', response_model=models.FormPublic)
|
||||||
async def update_form(
|
async def update_form(
|
||||||
id: int, form: models.FormUpdate,
|
_id: int, form: models.FormUpdate,
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
session: Session = Depends(get_session)
|
session: Session = Depends(get_session)
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
result = service.update_one(session, id, form)
|
result = service.update_one(session, _id, form)
|
||||||
except exceptions.FormNotFoundError as error:
|
except exceptions.FormNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
except exceptions.ProductorNotFoundError as error:
|
except exceptions.ProductorNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
except exceptions.UserNotFoundError as error:
|
except exceptions.UserNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@router.delete('/{id}', response_model=models.FormPublic)
|
|
||||||
|
@router.delete('/{_id}', response_model=models.FormPublic)
|
||||||
async def delete_form(
|
async def delete_form(
|
||||||
id: int,
|
_id: int,
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
session: Session = Depends(get_session)
|
session: Session = Depends(get_session)
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
result = service.delete_one(session, id)
|
result = service.delete_one(session, _id)
|
||||||
except exceptions.FormNotFoundError as error:
|
except exceptions.FormNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
from sqlmodel import Session, select
|
|
||||||
from sqlalchemy import func
|
|
||||||
|
|
||||||
import src.models as models
|
|
||||||
import src.forms.exceptions as exceptions
|
import src.forms.exceptions as exceptions
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
|
from sqlalchemy import func
|
||||||
|
from sqlmodel import Session, select
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def get_all(
|
def get_all(
|
||||||
session: Session,
|
session: Session,
|
||||||
@@ -14,45 +14,54 @@ def get_all(
|
|||||||
) -> list[models.FormPublic]:
|
) -> list[models.FormPublic]:
|
||||||
statement = select(models.Form)
|
statement = select(models.Form)
|
||||||
if user:
|
if user:
|
||||||
statement = statement\
|
statement = statement .join(
|
||||||
.join(models.Productor, models.Form.productor_id == models.Productor.id)\
|
models.Productor,
|
||||||
.where(models.Productor.type.in_([r.name for r in user.roles]))\
|
models.Form.productor_id == models.Productor.id) .where(
|
||||||
.distinct()
|
models.Productor.type.in_(
|
||||||
|
[
|
||||||
|
r.name for r in user.roles])) .distinct()
|
||||||
if len(seasons) > 0:
|
if len(seasons) > 0:
|
||||||
statement = statement.where(models.Form.season.in_(seasons))
|
statement = statement.where(models.Form.season.in_(seasons))
|
||||||
if len(productors) > 0:
|
if len(productors) > 0:
|
||||||
statement = statement.join(models.Productor).where(models.Productor.name.in_(productors))
|
statement = statement.join(
|
||||||
|
models.Productor).where(
|
||||||
|
models.Productor.name.in_(productors))
|
||||||
if not user:
|
if not user:
|
||||||
statement = statement.where(models.Form.visible == True)
|
statement = statement.where(models.Form.visible)
|
||||||
if current_season:
|
if current_season:
|
||||||
subquery = (
|
subquery = (
|
||||||
select(
|
select(
|
||||||
models.Productor.type,
|
models.Productor.type,
|
||||||
func.max(models.Form.start).label("max_start")
|
func.max(models.Form.start).label("max_start")
|
||||||
)
|
)
|
||||||
.join(models.Form)\
|
.join(models.Form)
|
||||||
.group_by(models.Productor.type)\
|
.group_by(models.Productor.type)
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
statement = select(models.Form)\
|
statement = select(models.Form)\
|
||||||
.join(models.Productor)\
|
.join(models.Productor)\
|
||||||
.join(subquery,
|
.join(subquery,
|
||||||
(models.Productor.type == subquery.c.type) &
|
(models.Productor.type == subquery.c.type) &
|
||||||
(models.Form.start == subquery.c.max_start)
|
(models.Form.start == subquery.c.max_start)
|
||||||
)
|
)
|
||||||
if not user:
|
if not user:
|
||||||
statement = statement.where(models.Form.visible == True)
|
statement = statement.where(models.Form.visible)
|
||||||
return session.exec(statement.order_by(models.Form.name)).all()
|
return session.exec(statement.order_by(models.Form.name)).all()
|
||||||
return session.exec(statement.order_by(models.Form.name)).all()
|
return session.exec(statement.order_by(models.Form.name)).all()
|
||||||
|
|
||||||
|
|
||||||
def get_one(session: Session, form_id: int) -> models.FormPublic:
|
def get_one(session: Session, form_id: int) -> models.FormPublic:
|
||||||
return session.get(models.Form, form_id)
|
return session.get(models.Form, form_id)
|
||||||
|
|
||||||
|
|
||||||
def create_one(session: Session, form: models.FormCreate) -> models.FormPublic:
|
def create_one(session: Session, form: models.FormCreate) -> models.FormPublic:
|
||||||
if not form:
|
if not form:
|
||||||
raise exceptions.FormCreateError(messages.Messages.invalid_input('form', 'input cannot be None'))
|
raise exceptions.FormCreateError(
|
||||||
|
messages.Messages.invalid_input(
|
||||||
|
'form', 'input cannot be None'))
|
||||||
if not session.get(models.Productor, form.productor_id):
|
if not session.get(models.Productor, form.productor_id):
|
||||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
raise exceptions.ProductorNotFoundError(
|
||||||
|
messages.Messages.not_found('productor'))
|
||||||
if not session.get(models.User, form.referer_id):
|
if not session.get(models.User, form.referer_id):
|
||||||
raise exceptions.UserNotFoundError(messages.Messages.not_found('user'))
|
raise exceptions.UserNotFoundError(messages.Messages.not_found('user'))
|
||||||
form_create = form.model_dump(exclude_unset=True)
|
form_create = form.model_dump(exclude_unset=True)
|
||||||
@@ -62,14 +71,20 @@ def create_one(session: Session, form: models.FormCreate) -> models.FormPublic:
|
|||||||
session.refresh(new_form)
|
session.refresh(new_form)
|
||||||
return new_form
|
return new_form
|
||||||
|
|
||||||
def update_one(session: Session, id: int, form: models.FormUpdate) -> models.FormPublic:
|
|
||||||
statement = select(models.Form).where(models.Form.id == id)
|
def update_one(
|
||||||
|
session: Session,
|
||||||
|
_id: int,
|
||||||
|
form: models.FormUpdate) -> models.FormPublic:
|
||||||
|
statement = select(models.Form).where(models.Form.id == _id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
new_form = result.first()
|
new_form = result.first()
|
||||||
if not new_form:
|
if not new_form:
|
||||||
raise exceptions.FormNotFoundError(messages.Messages.not_found('form'))
|
raise exceptions.FormNotFoundError(messages.Messages.not_found('form'))
|
||||||
if form.productor_id and not session.get(models.Productor, form.productor_id):
|
if form.productor_id and not session.get(
|
||||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
models.Productor, form.productor_id):
|
||||||
|
raise exceptions.ProductorNotFoundError(
|
||||||
|
messages.Messages.not_found('productor'))
|
||||||
if form.referer_id and not session.get(models.User, form.referer_id):
|
if form.referer_id and not session.get(models.User, form.referer_id):
|
||||||
raise exceptions.UserNotFoundError(messages.Messages.not_found('user'))
|
raise exceptions.UserNotFoundError(messages.Messages.not_found('user'))
|
||||||
form_updates = form.model_dump(exclude_unset=True)
|
form_updates = form.model_dump(exclude_unset=True)
|
||||||
@@ -80,8 +95,9 @@ def update_one(session: Session, id: int, form: models.FormUpdate) -> models.For
|
|||||||
session.refresh(new_form)
|
session.refresh(new_form)
|
||||||
return new_form
|
return new_form
|
||||||
|
|
||||||
def delete_one(session: Session, id: int) -> models.FormPublic:
|
|
||||||
statement = select(models.Form).where(models.Form.id == id)
|
def delete_one(session: Session, _id: int) -> models.FormPublic:
|
||||||
|
statement = select(models.Form).where(models.Form.id == _id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
form = result.first()
|
form = result.first()
|
||||||
if not form:
|
if not form:
|
||||||
@@ -91,10 +107,19 @@ def delete_one(session: Session, id: int) -> models.FormPublic:
|
|||||||
session.commit()
|
session.commit()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def is_allowed(session: Session, user: models.User, id: int) -> bool:
|
|
||||||
statement = select(models.Form)\
|
def is_allowed(session: Session, user: models.User, _id: int) -> bool:
|
||||||
.join(models.Productor, models.Form.productor_id == models.Productor.id)\
|
statement = (
|
||||||
.where(models.Form.id == id)\
|
select(models.Form)
|
||||||
.where(models.Productor.type.in_([r.name for r in user.roles]))\
|
.join(
|
||||||
|
models.Productor,
|
||||||
|
models.Form.productor_id == models.Productor.id)
|
||||||
|
.where(models.Form.id == _id)
|
||||||
|
.where(
|
||||||
|
models.Productor.type.in_(
|
||||||
|
[r.name for r in user.roles]
|
||||||
|
)
|
||||||
|
)
|
||||||
.distinct()
|
.distinct()
|
||||||
|
)
|
||||||
return len(session.exec(statement).all()) > 0
|
return len(session.exec(statement).all()) > 0
|
||||||
@@ -1,18 +1,15 @@
|
|||||||
from sqlmodel import SQLModel
|
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from src.auth.auth import router as auth_router
|
||||||
from src.templates.templates import router as template_router
|
|
||||||
from src.contracts.contracts import router as contracts_router
|
from src.contracts.contracts import router as contracts_router
|
||||||
from src.forms.forms import router as forms_router
|
from src.forms.forms import router as forms_router
|
||||||
from src.productors.productors import router as productors_router
|
from src.productors.productors import router as productors_router
|
||||||
from src.products.products import router as products_router
|
from src.products.products import router as products_router
|
||||||
from src.users.users import router as users_router
|
|
||||||
from src.auth.auth import router as auth_router
|
|
||||||
from src.shipments.shipments import router as shipment_router
|
|
||||||
from src.settings import settings
|
from src.settings import settings
|
||||||
from src.database import engine, create_all_tables
|
from src.shipments.shipments import router as shipment_router
|
||||||
|
from src.templates.templates import router as template_router
|
||||||
|
from src.users.users import router as users_router
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
pdferror = 'An error occured during PDF generation please contact administrator'
|
pdferror = 'An error occured during PDF generation please contact administrator'
|
||||||
|
|
||||||
|
|
||||||
class Messages:
|
class Messages:
|
||||||
unauthorized = 'User is Unauthorized'
|
unauthorized = 'User is Unauthorized'
|
||||||
notauthenticated = 'User is not authenticated'
|
notauthenticated = 'User is not authenticated'
|
||||||
|
|||||||
@@ -1,99 +1,136 @@
|
|||||||
from sqlmodel import Field, SQLModel, Relationship, Column, LargeBinary
|
import datetime
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
import datetime
|
|
||||||
|
from sqlmodel import Column, Field, LargeBinary, Relationship, SQLModel
|
||||||
|
|
||||||
|
|
||||||
class ContractType(SQLModel, table=True):
|
class ContractType(SQLModel, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(
|
||||||
|
default=None,
|
||||||
|
primary_key=True
|
||||||
|
)
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class UserContractTypeLink(SQLModel, table=True):
|
class UserContractTypeLink(SQLModel, table=True):
|
||||||
user_id: int = Field(foreign_key="user.id", primary_key=True)
|
user_id: int = Field(
|
||||||
contract_type_id: int = Field(foreign_key="contracttype.id", primary_key=True)
|
foreign_key='user.id',
|
||||||
|
primary_key=True
|
||||||
|
)
|
||||||
|
contract_type_id: int = Field(
|
||||||
|
foreign_key='contracttype.id',
|
||||||
|
primary_key=True
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class UserBase(SQLModel):
|
class UserBase(SQLModel):
|
||||||
name: str
|
name: str
|
||||||
email: str
|
email: str
|
||||||
|
|
||||||
|
|
||||||
class UserPublic(UserBase):
|
class UserPublic(UserBase):
|
||||||
id: int
|
id: int
|
||||||
roles: list[ContractType]
|
roles: list[ContractType]
|
||||||
|
|
||||||
|
|
||||||
class User(UserBase, table=True):
|
class User(UserBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
roles: list[ContractType] = Relationship(
|
roles: list[ContractType] = Relationship(
|
||||||
link_model=UserContractTypeLink
|
link_model=UserContractTypeLink
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class UserUpdate(SQLModel):
|
class UserUpdate(SQLModel):
|
||||||
name: str | None
|
name: str | None
|
||||||
email: str | None
|
email: str | None
|
||||||
role_names: list[str] | None
|
role_names: list[str] | None
|
||||||
|
|
||||||
|
|
||||||
class UserCreate(UserBase):
|
class UserCreate(UserBase):
|
||||||
role_names: list[str] | None
|
role_names: list[str] | None
|
||||||
|
|
||||||
|
|
||||||
class PaymentMethodBase(SQLModel):
|
class PaymentMethodBase(SQLModel):
|
||||||
name: str
|
name: str
|
||||||
details: str
|
details: str
|
||||||
max: int | None
|
max: int | None
|
||||||
|
|
||||||
|
|
||||||
class PaymentMethod(PaymentMethodBase, table=True):
|
class PaymentMethod(PaymentMethodBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
productor_id: int = Field(foreign_key="productor.id", ondelete="CASCADE")
|
productor_id: int = Field(foreign_key='productor.id', ondelete='CASCADE')
|
||||||
productor: Optional["Productor"] = Relationship(
|
productor: Optional['Productor'] = Relationship(
|
||||||
back_populates="payment_methods",
|
back_populates='payment_methods',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class PaymentMethodPublic(PaymentMethodBase):
|
class PaymentMethodPublic(PaymentMethodBase):
|
||||||
id: int
|
id: int
|
||||||
productor: Optional["Productor"]
|
productor: Optional['Productor']
|
||||||
|
|
||||||
|
|
||||||
class ProductorBase(SQLModel):
|
class ProductorBase(SQLModel):
|
||||||
name: str
|
name: str
|
||||||
address: str
|
address: str
|
||||||
type: str
|
type: str
|
||||||
|
|
||||||
|
|
||||||
class ProductorPublic(ProductorBase):
|
class ProductorPublic(ProductorBase):
|
||||||
id: int
|
id: int
|
||||||
products: list["Product"] = []
|
products: list['Product'] = Field(default_factory=list)
|
||||||
payment_methods: list["PaymentMethod"] = []
|
payment_methods: list['PaymentMethod'] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
class Productor(ProductorBase, table=True):
|
class Productor(ProductorBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
|
|
||||||
products: list["Product"] = Relationship(
|
products: list['Product'] = Relationship(
|
||||||
back_populates='productor',
|
back_populates='productor',
|
||||||
sa_relationship_kwargs={
|
sa_relationship_kwargs={
|
||||||
"order_by": "Product.name"
|
'order_by': 'Product.name'
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
payment_methods: list["PaymentMethod"] = Relationship(
|
payment_methods: list['PaymentMethod'] = Relationship(
|
||||||
back_populates="productor",
|
back_populates='productor',
|
||||||
cascade_delete=True
|
cascade_delete=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ProductorUpdate(SQLModel):
|
class ProductorUpdate(SQLModel):
|
||||||
name: str | None
|
name: str | None
|
||||||
address: str | None
|
address: str | None
|
||||||
payment_methods: list["PaymentMethod"] = []
|
payment_methods: list['PaymentMethod'] = Field(default_factory=list)
|
||||||
type: str | None
|
type: str | None
|
||||||
|
|
||||||
|
|
||||||
class ProductorCreate(ProductorBase):
|
class ProductorCreate(ProductorBase):
|
||||||
payment_methods: list["PaymentMethod"] = []
|
payment_methods: list['PaymentMethod'] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
class Unit(StrEnum):
|
class Unit(StrEnum):
|
||||||
GRAMS = "1"
|
GRAMS = '1'
|
||||||
KILO = "2"
|
KILO = '2'
|
||||||
PIECE = "3"
|
PIECE = '3'
|
||||||
|
|
||||||
|
|
||||||
class ProductType(StrEnum):
|
class ProductType(StrEnum):
|
||||||
OCCASIONAL = "1"
|
OCCASIONAL = '1'
|
||||||
RECCURENT = "2"
|
RECCURENT = '2'
|
||||||
|
|
||||||
|
|
||||||
class ShipmentProductLink(SQLModel, table=True):
|
class ShipmentProductLink(SQLModel, table=True):
|
||||||
shipment_id: Optional[int] = Field(default=None, foreign_key="shipment.id", primary_key=True)
|
shipment_id: Optional[int] = Field(
|
||||||
product_id: Optional[int] = Field(default=None, foreign_key="product.id", primary_key=True)
|
default=None,
|
||||||
|
foreign_key='shipment.id',
|
||||||
|
primary_key=True
|
||||||
|
)
|
||||||
|
product_id: Optional[int] = Field(
|
||||||
|
default=None,
|
||||||
|
foreign_key='product.id',
|
||||||
|
primary_key=True
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ProductBase(SQLModel):
|
class ProductBase(SQLModel):
|
||||||
name: str
|
name: str
|
||||||
@@ -103,17 +140,31 @@ class ProductBase(SQLModel):
|
|||||||
quantity: float | None
|
quantity: float | None
|
||||||
quantity_unit: str | None
|
quantity_unit: str | None
|
||||||
type: ProductType
|
type: ProductType
|
||||||
productor_id: int | None = Field(default=None, foreign_key="productor.id")
|
productor_id: int | None = Field(
|
||||||
|
default=None,
|
||||||
|
foreign_key='productor.id'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ProductPublic(ProductBase):
|
class ProductPublic(ProductBase):
|
||||||
id: int
|
id: int
|
||||||
productor: Productor | None
|
productor: Productor | None
|
||||||
shipments: list["Shipment"] | None
|
shipments: list['Shipment'] | None
|
||||||
|
|
||||||
|
|
||||||
class Product(ProductBase, table=True):
|
class Product(ProductBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(
|
||||||
shipments: list["Shipment"] = Relationship(back_populates="products", link_model=ShipmentProductLink)
|
default=None,
|
||||||
productor: Optional[Productor] = Relationship(back_populates="products")
|
primary_key=True
|
||||||
|
)
|
||||||
|
shipments: list['Shipment'] = Relationship(
|
||||||
|
back_populates='products',
|
||||||
|
link_model=ShipmentProductLink
|
||||||
|
)
|
||||||
|
productor: Optional[Productor] = Relationship(
|
||||||
|
back_populates='products'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ProductUpdate(SQLModel):
|
class ProductUpdate(SQLModel):
|
||||||
name: str | None
|
name: str | None
|
||||||
@@ -125,41 +176,46 @@ class ProductUpdate(SQLModel):
|
|||||||
productor_id: int | None
|
productor_id: int | None
|
||||||
type: ProductType | None
|
type: ProductType | None
|
||||||
|
|
||||||
|
|
||||||
class ProductCreate(ProductBase):
|
class ProductCreate(ProductBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class FormBase(SQLModel):
|
class FormBase(SQLModel):
|
||||||
name: str
|
name: str
|
||||||
productor_id: int | None = Field(default=None, foreign_key="productor.id")
|
productor_id: int | None = Field(default=None, foreign_key='productor.id')
|
||||||
referer_id: int | None = Field(default=None, foreign_key="user.id")
|
referer_id: int | None = Field(default=None, foreign_key='user.id')
|
||||||
season: str
|
season: str
|
||||||
start: datetime.date
|
start: datetime.date
|
||||||
end: datetime.date
|
end: datetime.date
|
||||||
minimum_shipment_value: float | None
|
minimum_shipment_value: float | None
|
||||||
visible: bool
|
visible: bool
|
||||||
|
|
||||||
|
|
||||||
class FormPublic(FormBase):
|
class FormPublic(FormBase):
|
||||||
id: int
|
id: int
|
||||||
productor: ProductorPublic | None
|
productor: ProductorPublic | None
|
||||||
referer: User | None
|
referer: User | None
|
||||||
shipments: list["ShipmentPublic"] = []
|
shipments: list['ShipmentPublic'] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
class Form(FormBase, table=True):
|
class Form(FormBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
productor: Optional['Productor'] = Relationship()
|
productor: Optional['Productor'] = Relationship()
|
||||||
referer: Optional['User'] = Relationship()
|
referer: Optional['User'] = Relationship()
|
||||||
shipments: list["Shipment"] = Relationship(
|
shipments: list['Shipment'] = Relationship(
|
||||||
back_populates="form",
|
back_populates='form',
|
||||||
cascade_delete=True,
|
cascade_delete=True,
|
||||||
sa_relationship_kwargs={
|
sa_relationship_kwargs={
|
||||||
"order_by": "Shipment.name"
|
'order_by': 'Shipment.name'
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
contracts: list["Contract"] = Relationship(
|
contracts: list['Contract'] = Relationship(
|
||||||
back_populates="form",
|
back_populates='form',
|
||||||
cascade_delete=True
|
cascade_delete=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class FormUpdate(SQLModel):
|
class FormUpdate(SQLModel):
|
||||||
name: str | None
|
name: str | None
|
||||||
productor_id: int | None
|
productor_id: int | None
|
||||||
@@ -170,35 +226,44 @@ class FormUpdate(SQLModel):
|
|||||||
minimum_shipment_value: float | None
|
minimum_shipment_value: float | None
|
||||||
visible: bool | None
|
visible: bool | None
|
||||||
|
|
||||||
|
|
||||||
class FormCreate(FormBase):
|
class FormCreate(FormBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TemplateBase(SQLModel):
|
class TemplateBase(SQLModel):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TemplatePublic(TemplateBase):
|
class TemplatePublic(TemplateBase):
|
||||||
id: int
|
id: int
|
||||||
|
|
||||||
|
|
||||||
class Template(TemplateBase, table=True):
|
class Template(TemplateBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
|
|
||||||
|
|
||||||
class TemplateUpdate(SQLModel):
|
class TemplateUpdate(SQLModel):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class TemplateCreate(TemplateBase):
|
class TemplateCreate(TemplateBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ChequeBase(SQLModel):
|
class ChequeBase(SQLModel):
|
||||||
name: str
|
name: str
|
||||||
value: str
|
value: str
|
||||||
|
|
||||||
|
|
||||||
class Cheque(ChequeBase, table=True):
|
class Cheque(ChequeBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
contract_id: int = Field(foreign_key="contract.id", ondelete="CASCADE")
|
contract_id: int = Field(foreign_key='contract.id', ondelete='CASCADE')
|
||||||
contract: Optional["Contract"] = Relationship(
|
contract: Optional['Contract'] = Relationship(
|
||||||
back_populates="cheques",
|
back_populates='cheques',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ContractBase(SQLModel):
|
class ContractBase(SQLModel):
|
||||||
firstname: str
|
firstname: str
|
||||||
lastname: str
|
lastname: str
|
||||||
@@ -207,105 +272,122 @@ class ContractBase(SQLModel):
|
|||||||
payment_method: str
|
payment_method: str
|
||||||
cheque_quantity: int
|
cheque_quantity: int
|
||||||
|
|
||||||
|
|
||||||
class Contract(ContractBase, table=True):
|
class Contract(ContractBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
|
|
||||||
form_id: int = Field(
|
form_id: int = Field(
|
||||||
foreign_key="form.id",
|
foreign_key='form.id',
|
||||||
nullable=False,
|
nullable=False,
|
||||||
ondelete="CASCADE"
|
ondelete='CASCADE'
|
||||||
)
|
)
|
||||||
products: list["ContractProduct"] = Relationship(
|
products: list['ContractProduct'] = Relationship(
|
||||||
back_populates="contract",
|
back_populates='contract',
|
||||||
cascade_delete=True
|
cascade_delete=True
|
||||||
)
|
)
|
||||||
form: Optional[Form] = Relationship(back_populates="contracts")
|
form: Form = Relationship(back_populates='contracts')
|
||||||
cheques: list[Cheque] = Relationship(
|
cheques: list[Cheque] = Relationship(
|
||||||
back_populates="contract",
|
back_populates='contract',
|
||||||
cascade_delete=True
|
cascade_delete=True
|
||||||
)
|
)
|
||||||
file: bytes = Field(sa_column=Column(LargeBinary))
|
file: bytes = Field(sa_column=Column(LargeBinary))
|
||||||
total_price: float | None
|
total_price: float | None
|
||||||
|
|
||||||
|
|
||||||
class ContractCreate(ContractBase):
|
class ContractCreate(ContractBase):
|
||||||
products: list["ContractProductCreate"] = []
|
products: list['ContractProductCreate'] = Field(default_factory=list)
|
||||||
cheques: list["Cheque"] = []
|
cheques: list['Cheque'] = Field(default_factory=list)
|
||||||
form_id: int
|
form_id: int
|
||||||
|
|
||||||
|
|
||||||
class ContractUpdate(SQLModel):
|
class ContractUpdate(SQLModel):
|
||||||
file: bytes
|
file: bytes
|
||||||
|
|
||||||
|
|
||||||
class ContractPublic(ContractBase):
|
class ContractPublic(ContractBase):
|
||||||
id: int
|
id: int
|
||||||
products: list["ContractProduct"] = []
|
products: list['ContractProduct'] = Field(default_factory=list)
|
||||||
form: Form
|
form: Form
|
||||||
total_price: float | None
|
total_price: float | None
|
||||||
# file: bytes
|
# file: bytes
|
||||||
|
|
||||||
|
|
||||||
class ContractProductBase(SQLModel):
|
class ContractProductBase(SQLModel):
|
||||||
product_id: int = Field(
|
product_id: int = Field(
|
||||||
foreign_key="product.id",
|
foreign_key='product.id',
|
||||||
nullable=False,
|
nullable=False,
|
||||||
ondelete="CASCADE"
|
ondelete='CASCADE'
|
||||||
)
|
)
|
||||||
shipment_id: int | None = Field(
|
shipment_id: int | None = Field(
|
||||||
default=None,
|
default=None,
|
||||||
foreign_key="shipment.id",
|
foreign_key='shipment.id',
|
||||||
nullable=True,
|
nullable=True,
|
||||||
ondelete="CASCADE"
|
ondelete='CASCADE'
|
||||||
)
|
)
|
||||||
quantity: float
|
quantity: float
|
||||||
|
|
||||||
|
|
||||||
class ContractProduct(ContractProductBase, table=True):
|
class ContractProduct(ContractProductBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
contract_id: int = Field(
|
contract_id: int = Field(
|
||||||
foreign_key="contract.id",
|
foreign_key='contract.id',
|
||||||
nullable=False,
|
nullable=False,
|
||||||
ondelete="CASCADE"
|
ondelete='CASCADE'
|
||||||
)
|
)
|
||||||
contract: Optional["Contract"] = Relationship(back_populates="products")
|
contract: Optional['Contract'] = Relationship(back_populates='products')
|
||||||
product: Optional["Product"] = Relationship()
|
product: Optional['Product'] = Relationship()
|
||||||
shipment: Optional["Shipment"] = Relationship()
|
shipment: Optional['Shipment'] = Relationship()
|
||||||
|
|
||||||
|
|
||||||
class ContractProductPublic(ContractProductBase):
|
class ContractProductPublic(ContractProductBase):
|
||||||
id: int
|
id: int
|
||||||
quantity: float
|
quantity: float
|
||||||
contract: Contract
|
contract: Contract
|
||||||
product: Product
|
product: Product
|
||||||
shipment: Optional["Shipment"]
|
shipment: Optional['Shipment']
|
||||||
|
|
||||||
|
|
||||||
class ContractProductCreate(ContractProductBase):
|
class ContractProductCreate(ContractProductBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ContractProductUpdate(ContractProductBase):
|
class ContractProductUpdate(ContractProductBase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ShipmentBase(SQLModel):
|
class ShipmentBase(SQLModel):
|
||||||
name: str
|
name: str
|
||||||
date: datetime.date
|
date: datetime.date
|
||||||
form_id: int | None = Field(default=None, foreign_key="form.id", ondelete="CASCADE")
|
form_id: int | None = Field(
|
||||||
|
default=None,
|
||||||
|
foreign_key='form.id',
|
||||||
|
ondelete='CASCADE')
|
||||||
|
|
||||||
|
|
||||||
class ShipmentPublic(ShipmentBase):
|
class ShipmentPublic(ShipmentBase):
|
||||||
id: int
|
id: int
|
||||||
products: list[Product] = []
|
products: list[Product] = Field(default_factory=list)
|
||||||
form: Form | None
|
form: Form | None
|
||||||
|
|
||||||
|
|
||||||
class Shipment(ShipmentBase, table=True):
|
class Shipment(ShipmentBase, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
products: list[Product] = Relationship(
|
products: list[Product] = Relationship(
|
||||||
back_populates="shipments",
|
back_populates='shipments',
|
||||||
link_model=ShipmentProductLink,
|
link_model=ShipmentProductLink,
|
||||||
sa_relationship_kwargs={
|
sa_relationship_kwargs={
|
||||||
"order_by": "Product.name"
|
'order_by': 'Product.name'
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
form: Optional[Form] = Relationship(back_populates="shipments")
|
form: Optional[Form] = Relationship(back_populates='shipments')
|
||||||
|
|
||||||
|
|
||||||
class ShipmentUpdate(SQLModel):
|
class ShipmentUpdate(SQLModel):
|
||||||
name: str | None
|
name: str | None
|
||||||
date: datetime.date | None
|
date: datetime.date | None
|
||||||
product_ids: list[int] | None = []
|
product_ids: list[int] | None = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
class ShipmentCreate(ShipmentBase):
|
class ShipmentCreate(ShipmentBase):
|
||||||
product_ids: list[int] = []
|
product_ids: list[int] = Field(default_factory=list)
|
||||||
form_id: int
|
form_id: int
|
||||||
@@ -1,10 +1,16 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
class ProductorServiceError(Exception):
|
class ProductorServiceError(Exception):
|
||||||
def __init__(self, message: str):
|
def __init__(self, message: str):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
logging.error('ProductorService : %s', message)
|
||||||
|
|
||||||
|
|
||||||
class ProductorNotFoundError(ProductorServiceError):
|
class ProductorNotFoundError(ProductorServiceError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ProductorCreateError(ProductorServiceError):
|
class ProductorCreateError(ProductorServiceError):
|
||||||
def __init__(self, message: str, field: str | None = None):
|
def __init__(self, message: str, field: str | None = None):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
import src.models as models
|
|
||||||
from src.database import get_session
|
|
||||||
from sqlmodel import Session
|
|
||||||
import src.productors.service as service
|
|
||||||
import src.productors.exceptions as exceptions
|
import src.productors.exceptions as exceptions
|
||||||
|
import src.productors.service as service
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
from src.auth.auth import get_current_user
|
from src.auth.auth import get_current_user
|
||||||
|
from src.database import get_session
|
||||||
|
|
||||||
router = APIRouter(prefix='/productors')
|
router = APIRouter(prefix='/productors')
|
||||||
|
|
||||||
|
|
||||||
@router.get('', response_model=list[models.ProductorPublic])
|
@router.get('', response_model=list[models.ProductorPublic])
|
||||||
def get_productors(
|
def get_productors(
|
||||||
names: list[str] = Query([]),
|
names: list[str] = Query([]),
|
||||||
@@ -18,17 +19,22 @@ def get_productors(
|
|||||||
):
|
):
|
||||||
return service.get_all(session, user, names, types)
|
return service.get_all(session, user, names, types)
|
||||||
|
|
||||||
@router.get('/{id}', response_model=models.ProductorPublic)
|
|
||||||
|
@router.get('/{_id}', response_model=models.ProductorPublic)
|
||||||
def get_productor(
|
def get_productor(
|
||||||
id: int,
|
_id: int,
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
session: Session = Depends(get_session)
|
session: Session = Depends(get_session)
|
||||||
):
|
):
|
||||||
result = service.get_one(session, id)
|
result = service.get_one(session, _id)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('productor'))
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=messages.Messages.not_found('productor')
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('', response_model=models.ProductorPublic)
|
@router.post('', response_model=models.ProductorPublic)
|
||||||
def create_productor(
|
def create_productor(
|
||||||
productor: models.ProductorCreate,
|
productor: models.ProductorCreate,
|
||||||
@@ -38,29 +44,31 @@ def create_productor(
|
|||||||
try:
|
try:
|
||||||
result = service.create_one(session, productor)
|
result = service.create_one(session, productor)
|
||||||
except exceptions.ProductorCreateError as error:
|
except exceptions.ProductorCreateError as error:
|
||||||
raise HTTPException(status_code=400, detail=str(error))
|
raise HTTPException(status_code=400, detail=str(error)) from error
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@router.put('/{id}', response_model=models.ProductorPublic)
|
|
||||||
|
@router.put('/{_id}', response_model=models.ProductorPublic)
|
||||||
def update_productor(
|
def update_productor(
|
||||||
id: int, productor: models.ProductorUpdate,
|
_id: int, productor: models.ProductorUpdate,
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
session: Session = Depends(get_session)
|
session: Session = Depends(get_session)
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
result = service.update_one(session, id, productor)
|
result = service.update_one(session, _id, productor)
|
||||||
except exceptions.ProductorNotFoundError as error:
|
except exceptions.ProductorNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@router.delete('/{id}', response_model=models.ProductorPublic)
|
|
||||||
|
@router.delete('/{_id}', response_model=models.ProductorPublic)
|
||||||
def delete_productor(
|
def delete_productor(
|
||||||
id: int,
|
_id: int,
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
session: Session = Depends(get_session)
|
session: Session = Depends(get_session)
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
result = service.delete_one(session, id)
|
result = service.delete_one(session, _id)
|
||||||
except exceptions.ProductorNotFoundError as error:
|
except exceptions.ProductorNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from sqlmodel import Session, select
|
|
||||||
import src.models as models
|
|
||||||
import src.productors.exceptions as exceptions
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
|
import src.productors.exceptions as exceptions
|
||||||
|
from sqlmodel import Session, select
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def get_all(
|
def get_all(
|
||||||
session: Session,
|
session: Session,
|
||||||
@@ -18,13 +19,20 @@ def get_all(
|
|||||||
statement = statement.where(models.Productor.type.in_(types))
|
statement = statement.where(models.Productor.type.in_(types))
|
||||||
return session.exec(statement.order_by(models.Productor.name)).all()
|
return session.exec(statement.order_by(models.Productor.name)).all()
|
||||||
|
|
||||||
|
|
||||||
def get_one(session: Session, productor_id: int) -> models.ProductorPublic:
|
def get_one(session: Session, productor_id: int) -> models.ProductorPublic:
|
||||||
return session.get(models.Productor, productor_id)
|
return session.get(models.Productor, productor_id)
|
||||||
|
|
||||||
def create_one(session: Session, productor: models.ProductorCreate) -> models.ProductorPublic:
|
|
||||||
|
def create_one(
|
||||||
|
session: Session,
|
||||||
|
productor: models.ProductorCreate) -> models.ProductorPublic:
|
||||||
if not productor:
|
if not productor:
|
||||||
raise exceptions.ProductorCreateError(messages.Messages.invalid_input('productor', 'input cannot be None'))
|
raise exceptions.ProductorCreateError(
|
||||||
productor_create = productor.model_dump(exclude_unset=True, exclude='payment_methods')
|
messages.Messages.invalid_input(
|
||||||
|
'productor', 'input cannot be None'))
|
||||||
|
productor_create = productor.model_dump(
|
||||||
|
exclude_unset=True, exclude='payment_methods')
|
||||||
new_productor = models.Productor(**productor_create)
|
new_productor = models.Productor(**productor_create)
|
||||||
|
|
||||||
new_productor.payment_methods = [
|
new_productor.payment_methods = [
|
||||||
@@ -39,12 +47,17 @@ def create_one(session: Session, productor: models.ProductorCreate) -> models.Pr
|
|||||||
session.refresh(new_productor)
|
session.refresh(new_productor)
|
||||||
return new_productor
|
return new_productor
|
||||||
|
|
||||||
def update_one(session: Session, id: int, productor: models.ProductorUpdate) -> models.ProductorPublic:
|
|
||||||
|
def update_one(
|
||||||
|
session: Session,
|
||||||
|
id: int,
|
||||||
|
productor: models.ProductorUpdate) -> models.ProductorPublic:
|
||||||
statement = select(models.Productor).where(models.Productor.id == id)
|
statement = select(models.Productor).where(models.Productor.id == id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
new_productor = result.first()
|
new_productor = result.first()
|
||||||
if not new_productor:
|
if not new_productor:
|
||||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
raise exceptions.ProductorNotFoundError(
|
||||||
|
messages.Messages.not_found('productor'))
|
||||||
|
|
||||||
productor_updates = productor.model_dump(exclude_unset=True)
|
productor_updates = productor.model_dump(exclude_unset=True)
|
||||||
if 'payment_methods' in productor_updates:
|
if 'payment_methods' in productor_updates:
|
||||||
@@ -67,12 +80,14 @@ def update_one(session: Session, id: int, productor: models.ProductorUpdate) ->
|
|||||||
session.refresh(new_productor)
|
session.refresh(new_productor)
|
||||||
return new_productor
|
return new_productor
|
||||||
|
|
||||||
|
|
||||||
def delete_one(session: Session, id: int) -> models.ProductorPublic:
|
def delete_one(session: Session, id: int) -> models.ProductorPublic:
|
||||||
statement = select(models.Productor).where(models.Productor.id == id)
|
statement = select(models.Productor).where(models.Productor.id == id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
productor = result.first()
|
productor = result.first()
|
||||||
if not productor:
|
if not productor:
|
||||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
raise exceptions.ProductorNotFoundError(
|
||||||
|
messages.Messages.not_found('productor'))
|
||||||
result = models.ProductorPublic.model_validate(productor)
|
result = models.ProductorPublic.model_validate(productor)
|
||||||
session.delete(productor)
|
session.delete(productor)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|||||||
@@ -2,12 +2,15 @@ class ProductServiceError(Exception):
|
|||||||
def __init__(self, message: str):
|
def __init__(self, message: str):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|
||||||
|
|
||||||
class ProductorNotFoundError(ProductServiceError):
|
class ProductorNotFoundError(ProductServiceError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ProductNotFoundError(ProductServiceError):
|
class ProductNotFoundError(ProductServiceError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ProductCreateError(ProductServiceError):
|
class ProductCreateError(ProductServiceError):
|
||||||
def __init__(self, message: str, field: str | None = None):
|
def __init__(self, message: str, field: str | None = None):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
import src.models as models
|
|
||||||
from src.database import get_session
|
|
||||||
from sqlmodel import Session
|
|
||||||
import src.products.service as service
|
|
||||||
import src.products.exceptions as exceptions
|
import src.products.exceptions as exceptions
|
||||||
|
import src.products.service as service
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
from src.auth.auth import get_current_user
|
from src.auth.auth import get_current_user
|
||||||
|
from src.database import get_session
|
||||||
|
|
||||||
router = APIRouter(prefix='/products')
|
router = APIRouter(prefix='/products')
|
||||||
|
|
||||||
|
|
||||||
@router.get('', response_model=list[models.ProductPublic], )
|
@router.get('', response_model=list[models.ProductPublic], )
|
||||||
def get_products(
|
def get_products(
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
@@ -25,6 +26,7 @@ def get_products(
|
|||||||
types,
|
types,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{id}', response_model=models.ProductPublic)
|
@router.get('/{id}', response_model=models.ProductPublic)
|
||||||
def get_product(
|
def get_product(
|
||||||
id: int,
|
id: int,
|
||||||
@@ -33,9 +35,11 @@ def get_product(
|
|||||||
):
|
):
|
||||||
result = service.get_one(session, id)
|
result = service.get_one(session, id)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('product'))
|
raise HTTPException(status_code=404,
|
||||||
|
detail=messages.Messages.not_found('product'))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('', response_model=models.ProductPublic)
|
@router.post('', response_model=models.ProductPublic)
|
||||||
def create_product(
|
def create_product(
|
||||||
product: models.ProductCreate,
|
product: models.ProductCreate,
|
||||||
@@ -50,6 +54,7 @@ def create_product(
|
|||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.put('/{id}', response_model=models.ProductPublic)
|
@router.put('/{id}', response_model=models.ProductPublic)
|
||||||
def update_product(
|
def update_product(
|
||||||
id: int, product: models.ProductUpdate,
|
id: int, product: models.ProductUpdate,
|
||||||
@@ -64,6 +69,7 @@ def update_product(
|
|||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete('/{id}', response_model=models.ProductPublic)
|
@router.delete('/{id}', response_model=models.ProductPublic)
|
||||||
def delete_product(
|
def delete_product(
|
||||||
id: int,
|
id: int,
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from sqlmodel import Session, select
|
|
||||||
import src.models as models
|
|
||||||
import src.products.exceptions as exceptions
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
|
import src.products.exceptions as exceptions
|
||||||
|
from sqlmodel import Session, select
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def get_all(
|
def get_all(
|
||||||
session: Session,
|
session: Session,
|
||||||
@@ -10,10 +11,13 @@ def get_all(
|
|||||||
productors: list[str],
|
productors: list[str],
|
||||||
types: list[str],
|
types: list[str],
|
||||||
) -> list[models.ProductPublic]:
|
) -> list[models.ProductPublic]:
|
||||||
statement = select(models.Product)\
|
statement = select(
|
||||||
.join(models.Productor, models.Product.productor_id == models.Productor.id)\
|
models.Product) .join(
|
||||||
.where(models.Productor.type.in_([r.name for r in user.roles]))\
|
models.Productor,
|
||||||
.distinct()
|
models.Product.productor_id == models.Productor.id) .where(
|
||||||
|
models.Productor.type.in_(
|
||||||
|
[
|
||||||
|
r.name for r in user.roles])) .distinct()
|
||||||
if len(names) > 0:
|
if len(names) > 0:
|
||||||
statement = statement.where(models.Product.name.in_(names))
|
statement = statement.where(models.Product.name.in_(names))
|
||||||
if len(productors) > 0:
|
if len(productors) > 0:
|
||||||
@@ -22,14 +26,21 @@ def get_all(
|
|||||||
statement = statement.where(models.Product.type.in_(types))
|
statement = statement.where(models.Product.type.in_(types))
|
||||||
return session.exec(statement.order_by(models.Product.name)).all()
|
return session.exec(statement.order_by(models.Product.name)).all()
|
||||||
|
|
||||||
|
|
||||||
def get_one(session: Session, product_id: int) -> models.ProductPublic:
|
def get_one(session: Session, product_id: int) -> models.ProductPublic:
|
||||||
return session.get(models.Product, product_id)
|
return session.get(models.Product, product_id)
|
||||||
|
|
||||||
def create_one(session: Session, product: models.ProductCreate) -> models.ProductPublic:
|
|
||||||
|
def create_one(
|
||||||
|
session: Session,
|
||||||
|
product: models.ProductCreate) -> models.ProductPublic:
|
||||||
if not product:
|
if not product:
|
||||||
raise exceptions.ProductCreateError(messages.Messages.invalid_input('product', 'input cannot be None'))
|
raise exceptions.ProductCreateError(
|
||||||
|
messages.Messages.invalid_input(
|
||||||
|
'product', 'input cannot be None'))
|
||||||
if not session.get(models.Productor, product.productor_id):
|
if not session.get(models.Productor, product.productor_id):
|
||||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
raise exceptions.ProductorNotFoundError(
|
||||||
|
messages.Messages.not_found('productor'))
|
||||||
product_create = product.model_dump(exclude_unset=True)
|
product_create = product.model_dump(exclude_unset=True)
|
||||||
new_product = models.Product(**product_create)
|
new_product = models.Product(**product_create)
|
||||||
session.add(new_product)
|
session.add(new_product)
|
||||||
@@ -37,14 +48,21 @@ def create_one(session: Session, product: models.ProductCreate) -> models.Produc
|
|||||||
session.refresh(new_product)
|
session.refresh(new_product)
|
||||||
return new_product
|
return new_product
|
||||||
|
|
||||||
def update_one(session: Session, id: int, product: models.ProductUpdate) -> models.ProductPublic:
|
|
||||||
|
def update_one(
|
||||||
|
session: Session,
|
||||||
|
id: int,
|
||||||
|
product: models.ProductUpdate) -> models.ProductPublic:
|
||||||
statement = select(models.Product).where(models.Product.id == id)
|
statement = select(models.Product).where(models.Product.id == id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
new_product = result.first()
|
new_product = result.first()
|
||||||
if not new_product:
|
if not new_product:
|
||||||
raise exceptions.ProductNotFoundError(messages.Messages.not_found('product'))
|
raise exceptions.ProductNotFoundError(
|
||||||
if product.productor_id and not session.get(models.Productor, product.productor_id):
|
messages.Messages.not_found('product'))
|
||||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
if product.productor_id and not session.get(
|
||||||
|
models.Productor, product.productor_id):
|
||||||
|
raise exceptions.ProductorNotFoundError(
|
||||||
|
messages.Messages.not_found('productor'))
|
||||||
|
|
||||||
product_updates = product.model_dump(exclude_unset=True)
|
product_updates = product.model_dump(exclude_unset=True)
|
||||||
for key, value in product_updates.items():
|
for key, value in product_updates.items():
|
||||||
@@ -55,12 +73,14 @@ def update_one(session: Session, id: int, product: models.ProductUpdate) -> mode
|
|||||||
session.refresh(new_product)
|
session.refresh(new_product)
|
||||||
return new_product
|
return new_product
|
||||||
|
|
||||||
|
|
||||||
def delete_one(session: Session, id: int) -> models.ProductPublic:
|
def delete_one(session: Session, id: int) -> models.ProductPublic:
|
||||||
statement = select(models.Product).where(models.Product.id == id)
|
statement = select(models.Product).where(models.Product.id == id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
product = result.first()
|
product = result.first()
|
||||||
if not product:
|
if not product:
|
||||||
raise exceptions.ProductNotFoundError(messages.Messages.not_found('product'))
|
raise exceptions.ProductNotFoundError(
|
||||||
|
messages.Messages.not_found('product'))
|
||||||
result = models.ProductPublic.model_validate(product)
|
result = models.ProductPublic.model_validate(product)
|
||||||
session.delete(product)
|
session.delete(product)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
origins: str
|
origins: str
|
||||||
db_host: str
|
db_host: str
|
||||||
@@ -20,10 +21,21 @@ class Settings(BaseSettings):
|
|||||||
env_file='../.env'
|
env_file='../.env'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|
||||||
AUTH_URL = f"{settings.keycloak_server}/realms/{settings.keycloak_realm}/protocol/openid-connect/auth"
|
AUTH_URL = (
|
||||||
TOKEN_URL = f"{settings.keycloak_server}/realms/{settings.keycloak_realm}/protocol/openid-connect/token"
|
f'{settings.keycloak_server}/realms/'
|
||||||
ISSUER = f"{settings.keycloak_server}/realms/{settings.keycloak_realm}"
|
f'{settings.keycloak_realm}/protocol/openid-connect/auth'
|
||||||
JWKS_URL = f"{ISSUER}/protocol/openid-connect/certs"
|
)
|
||||||
LOGOUT_URL = f'{settings.keycloak_server}/realms/{settings.keycloak_realm}/protocol/openid-connect/logout'
|
TOKEN_URL = (
|
||||||
|
f'{settings.keycloak_server}/realms/'
|
||||||
|
f'{settings.keycloak_realm}/protocol/openid-connect/token'
|
||||||
|
)
|
||||||
|
|
||||||
|
ISSUER = f'{settings.keycloak_server}/realms/{settings.keycloak_realm}'
|
||||||
|
JWKS_URL = f'{ISSUER}/protocol/openid-connect/certs'
|
||||||
|
LOGOUT_URL = (
|
||||||
|
f'{settings.keycloak_server}/realms/'
|
||||||
|
f'{settings.keycloak_realm}/protocol/openid-connect/logout'
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
class ShipmentServiceError(Exception):
|
class ShipmentServiceError(Exception):
|
||||||
def __init__(self, message: str):
|
def __init__(self, message: str):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
logging.error('ShipmentService : %s', message)
|
||||||
|
|
||||||
|
|
||||||
class ShipmentNotFoundError(ShipmentServiceError):
|
class ShipmentNotFoundError(ShipmentServiceError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ShipmentCreateError(ShipmentServiceError):
|
class ShipmentCreateError(ShipmentServiceError):
|
||||||
def __init__(self, message: str, field: str | None = None):
|
def __init__(self, message: str, field: str | None = None):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|||||||
@@ -1,58 +1,111 @@
|
|||||||
from sqlmodel import Session, select
|
# pylint: disable=E1101
|
||||||
import src.models as models
|
|
||||||
import src.shipments.exceptions as exceptions
|
|
||||||
import src.messages as messages
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
import src.messages as messages
|
||||||
|
import src.shipments.exceptions as exceptions
|
||||||
|
from sqlmodel import Session, select
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def get_all(
|
def get_all(
|
||||||
session: Session,
|
session: Session,
|
||||||
user: models.User,
|
user: models.User,
|
||||||
names: list[str],
|
names: list[str] = None,
|
||||||
dates: list[str],
|
dates: list[str] = None,
|
||||||
forms: list[str]
|
forms: list[str] = None
|
||||||
) -> list[models.ShipmentPublic]:
|
) -> list[models.ShipmentPublic]:
|
||||||
statement = select(models.Shipment)\
|
statement = (
|
||||||
.join(models.Form, models.Shipment.form_id == models.Form.id)\
|
select(models.Shipment)
|
||||||
.join(models.Productor, models.Form.productor_id == models.Productor.id)\
|
.join(
|
||||||
.where(models.Productor.type.in_([r.name for r in user.roles]))\
|
models.Form,
|
||||||
|
models.Shipment.form_id == models.Form.id)
|
||||||
|
.join(
|
||||||
|
models.Productor,
|
||||||
|
models.Form.productor_id == models.Productor.id)
|
||||||
|
.where(
|
||||||
|
models.Productor.type.in_(
|
||||||
|
[r.name for r in user.roles]
|
||||||
|
)
|
||||||
|
)
|
||||||
.distinct()
|
.distinct()
|
||||||
if len(names) > 0:
|
)
|
||||||
|
if names and len(names) > 0:
|
||||||
statement = statement.where(models.Shipment.name.in_(names))
|
statement = statement.where(models.Shipment.name.in_(names))
|
||||||
if len(dates) > 0:
|
if dates and len(dates) > 0:
|
||||||
statement = statement.where(models.Shipment.date.in_(list(map(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').date(), dates))))
|
statement = statement.where(
|
||||||
if len(forms) > 0:
|
models.Shipment.date.in_(
|
||||||
|
list(map(
|
||||||
|
lambda x: datetime.datetime.strptime(
|
||||||
|
x, '%Y-%m-%d').date(),
|
||||||
|
dates
|
||||||
|
))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if forms and len(forms) > 0:
|
||||||
statement = statement.where(models.Form.name.in_(forms))
|
statement = statement.where(models.Form.name.in_(forms))
|
||||||
return session.exec(statement.order_by(models.Shipment.name)).all()
|
return session.exec(statement.order_by(models.Shipment.name)).all()
|
||||||
|
|
||||||
|
|
||||||
def get_one(session: Session, shipment_id: int) -> models.ShipmentPublic:
|
def get_one(session: Session, shipment_id: int) -> models.ShipmentPublic:
|
||||||
return session.get(models.Shipment, shipment_id)
|
return session.get(models.Shipment, shipment_id)
|
||||||
|
|
||||||
def create_one(session: Session, shipment: models.ShipmentCreate) -> models.ShipmentPublic:
|
|
||||||
|
def create_one(
|
||||||
|
session: Session,
|
||||||
|
shipment: models.ShipmentCreate) -> models.ShipmentPublic:
|
||||||
if shipment is None:
|
if shipment is None:
|
||||||
raise exceptions.ShipmentCreateError(messages.Messages.invalid_input('shipment', 'input cannot be None'))
|
raise exceptions.ShipmentCreateError(
|
||||||
products = session.exec(select(models.Product).where(models.Product.id.in_(shipment.product_ids))).all()
|
messages.Messages.invalid_input(
|
||||||
shipment_create = shipment.model_dump(exclude_unset=True, exclude={'product_ids'})
|
'shipment', 'input cannot be None'))
|
||||||
|
products = session.exec(
|
||||||
|
select(models.Product)
|
||||||
|
.where(
|
||||||
|
models.Product.id.in_(
|
||||||
|
shipment.product_ids
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).all()
|
||||||
|
shipment_create = shipment.model_dump(
|
||||||
|
exclude_unset=True, exclude={'product_ids'}
|
||||||
|
)
|
||||||
new_shipment = models.Shipment(**shipment_create, products=products)
|
new_shipment = models.Shipment(**shipment_create, products=products)
|
||||||
session.add(new_shipment)
|
session.add(new_shipment)
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(new_shipment)
|
session.refresh(new_shipment)
|
||||||
return new_shipment
|
return new_shipment
|
||||||
|
|
||||||
def update_one(session: Session, id: int, shipment: models.ShipmentUpdate) -> models.ShipmentPublic:
|
|
||||||
|
def update_one(
|
||||||
|
session: Session,
|
||||||
|
_id: int,
|
||||||
|
shipment: models.ShipmentUpdate) -> models.ShipmentPublic:
|
||||||
if shipment is None:
|
if shipment is None:
|
||||||
raise exceptions.ShipmentCreateError(messages.Messages.invalid_input('shipment', 'input cannot be None'))
|
raise exceptions.ShipmentCreateError(
|
||||||
statement = select(models.Shipment).where(models.Shipment.id == id)
|
messages.Messages.invalid_input(
|
||||||
|
'shipment', 'input cannot be None'))
|
||||||
|
statement = select(models.Shipment).where(models.Shipment.id == _id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
new_shipment = result.first()
|
new_shipment = result.first()
|
||||||
if not new_shipment:
|
if not new_shipment:
|
||||||
raise exceptions.ShipmentNotFoundError(messages.Messages.not_found('shipment'))
|
raise exceptions.ShipmentNotFoundError(
|
||||||
|
messages.Messages.not_found('shipment'))
|
||||||
|
|
||||||
products_to_add = session.exec(select(models.Product).where(models.Product.id.in_(shipment.product_ids))).all()
|
products_to_add = session.exec(
|
||||||
|
select(
|
||||||
|
models.Product
|
||||||
|
).where(
|
||||||
|
models.Product.id.in_(
|
||||||
|
shipment.product_ids
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).all()
|
||||||
new_shipment.products.clear()
|
new_shipment.products.clear()
|
||||||
for add in products_to_add:
|
for add in products_to_add:
|
||||||
new_shipment.products.append(add)
|
new_shipment.products.append(add)
|
||||||
|
|
||||||
shipment_updates = shipment.model_dump(exclude_unset=True, exclude={"product_ids"})
|
shipment_updates = shipment.model_dump(
|
||||||
|
exclude_unset=True, exclude={"product_ids"}
|
||||||
|
)
|
||||||
for key, value in shipment_updates.items():
|
for key, value in shipment_updates.items():
|
||||||
setattr(new_shipment, key, value)
|
setattr(new_shipment, key, value)
|
||||||
|
|
||||||
@@ -61,12 +114,14 @@ def update_one(session: Session, id: int, shipment: models.ShipmentUpdate) -> mo
|
|||||||
session.refresh(new_shipment)
|
session.refresh(new_shipment)
|
||||||
return new_shipment
|
return new_shipment
|
||||||
|
|
||||||
def delete_one(session: Session, id: int) -> models.ShipmentPublic:
|
|
||||||
statement = select(models.Shipment).where(models.Shipment.id == id)
|
def delete_one(session: Session, _id: int) -> models.ShipmentPublic:
|
||||||
|
statement = select(models.Shipment).where(models.Shipment.id == _id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
shipment = result.first()
|
shipment = result.first()
|
||||||
if not shipment:
|
if not shipment:
|
||||||
raise exceptions.ShipmentNotFoundError(messages.Messages.not_found('shipment'))
|
raise exceptions.ShipmentNotFoundError(
|
||||||
|
messages.Messages.not_found('shipment'))
|
||||||
|
|
||||||
result = models.ShipmentPublic.model_validate(shipment)
|
result = models.ShipmentPublic.model_validate(shipment)
|
||||||
session.delete(shipment)
|
session.delete(shipment)
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
import src.models as models
|
|
||||||
from src.database import get_session
|
|
||||||
from sqlmodel import Session
|
|
||||||
import src.shipments.service as service
|
|
||||||
import src.shipments.exceptions as exceptions
|
import src.shipments.exceptions as exceptions
|
||||||
|
import src.shipments.service as service
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
from src.auth.auth import get_current_user
|
from src.auth.auth import get_current_user
|
||||||
|
from src.database import get_session
|
||||||
|
|
||||||
router = APIRouter(prefix='/shipments')
|
router = APIRouter(prefix='/shipments')
|
||||||
|
|
||||||
|
|
||||||
@router.get('', response_model=list[models.ShipmentPublic], )
|
@router.get('', response_model=list[models.ShipmentPublic], )
|
||||||
def get_shipments(
|
def get_shipments(
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
@@ -25,17 +26,22 @@ def get_shipments(
|
|||||||
forms,
|
forms,
|
||||||
)
|
)
|
||||||
|
|
||||||
@router.get('/{id}', response_model=models.ShipmentPublic)
|
|
||||||
|
@router.get('/{_id}', response_model=models.ShipmentPublic)
|
||||||
def get_shipment(
|
def get_shipment(
|
||||||
id: int,
|
_id: int,
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
session: Session = Depends(get_session)
|
session: Session = Depends(get_session)
|
||||||
):
|
):
|
||||||
result = service.get_one(session, id)
|
result = service.get_one(session, _id)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('shipment'))
|
raise HTTPException(
|
||||||
|
status_code=404,
|
||||||
|
detail=messages.Messages.not_found('shipment')
|
||||||
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('', response_model=models.ShipmentPublic)
|
@router.post('', response_model=models.ShipmentPublic)
|
||||||
def create_shipment(
|
def create_shipment(
|
||||||
shipment: models.ShipmentCreate,
|
shipment: models.ShipmentCreate,
|
||||||
@@ -45,30 +51,32 @@ def create_shipment(
|
|||||||
try:
|
try:
|
||||||
result = service.create_one(session, shipment)
|
result = service.create_one(session, shipment)
|
||||||
except exceptions.ShipmentCreateError as error:
|
except exceptions.ShipmentCreateError as error:
|
||||||
raise HTTPException(status_code=400, detail=str(error))
|
raise HTTPException(status_code=400, detail=str(error)) from error
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@router.put('/{id}', response_model=models.ShipmentPublic)
|
|
||||||
|
@router.put('/{_id}', response_model=models.ShipmentPublic)
|
||||||
def update_shipment(
|
def update_shipment(
|
||||||
id: int,
|
_id: int,
|
||||||
shipment: models.ShipmentUpdate,
|
shipment: models.ShipmentUpdate,
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
session: Session = Depends(get_session)
|
session: Session = Depends(get_session)
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
result = service.update_one(session, id, shipment)
|
result = service.update_one(session, _id, shipment)
|
||||||
except exceptions.ShipmentNotFoundError as error:
|
except exceptions.ShipmentNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@router.delete('/{id}', response_model=models.ShipmentPublic)
|
|
||||||
|
@router.delete('/{_id}', response_model=models.ShipmentPublic)
|
||||||
def delete_shipment(
|
def delete_shipment(
|
||||||
id: int,
|
_id: int,
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
session: Session = Depends(get_session)
|
session: Session = Depends(get_session)
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
result = service.delete_one(session, id)
|
result = service.delete_one(session, _id)
|
||||||
except exceptions.ShipmentNotFoundError as error:
|
except exceptions.ShipmentNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=str(error))
|
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -1,14 +1,19 @@
|
|||||||
from sqlmodel import Session, select
|
from sqlmodel import Session, select
|
||||||
import src.models as models
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def get_all(session: Session) -> list[models.TemplatePublic]:
|
def get_all(session: Session) -> list[models.TemplatePublic]:
|
||||||
statement = select(models.Template)
|
statement = select(models.Template)
|
||||||
return session.exec(statement.order_by(models.Template.name)).all()
|
return session.exec(statement.order_by(models.Template.name)).all()
|
||||||
|
|
||||||
|
|
||||||
def get_one(session: Session, template_id: int) -> models.TemplatePublic:
|
def get_one(session: Session, template_id: int) -> models.TemplatePublic:
|
||||||
return session.get(models.Template, template_id)
|
return session.get(models.Template, template_id)
|
||||||
|
|
||||||
def create_one(session: Session, template: models.TemplateCreate) -> models.TemplatePublic:
|
|
||||||
|
def create_one(
|
||||||
|
session: Session,
|
||||||
|
template: models.TemplateCreate) -> models.TemplatePublic:
|
||||||
template_create = template.model_dump(exclude_unset=True)
|
template_create = template.model_dump(exclude_unset=True)
|
||||||
new_template = models.Template(**template_create)
|
new_template = models.Template(**template_create)
|
||||||
session.add(new_template)
|
session.add(new_template)
|
||||||
@@ -16,7 +21,11 @@ def create_one(session: Session, template: models.TemplateCreate) -> models.Temp
|
|||||||
session.refresh(new_template)
|
session.refresh(new_template)
|
||||||
return new_template
|
return new_template
|
||||||
|
|
||||||
def update_one(session: Session, id: int, template: models.TemplateUpdate) -> models.TemplatePublic:
|
|
||||||
|
def update_one(
|
||||||
|
session: Session,
|
||||||
|
id: int,
|
||||||
|
template: models.TemplateUpdate) -> models.TemplatePublic:
|
||||||
statement = select(models.Template).where(models.Template.id == id)
|
statement = select(models.Template).where(models.Template.id == id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
new_template = result.first()
|
new_template = result.first()
|
||||||
@@ -30,6 +39,7 @@ def update_one(session: Session, id: int, template: models.TemplateUpdate) -> mo
|
|||||||
session.refresh(new_template)
|
session.refresh(new_template)
|
||||||
return new_template
|
return new_template
|
||||||
|
|
||||||
|
|
||||||
def delete_one(session: Session, id: int) -> models.TemplatePublic:
|
def delete_one(session: Session, id: int) -> models.TemplatePublic:
|
||||||
statement = select(models.Template).where(models.Template.id == id)
|
statement = select(models.Template).where(models.Template.id == id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
import src.models as models
|
|
||||||
from src.database import get_session
|
|
||||||
from sqlmodel import Session
|
|
||||||
import src.templates.service as service
|
import src.templates.service as service
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
from src.auth.auth import get_current_user
|
from src.auth.auth import get_current_user
|
||||||
|
from src.database import get_session
|
||||||
|
|
||||||
router = APIRouter(prefix='/templates')
|
router = APIRouter(prefix='/templates')
|
||||||
|
|
||||||
|
|
||||||
@router.get('', response_model=list[models.TemplatePublic])
|
@router.get('', response_model=list[models.TemplatePublic])
|
||||||
def get_templates(
|
def get_templates(
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
@@ -15,6 +16,7 @@ def get_templates(
|
|||||||
):
|
):
|
||||||
return service.get_all(session)
|
return service.get_all(session)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{id}', response_model=models.TemplatePublic)
|
@router.get('/{id}', response_model=models.TemplatePublic)
|
||||||
def get_template(
|
def get_template(
|
||||||
id: int,
|
id: int,
|
||||||
@@ -23,9 +25,11 @@ def get_template(
|
|||||||
):
|
):
|
||||||
result = service.get_one(session, id)
|
result = service.get_one(session, id)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('template'))
|
raise HTTPException(status_code=404,
|
||||||
|
detail=messages.Messages.not_found('template'))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('', response_model=models.TemplatePublic)
|
@router.post('', response_model=models.TemplatePublic)
|
||||||
def create_template(
|
def create_template(
|
||||||
template: models.TemplateCreate,
|
template: models.TemplateCreate,
|
||||||
@@ -34,6 +38,7 @@ def create_template(
|
|||||||
):
|
):
|
||||||
return service.create_one(session, template)
|
return service.create_one(session, template)
|
||||||
|
|
||||||
|
|
||||||
@router.put('/{id}', response_model=models.TemplatePublic)
|
@router.put('/{id}', response_model=models.TemplatePublic)
|
||||||
def update_template(
|
def update_template(
|
||||||
id: int, template: models.TemplateUpdate,
|
id: int, template: models.TemplateUpdate,
|
||||||
@@ -42,9 +47,11 @@ def update_template(
|
|||||||
):
|
):
|
||||||
result = service.update_one(session, id, template)
|
result = service.update_one(session, id, template)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('template'))
|
raise HTTPException(status_code=404,
|
||||||
|
detail=messages.Messages.not_found('template'))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete('/{id}', response_model=models.TemplatePublic)
|
@router.delete('/{id}', response_model=models.TemplatePublic)
|
||||||
def delete_template(
|
def delete_template(
|
||||||
id: int,
|
id: int,
|
||||||
@@ -53,5 +60,6 @@ def delete_template(
|
|||||||
):
|
):
|
||||||
result = service.delete_one(session, id)
|
result = service.delete_one(session, id)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('template'))
|
raise HTTPException(status_code=404,
|
||||||
|
detail=messages.Messages.not_found('template'))
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
class UserServiceError(Exception):
|
class UserServiceError(Exception):
|
||||||
def __init__(self, message: str):
|
def __init__(self, message: str):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
logging.error('UserService : %s', message)
|
||||||
|
|
||||||
|
|
||||||
class UserNotFoundError(UserServiceError):
|
class UserNotFoundError(UserServiceError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class UserCreateError(UserServiceError):
|
class UserCreateError(UserServiceError):
|
||||||
def __init__(self, message: str, field: str | None = None):
|
def __init__(self, message: str, field: str | None = None):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
from sqlmodel import Session, select
|
|
||||||
|
|
||||||
import src.models as models
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
|
|
||||||
import src.users.exceptions as exceptions
|
import src.users.exceptions as exceptions
|
||||||
|
from sqlmodel import Session, select
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def get_all(
|
def get_all(
|
||||||
session: Session,
|
session: Session,
|
||||||
@@ -17,11 +16,15 @@ def get_all(
|
|||||||
statement = statement.where(models.User.email.in_(emails))
|
statement = statement.where(models.User.email.in_(emails))
|
||||||
return session.exec(statement.order_by(models.User.name)).all()
|
return session.exec(statement.order_by(models.User.name)).all()
|
||||||
|
|
||||||
|
|
||||||
def get_one(session: Session, user_id: int) -> models.UserPublic:
|
def get_one(session: Session, user_id: int) -> models.UserPublic:
|
||||||
return session.get(models.User, user_id)
|
return session.get(models.User, user_id)
|
||||||
|
|
||||||
def get_or_create_roles(session: Session, role_names: list[str]) -> list[models.ContractType]:
|
|
||||||
statement = select(models.ContractType).where(models.ContractType.name.in_(role_names))
|
def get_or_create_roles(session: Session,
|
||||||
|
role_names: list[str]) -> list[models.ContractType]:
|
||||||
|
statement = select(models.ContractType).where(
|
||||||
|
models.ContractType.name.in_(role_names))
|
||||||
existing = session.exec(statement).all()
|
existing = session.exec(statement).all()
|
||||||
existing_roles = {role.name for role in existing}
|
existing_roles = {role.name for role in existing}
|
||||||
missing_role = set(role_names) - existing_roles
|
missing_role = set(role_names) - existing_roles
|
||||||
@@ -37,8 +40,11 @@ def get_or_create_roles(session: Session, role_names: list[str]) -> list[models.
|
|||||||
session.refresh(role)
|
session.refresh(role)
|
||||||
return existing + new_roles
|
return existing + new_roles
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_user(session: Session, user_create: models.UserCreate):
|
def get_or_create_user(session: Session, user_create: models.UserCreate):
|
||||||
statement = select(models.User).where(models.User.email == user_create.email)
|
statement = select(
|
||||||
|
models.User).where(
|
||||||
|
models.User.email == user_create.email)
|
||||||
user = session.exec(statement).first()
|
user = session.exec(statement).first()
|
||||||
if user:
|
if user:
|
||||||
user_role_names = [r.name for r in user.roles]
|
user_role_names = [r.name for r in user.roles]
|
||||||
@@ -48,13 +54,17 @@ def get_or_create_user(session: Session, user_create: models.UserCreate):
|
|||||||
user = create_one(session, user_create)
|
user = create_one(session, user_create)
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
def get_roles(session: Session):
|
def get_roles(session: Session):
|
||||||
statement = select(models.ContractType)
|
statement = select(models.ContractType)
|
||||||
return session.exec(statement.order_by(models.ContractType.name)).all()
|
return session.exec(statement.order_by(models.ContractType.name)).all()
|
||||||
|
|
||||||
|
|
||||||
def create_one(session: Session, user: models.UserCreate) -> models.UserPublic:
|
def create_one(session: Session, user: models.UserCreate) -> models.UserPublic:
|
||||||
if user is None:
|
if user is None:
|
||||||
raise exceptions.UserCreateError(messages.Messages.invalid_input('user', 'input cannot be None'))
|
raise exceptions.UserCreateError(
|
||||||
|
messages.Messages.invalid_input(
|
||||||
|
'user', 'input cannot be None'))
|
||||||
new_user = models.User(
|
new_user = models.User(
|
||||||
name=user.name,
|
name=user.name,
|
||||||
email=user.email
|
email=user.email
|
||||||
@@ -68,9 +78,15 @@ def create_one(session: Session, user: models.UserCreate) -> models.UserPublic:
|
|||||||
session.refresh(new_user)
|
session.refresh(new_user)
|
||||||
return new_user
|
return new_user
|
||||||
|
|
||||||
def update_one(session: Session, id: int, user: models.UserCreate) -> models.UserPublic:
|
|
||||||
|
def update_one(
|
||||||
|
session: Session,
|
||||||
|
id: int,
|
||||||
|
user: models.UserCreate) -> models.UserPublic:
|
||||||
if user is None:
|
if user is None:
|
||||||
raise exceptions.UserCreateError(messages.s.invalid_input('user', 'input cannot be None'))
|
raise exceptions.UserCreateError(
|
||||||
|
messages.s.invalid_input(
|
||||||
|
'user', 'input cannot be None'))
|
||||||
statement = select(models.User).where(models.User.id == id)
|
statement = select(models.User).where(models.User.id == id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
new_user = result.first()
|
new_user = result.first()
|
||||||
@@ -86,6 +102,7 @@ def update_one(session: Session, id: int, user: models.UserCreate) -> models.Use
|
|||||||
session.refresh(new_user)
|
session.refresh(new_user)
|
||||||
return new_user
|
return new_user
|
||||||
|
|
||||||
|
|
||||||
def delete_one(session: Session, id: int) -> models.UserPublic:
|
def delete_one(session: Session, id: int) -> models.UserPublic:
|
||||||
statement = select(models.User).where(models.User.id == id)
|
statement = select(models.User).where(models.User.id == id)
|
||||||
result = session.exec(statement)
|
result = session.exec(statement)
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
import src.models as models
|
|
||||||
from src.database import get_session
|
|
||||||
from sqlmodel import Session
|
|
||||||
import src.users.service as service
|
|
||||||
from src.auth.auth import get_current_user
|
|
||||||
import src.users.exceptions as exceptions
|
import src.users.exceptions as exceptions
|
||||||
|
import src.users.service as service
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
|
from src.auth.auth import get_current_user
|
||||||
|
from src.database import get_session
|
||||||
|
|
||||||
router = APIRouter(prefix='/users')
|
router = APIRouter(prefix='/users')
|
||||||
|
|
||||||
|
|
||||||
@router.get('', response_model=list[models.UserPublic])
|
@router.get('', response_model=list[models.UserPublic])
|
||||||
def get_users(
|
def get_users(
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
@@ -22,6 +23,7 @@ def get_users(
|
|||||||
emails,
|
emails,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/roles', response_model=list[models.ContractType])
|
@router.get('/roles', response_model=list[models.ContractType])
|
||||||
def get_roles(
|
def get_roles(
|
||||||
user: models.User = Depends(get_current_user),
|
user: models.User = Depends(get_current_user),
|
||||||
@@ -29,6 +31,7 @@ def get_roles(
|
|||||||
):
|
):
|
||||||
return service.get_roles(session)
|
return service.get_roles(session)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{id}', response_model=models.UserPublic)
|
@router.get('/{id}', response_model=models.UserPublic)
|
||||||
def get_users(
|
def get_users(
|
||||||
id: int,
|
id: int,
|
||||||
@@ -37,9 +40,11 @@ def get_users(
|
|||||||
):
|
):
|
||||||
result = service.get_one(session, id)
|
result = service.get_one(session, id)
|
||||||
if result is None:
|
if result is None:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('user'))
|
raise HTTPException(status_code=404,
|
||||||
|
detail=messages.Messages.not_found('user'))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.post('', response_model=models.UserPublic)
|
@router.post('', response_model=models.UserPublic)
|
||||||
def create_user(
|
def create_user(
|
||||||
user: models.UserCreate,
|
user: models.UserCreate,
|
||||||
@@ -52,6 +57,7 @@ def create_user(
|
|||||||
raise HTTPException(status_code=400, detail=str(error))
|
raise HTTPException(status_code=400, detail=str(error))
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
@router.put('/{id}', response_model=models.UserPublic)
|
@router.put('/{id}', response_model=models.UserPublic)
|
||||||
def update_user(
|
def update_user(
|
||||||
id: int,
|
id: int,
|
||||||
@@ -62,9 +68,11 @@ def update_user(
|
|||||||
try:
|
try:
|
||||||
result = service.update_one(session, id, user)
|
result = service.update_one(session, id, user)
|
||||||
except exceptions.UserNotFoundError as error:
|
except exceptions.UserNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('user'))
|
raise HTTPException(status_code=404,
|
||||||
|
detail=messages.Messages.not_found('user'))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete('/{id}', response_model=models.UserPublic)
|
@router.delete('/{id}', response_model=models.UserPublic)
|
||||||
def delete_user(
|
def delete_user(
|
||||||
id: int,
|
id: int,
|
||||||
@@ -74,5 +82,6 @@ def delete_user(
|
|||||||
try:
|
try:
|
||||||
result = service.delete_one(session, id)
|
result = service.delete_one(session, id)
|
||||||
except exceptions.UserNotFoundError as error:
|
except exceptions.UserNotFoundError as error:
|
||||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('user'))
|
raise HTTPException(status_code=404,
|
||||||
|
detail=messages.Messages.not_found('user'))
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
from sqlmodel import SQLModel, Session, create_engine
|
|
||||||
from sqlalchemy.pool import StaticPool
|
from sqlalchemy.pool import StaticPool
|
||||||
|
from sqlmodel import Session, SQLModel, create_engine
|
||||||
|
from src import models
|
||||||
|
from src.auth.auth import get_current_user
|
||||||
|
from src.database import get_session
|
||||||
|
from src.main import app
|
||||||
|
|
||||||
from .fixtures import *
|
from .fixtures import *
|
||||||
from src.main import app
|
|
||||||
import src.models as models
|
|
||||||
from src.database import get_session
|
|
||||||
from src.auth.auth import get_current_user
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import src.models as models
|
|
||||||
import tests.factories.contracts as contract_factory
|
import tests.factories.contracts as contract_factory
|
||||||
import tests.factories.products as product_factory
|
import tests.factories.products as product_factory
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def contract_product_factory(**kwargs):
|
def contract_product_factory(**kwargs):
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import src.models as models
|
from src import models
|
||||||
|
|
||||||
from .forms import form_factory
|
from .forms import form_factory
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import src.models as models
|
import datetime
|
||||||
|
|
||||||
|
from src import models
|
||||||
|
|
||||||
from .productors import productor_public_factory
|
from .productors import productor_public_factory
|
||||||
from .users import user_factory
|
from .users import user_factory
|
||||||
import datetime
|
|
||||||
|
|
||||||
|
|
||||||
def form_factory(**kwargs):
|
def form_factory(**kwargs):
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import src.models as models
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def productor_factory(**kwargs):
|
def productor_factory(**kwargs):
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import src.models as models
|
from src import models
|
||||||
|
|
||||||
from .productors import productor_factory
|
from .productors import productor_factory
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import src.models as models
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def shipment_factory(**kwargs):
|
def shipment_factory(**kwargs):
|
||||||
data = dict(
|
data = dict(
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import src.models as models
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
def user_factory(**kwargs):
|
def user_factory(**kwargs):
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
import pytest
|
|
||||||
import datetime
|
import datetime
|
||||||
from sqlmodel import Session
|
|
||||||
|
|
||||||
import src.models as models
|
import pytest
|
||||||
import src.forms.service as forms_service
|
import src.forms.service as forms_service
|
||||||
import src.shipments.service as shipments_service
|
|
||||||
import src.productors.service as productors_service
|
import src.productors.service as productors_service
|
||||||
import src.products.service as products_service
|
import src.products.service as products_service
|
||||||
|
import src.shipments.service as shipments_service
|
||||||
import src.users.service as users_service
|
import src.users.service as users_service
|
||||||
import tests.factories.forms as forms_factory
|
import tests.factories.forms as forms_factory
|
||||||
import tests.factories.shipments as shipments_factory
|
|
||||||
import tests.factories.productors as productors_factory
|
import tests.factories.productors as productors_factory
|
||||||
import tests.factories.products as products_factory
|
import tests.factories.products as products_factory
|
||||||
|
import tests.factories.shipments as shipments_factory
|
||||||
import tests.factories.users as users_factory
|
import tests.factories.users as users_factory
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import src.contracts.service as service
|
import src.contracts.service as service
|
||||||
import src.models as models
|
import tests.factories.contract_products as contract_products_factory
|
||||||
from src.main import app
|
|
||||||
from src.auth.auth import get_current_user
|
|
||||||
import tests.factories.contracts as contract_factory
|
import tests.factories.contracts as contract_factory
|
||||||
import tests.factories.forms as form_factory
|
import tests.factories.forms as form_factory
|
||||||
import tests.factories.contract_products as contract_products_factory
|
|
||||||
|
|
||||||
from fastapi.exceptions import HTTPException
|
from fastapi.exceptions import HTTPException
|
||||||
|
from src import models
|
||||||
|
from src.auth.auth import get_current_user
|
||||||
|
from src.main import app
|
||||||
|
|
||||||
|
|
||||||
class TestContracts:
|
class TestContracts:
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import src.forms.service as service
|
|
||||||
import src.forms.exceptions as forms_exceptions
|
import src.forms.exceptions as forms_exceptions
|
||||||
import src.models as models
|
import src.forms.service as service
|
||||||
from src.main import app
|
import src.messages as messages
|
||||||
from src.auth.auth import get_current_user
|
|
||||||
import tests.factories.forms as form_factory
|
import tests.factories.forms as form_factory
|
||||||
from fastapi.exceptions import HTTPException
|
from fastapi.exceptions import HTTPException
|
||||||
import src.messages as messages
|
from src import models
|
||||||
|
from src.auth.auth import get_current_user
|
||||||
|
from src.main import app
|
||||||
|
|
||||||
|
|
||||||
class TestForms:
|
class TestForms:
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
from fastapi.exceptions import HTTPException
|
|
||||||
|
|
||||||
from src.main import app
|
|
||||||
import src.models as models
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
from src.auth.auth import get_current_user
|
|
||||||
|
|
||||||
import src.productors.service as service
|
|
||||||
import src.productors.exceptions as exceptions
|
import src.productors.exceptions as exceptions
|
||||||
|
import src.productors.service as service
|
||||||
import tests.factories.productors as productor_factory
|
import tests.factories.productors as productor_factory
|
||||||
|
from fastapi.exceptions import HTTPException
|
||||||
|
from src import models
|
||||||
|
from src.auth.auth import get_current_user
|
||||||
|
from src.main import app
|
||||||
|
|
||||||
|
|
||||||
class TestProductors:
|
class TestProductors:
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import src.products.service as service
|
|
||||||
import src.products.exceptions as exceptions
|
import src.products.exceptions as exceptions
|
||||||
import src.models as models
|
import src.products.service as service
|
||||||
from src.main import app
|
|
||||||
from src.auth.auth import get_current_user
|
|
||||||
import tests.factories.products as product_factory
|
import tests.factories.products as product_factory
|
||||||
|
|
||||||
from fastapi.exceptions import HTTPException
|
from fastapi.exceptions import HTTPException
|
||||||
|
from src import models
|
||||||
|
from src.auth.auth import get_current_user
|
||||||
|
from src.main import app
|
||||||
|
|
||||||
|
|
||||||
class TestProducts:
|
class TestProducts:
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import src.shipments.service as service
|
|
||||||
import src.models as models
|
|
||||||
from src.main import app
|
|
||||||
import src.messages as messages
|
import src.messages as messages
|
||||||
import src.shipments.exceptions as exceptions
|
import src.shipments.exceptions as exceptions
|
||||||
from src.auth.auth import get_current_user
|
import src.shipments.service as service
|
||||||
import tests.factories.shipments as shipment_factory
|
import tests.factories.shipments as shipment_factory
|
||||||
|
|
||||||
from fastapi.exceptions import HTTPException
|
from fastapi.exceptions import HTTPException
|
||||||
|
from src import models
|
||||||
|
from src.auth.auth import get_current_user
|
||||||
|
from src.main import app
|
||||||
|
|
||||||
|
|
||||||
class TestShipments:
|
class TestShipments:
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import src.users.service as service
|
|
||||||
import src.models as models
|
|
||||||
from src.main import app
|
|
||||||
from src.auth.auth import get_current_user
|
|
||||||
import tests.factories.users as user_factory
|
|
||||||
import src.users.exceptions as exceptions
|
import src.users.exceptions as exceptions
|
||||||
|
import src.users.service as service
|
||||||
|
import tests.factories.users as user_factory
|
||||||
from fastapi.exceptions import HTTPException
|
from fastapi.exceptions import HTTPException
|
||||||
|
from src import models
|
||||||
|
from src.auth.auth import get_current_user
|
||||||
|
from src.main import app
|
||||||
|
|
||||||
|
|
||||||
class TestUsers:
|
class TestUsers:
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from sqlmodel import Session
|
|
||||||
|
|
||||||
import src.models as models
|
|
||||||
import src.forms.service as forms_service
|
|
||||||
import src.forms.exceptions as forms_exceptions
|
import src.forms.exceptions as forms_exceptions
|
||||||
|
import src.forms.service as forms_service
|
||||||
import tests.factories.forms as forms_factory
|
import tests.factories.forms as forms_factory
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
class TestFormsService:
|
class TestFormsService:
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from sqlmodel import Session
|
|
||||||
|
|
||||||
import src.models as models
|
|
||||||
import src.productors.service as productors_service
|
|
||||||
import src.productors.exceptions as productors_exceptions
|
import src.productors.exceptions as productors_exceptions
|
||||||
|
import src.productors.service as productors_service
|
||||||
import tests.factories.productors as productors_factory
|
import tests.factories.productors as productors_factory
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
class TestProductorsService:
|
class TestProductorsService:
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from sqlmodel import Session
|
|
||||||
|
|
||||||
import src.models as models
|
|
||||||
import src.products.service as products_service
|
|
||||||
import src.products.exceptions as products_exceptions
|
import src.products.exceptions as products_exceptions
|
||||||
|
import src.products.service as products_service
|
||||||
import tests.factories.products as products_factory
|
import tests.factories.products as products_factory
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
class TestProductsService:
|
class TestProductsService:
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import pytest
|
|
||||||
import datetime
|
import datetime
|
||||||
from sqlmodel import Session
|
|
||||||
|
|
||||||
import src.models as models
|
import pytest
|
||||||
import src.shipments.service as shipments_service
|
|
||||||
import src.shipments.exceptions as shipments_exceptions
|
import src.shipments.exceptions as shipments_exceptions
|
||||||
|
import src.shipments.service as shipments_service
|
||||||
import tests.factories.shipments as shipments_factory
|
import tests.factories.shipments as shipments_factory
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
class TestShipmentsService:
|
class TestShipmentsService:
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from sqlmodel import Session
|
|
||||||
|
|
||||||
import src.models as models
|
|
||||||
import src.users.service as users_service
|
|
||||||
import src.users.exceptions as users_exceptions
|
import src.users.exceptions as users_exceptions
|
||||||
|
import src.users.service as users_service
|
||||||
import tests.factories.users as users_factory
|
import tests.factories.users as users_factory
|
||||||
|
from sqlmodel import Session
|
||||||
|
from src import models
|
||||||
|
|
||||||
|
|
||||||
class TestUsersService:
|
class TestUsersService:
|
||||||
|
|||||||
Reference in New Issue
Block a user