This commit is contained in:
@@ -30,6 +30,31 @@ async def get_forms_filtered(
|
||||
return service.get_all(session, seasons, productors, current_season, user)
|
||||
|
||||
|
||||
@router.get(
|
||||
'/{_id}/preview-delete',
|
||||
response_model=list[models.DeleteDependency]
|
||||
)
|
||||
async def preview_delete(
|
||||
_id: int,
|
||||
session: Session = Depends(get_session),
|
||||
user: models.User = Depends(get_current_user),
|
||||
):
|
||||
if not service.is_allowed(session, user, _id=_id):
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail=messages.Messages.not_allowed('forms', 'delete')
|
||||
)
|
||||
try:
|
||||
result = service.get_delete_dependencies(
|
||||
session,
|
||||
_id
|
||||
)
|
||||
print(result)
|
||||
except exceptions.FormNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||
return result
|
||||
|
||||
|
||||
@router.get('/{_id}', response_model=models.FormPublic)
|
||||
async def get_form(
|
||||
_id: int,
|
||||
@@ -68,7 +93,7 @@ async def create_form(
|
||||
|
||||
@router.put('/{_id}', response_model=models.FormPublic)
|
||||
async def update_form(
|
||||
_id: int,
|
||||
_id: int,
|
||||
form: models.FormUpdate,
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
|
||||
@@ -107,6 +107,44 @@ def delete_one(session: Session, _id: int) -> models.FormPublic:
|
||||
return result
|
||||
|
||||
|
||||
def get_delete_dependencies(
|
||||
session: Session,
|
||||
_id: int
|
||||
) -> list[models.DeleteDependency]:
|
||||
statement = select(models.Form).where(models.Form.id == _id)
|
||||
result = session.exec(statement)
|
||||
form = result.first()
|
||||
if not form:
|
||||
raise exceptions.FormNotFoundError(messages.Messages.not_found('form'))
|
||||
print(_id)
|
||||
statement_shipment = (
|
||||
select(models.Shipment)
|
||||
.where(models.Shipment.form_id == _id)
|
||||
.distinct()
|
||||
)
|
||||
statement_contracts = (
|
||||
select(models.Contract)
|
||||
.where(models.Contract.form_id == _id)
|
||||
.distinct()
|
||||
)
|
||||
shipments = session.exec(statement_shipment).all()
|
||||
contracts = session.exec(statement_contracts).all()
|
||||
result = [
|
||||
models.DeleteDependency(
|
||||
name=sh.name,
|
||||
id=sh.id,
|
||||
type='shipment'
|
||||
) for sh in shipments
|
||||
] + [
|
||||
models.DeleteDependency(
|
||||
name=f'{co.firstname} {co.lastname}',
|
||||
id=co.id,
|
||||
type='contract'
|
||||
) for co in contracts
|
||||
]
|
||||
return result
|
||||
|
||||
|
||||
def is_allowed(
|
||||
session: Session,
|
||||
user: models.User,
|
||||
|
||||
@@ -5,6 +5,12 @@ from typing import Optional
|
||||
from sqlmodel import Column, Field, LargeBinary, Relationship, SQLModel
|
||||
|
||||
|
||||
class DeleteDependency(SQLModel):
|
||||
id: int
|
||||
name: str
|
||||
type: str
|
||||
|
||||
|
||||
class ContractType(SQLModel, table=True):
|
||||
id: int | None = Field(
|
||||
default=None,
|
||||
|
||||
@@ -138,11 +138,7 @@ def is_allowed(
|
||||
return False
|
||||
if not _id:
|
||||
statement = (
|
||||
select(models.Shipment)
|
||||
.join(
|
||||
models.Form,
|
||||
models.Shipment.form_id == models.Form.id
|
||||
)
|
||||
select(models.Form)
|
||||
.where(models.Form.id == shipment.form_id)
|
||||
)
|
||||
form = session.exec(statement).first()
|
||||
@@ -162,4 +158,3 @@ def is_allowed(
|
||||
.distinct()
|
||||
)
|
||||
return len(session.exec(statement).all()) > 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user