This commit is contained in:
@@ -77,8 +77,8 @@ def callback(code: str, session: Session = Depends(get_session)):
|
||||
response = requests.post(TOKEN_URL, data=data, headers=headers)
|
||||
if response.status_code != 200:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=messages.failtogettoken
|
||||
status_code=404,
|
||||
detail=messages.Messages.not_found('token')
|
||||
)
|
||||
|
||||
token_data = response.json()
|
||||
@@ -154,26 +154,26 @@ def verify_token(token: str):
|
||||
)
|
||||
return decoded
|
||||
except jwt.ExpiredSignatureError:
|
||||
raise HTTPException(status_code=401, detail=messages.tokenexipired)
|
||||
raise HTTPException(status_code=401, detail=messages.Messages.tokenexipired)
|
||||
except jwt.InvalidTokenError:
|
||||
raise HTTPException(status_code=401, detail=messages.invalidtoken)
|
||||
raise HTTPException(status_code=401, detail=messages.Messages.invalidtoken)
|
||||
|
||||
|
||||
def get_current_user(request: Request, session: Session = Depends(get_session)):
|
||||
access_token = request.cookies.get('access_token')
|
||||
if not access_token:
|
||||
raise HTTPException(status_code=401, detail=messages.notauthenticated)
|
||||
raise HTTPException(status_code=401, detail=messages.Messages.notauthenticated)
|
||||
payload = verify_token(access_token)
|
||||
if not payload:
|
||||
raise HTTPException(status_code=401, detail='aze')
|
||||
email = payload.get('email')
|
||||
|
||||
if not email:
|
||||
raise HTTPException(status_code=401, detail=messages.notauthenticated)
|
||||
raise HTTPException(status_code=401, detail=messages.Messages.notauthenticated)
|
||||
|
||||
user = session.exec(select(User).where(User.email == email)).first()
|
||||
if not user:
|
||||
raise HTTPException(status_code=401, detail=messages.usernotfound)
|
||||
raise HTTPException(status_code=401, detail=messages.Messages.not_found('user'))
|
||||
return user
|
||||
|
||||
@router.post('/refresh')
|
||||
@@ -191,8 +191,8 @@ def refresh_token(refresh_token: Annotated[str | None, Cookie()] = None):
|
||||
result = requests.post(TOKEN_URL, data=data, headers=headers)
|
||||
if result.status_code != 200:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=messages.failtogettoken
|
||||
status_code=404,
|
||||
detail=messages.Messages.not_found('token')
|
||||
)
|
||||
|
||||
token_data = result.json()
|
||||
|
||||
@@ -81,8 +81,6 @@ async def create_contract(
|
||||
recurrent_price = compute_recurrent_prices(recurrents, len(new_contract.form.shipments))
|
||||
price = recurrent_price + compute_occasional_prices(occasionals)
|
||||
cheques = list(map(lambda x: {"name": x.name, "value": x.value}, new_contract.cheques))
|
||||
# TODO: send contract to referer
|
||||
|
||||
try:
|
||||
pdf_bytes = generate_html_contract(
|
||||
new_contract,
|
||||
@@ -165,10 +163,10 @@ def get_contract_file(
|
||||
user: models.User = Depends(get_current_user)
|
||||
):
|
||||
if not service.is_allowed(session, user, id):
|
||||
raise HTTPException(status_code=403, detail=messages.notallowed)
|
||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contract', 'get'))
|
||||
contract = service.get_one(session, id)
|
||||
if contract is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('contract'))
|
||||
filename = f'{contract.form.name.replace(' ', '_')}_{contract.form.season}_{contract.firstname}-{contract.lastname}'
|
||||
return StreamingResponse(
|
||||
io.BytesIO(contract.file),
|
||||
@@ -185,7 +183,7 @@ def get_contract_files(
|
||||
user: models.User = Depends(get_current_user)
|
||||
):
|
||||
if not form_service.is_allowed(session, user, form_id):
|
||||
raise HTTPException(status_code=403, detail=messages.notallowed)
|
||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contracts', 'get'))
|
||||
form = form_service.get_one(session, form_id=form_id)
|
||||
contracts = service.get_all(session, user, forms=[form.name])
|
||||
zipped_contracts = io.BytesIO()
|
||||
@@ -210,7 +208,7 @@ def get_contract_recap(
|
||||
user: models.User = Depends(get_current_user)
|
||||
):
|
||||
if not form_service.is_allowed(session, user, form_id):
|
||||
raise HTTPException(status_code=403, detail=messages.notallowed)
|
||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contract recap', 'get'))
|
||||
form = form_service.get_one(session, form_id=form_id)
|
||||
contracts = service.get_all(session, user, forms=[form.name])
|
||||
|
||||
@@ -225,17 +223,17 @@ def get_contract_recap(
|
||||
@router.get('/{id}', response_model=models.ContractPublic)
|
||||
def get_contract(id: int, session: Session = Depends(get_session), user: models.User = Depends(get_current_user)):
|
||||
if not service.is_allowed(session, user, id):
|
||||
raise HTTPException(status_code=403, detail=messages.notallowed)
|
||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contract', 'get'))
|
||||
result = service.get_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('contract'))
|
||||
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)):
|
||||
if not service.is_allowed(session, user, id):
|
||||
raise HTTPException(status_code=403, detail=messages.notallowed)
|
||||
raise HTTPException(status_code=403, detail=messages.Messages.not_allowed('contract', 'delete'))
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('contract'))
|
||||
return result
|
||||
|
||||
@@ -32,7 +32,7 @@ async def get_forms_filtered(
|
||||
async def get_form(id: int, session: Session = Depends(get_session)):
|
||||
result = service.get_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('form'))
|
||||
return result
|
||||
|
||||
@router.post('', response_model=models.FormPublic)
|
||||
@@ -43,12 +43,12 @@ async def create_form(
|
||||
):
|
||||
try:
|
||||
form = service.create_one(session, form)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
except exceptions.UserNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.usernotfound)
|
||||
except exceptions.FormCreateError:
|
||||
raise HTTPException(status_code=400, detail=messages.forminputinvalid)
|
||||
except exceptions.ProductorNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
except exceptions.UserNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
except exceptions.FormCreateError as error:
|
||||
raise HTTPException(status_code=400, detail=str(error))
|
||||
return form
|
||||
|
||||
@router.put('/{id}', response_model=models.FormPublic)
|
||||
@@ -59,12 +59,12 @@ async def update_form(
|
||||
):
|
||||
try:
|
||||
result = service.update_one(session, id, form)
|
||||
except exceptions.FormNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
except exceptions.UserNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.usernotfound)
|
||||
except exceptions.FormNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
except exceptions.ProductorNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
except exceptions.UserNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.FormPublic)
|
||||
@@ -75,6 +75,6 @@ async def delete_form(
|
||||
):
|
||||
try:
|
||||
result = service.delete_one(session, id)
|
||||
except exceptions.FormNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
except exceptions.FormNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
return result
|
||||
|
||||
@@ -3,6 +3,7 @@ from sqlalchemy import func
|
||||
|
||||
import src.models as models
|
||||
import src.forms.exceptions as exceptions
|
||||
import src.messages as messages
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
@@ -49,11 +50,11 @@ def get_one(session: Session, form_id: int) -> models.FormPublic:
|
||||
|
||||
def create_one(session: Session, form: models.FormCreate) -> models.FormPublic:
|
||||
if not form:
|
||||
raise exceptions.FormCreateError('FormCreate 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):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {form.productor_id} not found')
|
||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
||||
if not session.get(models.User, form.referer_id):
|
||||
raise exceptions.UserNotFoundError(f'User {form.referer_id} not found')
|
||||
raise exceptions.UserNotFoundError(messages.Messages.not_found('user'))
|
||||
form_create = form.model_dump(exclude_unset=True)
|
||||
new_form = models.Form(**form_create)
|
||||
session.add(new_form)
|
||||
@@ -66,11 +67,11 @@ def update_one(session: Session, id: int, form: models.FormUpdate) -> models.For
|
||||
result = session.exec(statement)
|
||||
new_form = result.first()
|
||||
if not new_form:
|
||||
raise exceptions.FormNotFoundError(f'Form {id} not found')
|
||||
raise exceptions.FormNotFoundError(messages.Messages.not_found('form'))
|
||||
if form.productor_id and not session.get(models.Productor, form.productor_id):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {form.productor_id} not found')
|
||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
||||
if form.referer_id and not session.get(models.User, form.referer_id):
|
||||
raise exceptions.UserNotFoundError(f'User {form.referer_id} not found')
|
||||
raise exceptions.UserNotFoundError(messages.Messages.not_found('user'))
|
||||
form_updates = form.model_dump(exclude_unset=True)
|
||||
for key, value in form_updates.items():
|
||||
setattr(new_form, key, value)
|
||||
@@ -84,7 +85,7 @@ def delete_one(session: Session, id: int) -> models.FormPublic:
|
||||
result = session.exec(statement)
|
||||
form = result.first()
|
||||
if not form:
|
||||
raise exceptions.FormNotFoundError(f'Form {id} not found')
|
||||
raise exceptions.FormNotFoundError(messages.Messages.not_found('form'))
|
||||
result = models.FormPublic.model_validate(form)
|
||||
session.delete(form)
|
||||
session.commit()
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
pdferror = 'An error occured during PDF generation please contact administrator'
|
||||
|
||||
tokenexipired = 'Token expired'
|
||||
invalidtoken = 'Invalid token'
|
||||
notauthenticated = 'Not authenticated'
|
||||
failtogettoken = 'Failed to get token'
|
||||
unauthorized = 'Unauthorized'
|
||||
notallowed = 'Not Allowed'
|
||||
class Messages:
|
||||
unauthorized = 'User is Unauthorized'
|
||||
notauthenticated = 'User is not authenticated'
|
||||
tokenexipired = 'Token has expired'
|
||||
invalidtoken = 'Token is invalid'
|
||||
|
||||
@staticmethod
|
||||
def not_found(resource: str) -> str:
|
||||
return f'{resource.capitalize()} not found'
|
||||
|
||||
@staticmethod
|
||||
def invalid_input(resource: str, reason: str = "") -> str:
|
||||
return f'Invalid {resource} input {':' if reason else ""} {reason}'
|
||||
|
||||
notfound = 'Resource was not found.'
|
||||
usernotfound = 'User not found'
|
||||
userloggedout = 'User logged out'
|
||||
|
||||
productorinputinvalid = 'Invalid productor input'
|
||||
productornotfound = 'Productor not found'
|
||||
|
||||
forminputinvalid = 'Invalid form input'
|
||||
formnotfound = 'Form not found'
|
||||
|
||||
productinputinvalid = 'Invalid product input'
|
||||
productnotfound = 'Product not found'
|
||||
@staticmethod
|
||||
def not_allowed(resource: str, action: str) -> str:
|
||||
return f'User is not allowed to {action} this {resource}'
|
||||
@@ -303,7 +303,7 @@ class Shipment(ShipmentBase, table=True):
|
||||
|
||||
class ShipmentUpdate(SQLModel):
|
||||
name: str | None
|
||||
date: str | None
|
||||
date: datetime.date | None
|
||||
product_ids: list[int] | None = []
|
||||
|
||||
class ShipmentCreate(ShipmentBase):
|
||||
|
||||
@@ -26,7 +26,7 @@ def get_productor(
|
||||
):
|
||||
result = service.get_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('productor'))
|
||||
return result
|
||||
|
||||
@router.post('', response_model=models.ProductorPublic)
|
||||
@@ -37,8 +37,8 @@ def create_productor(
|
||||
):
|
||||
try:
|
||||
result = service.create_one(session, productor)
|
||||
except exceptions.ProductorCreateError:
|
||||
raise HTTPException(status_code=400, detail=messages.productorinputinvalid)
|
||||
except exceptions.ProductorCreateError as error:
|
||||
raise HTTPException(status_code=400, detail=str(error))
|
||||
return result
|
||||
|
||||
@router.put('/{id}', response_model=models.ProductorPublic)
|
||||
@@ -49,8 +49,8 @@ def update_productor(
|
||||
):
|
||||
try:
|
||||
result = service.update_one(session, id, productor)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
except exceptions.ProductorNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.ProductorPublic)
|
||||
@@ -61,6 +61,6 @@ def delete_productor(
|
||||
):
|
||||
try:
|
||||
result = service.delete_one(session, id)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
except exceptions.ProductorNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
return result
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from sqlmodel import Session, select
|
||||
import src.models as models
|
||||
import src.productors.exceptions as exceptions
|
||||
import src.messages as messages
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
@@ -22,7 +23,7 @@ def get_one(session: Session, productor_id: int) -> models.ProductorPublic:
|
||||
|
||||
def create_one(session: Session, productor: models.ProductorCreate) -> models.ProductorPublic:
|
||||
if not productor:
|
||||
raise exceptions.ProductorCreateError('ProductorCreate input cannot be None')
|
||||
raise exceptions.ProductorCreateError(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)
|
||||
|
||||
@@ -43,7 +44,7 @@ def update_one(session: Session, id: int, productor: models.ProductorUpdate) ->
|
||||
result = session.exec(statement)
|
||||
new_productor = result.first()
|
||||
if not new_productor:
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {id} not found')
|
||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
||||
|
||||
productor_updates = productor.model_dump(exclude_unset=True)
|
||||
if 'payment_methods' in productor_updates:
|
||||
@@ -71,7 +72,7 @@ def delete_one(session: Session, id: int) -> models.ProductorPublic:
|
||||
result = session.exec(statement)
|
||||
productor = result.first()
|
||||
if not productor:
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {id} not found')
|
||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
||||
result = models.ProductorPublic.model_validate(productor)
|
||||
session.delete(productor)
|
||||
session.commit()
|
||||
|
||||
@@ -33,7 +33,7 @@ def get_product(
|
||||
):
|
||||
result = service.get_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('product'))
|
||||
return result
|
||||
|
||||
@router.post('', response_model=models.ProductPublic)
|
||||
@@ -44,10 +44,10 @@ def create_product(
|
||||
):
|
||||
try:
|
||||
result = service.create_one(session, product)
|
||||
except exceptions.ProductCreateError:
|
||||
raise HTTPException(status_code=400, detail=messages.productinputinvalid)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
except exceptions.ProductCreateError as error:
|
||||
raise HTTPException(status_code=400, detail=str(error))
|
||||
except exceptions.ProductorNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
return result
|
||||
|
||||
@router.put('/{id}', response_model=models.ProductPublic)
|
||||
@@ -58,10 +58,10 @@ def update_product(
|
||||
):
|
||||
try:
|
||||
result = service.update_one(session, id, product)
|
||||
except exceptions.ProductNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productnotfound)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
except exceptions.ProductNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
except exceptions.ProductorNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.ProductPublic)
|
||||
@@ -72,6 +72,6 @@ def delete_product(
|
||||
):
|
||||
try:
|
||||
result = service.delete_one(session, id)
|
||||
except exceptions.ProductNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
except exceptions.ProductNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
return result
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from sqlmodel import Session, select
|
||||
import src.models as models
|
||||
import src.products.exceptions as exceptions
|
||||
import src.messages as messages
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
@@ -26,9 +27,9 @@ def get_one(session: Session, product_id: int) -> models.ProductPublic:
|
||||
|
||||
def create_one(session: Session, product: models.ProductCreate) -> models.ProductPublic:
|
||||
if not product:
|
||||
raise exceptions.ProductCreateError('ProductCreate 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):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {product.productor_id} not found')
|
||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
||||
product_create = product.model_dump(exclude_unset=True)
|
||||
new_product = models.Product(**product_create)
|
||||
session.add(new_product)
|
||||
@@ -41,9 +42,9 @@ def update_one(session: Session, id: int, product: models.ProductUpdate) -> mode
|
||||
result = session.exec(statement)
|
||||
new_product = result.first()
|
||||
if not new_product:
|
||||
raise exceptions.ProductNotFoundError(f'Product {id} not found')
|
||||
raise exceptions.ProductNotFoundError(messages.Messages.not_found('product'))
|
||||
if product.productor_id and not session.get(models.Productor, product.productor_id):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {product.productor_id} not found')
|
||||
raise exceptions.ProductorNotFoundError(messages.Messages.not_found('productor'))
|
||||
|
||||
product_updates = product.model_dump(exclude_unset=True)
|
||||
for key, value in product_updates.items():
|
||||
@@ -59,7 +60,7 @@ def delete_one(session: Session, id: int) -> models.ProductPublic:
|
||||
result = session.exec(statement)
|
||||
product = result.first()
|
||||
if not product:
|
||||
raise exceptions.ProductNotFoundError(f'Product {id} not found')
|
||||
raise exceptions.ProductNotFoundError(messages.Messages.not_found('product'))
|
||||
result = models.ProductPublic.model_validate(product)
|
||||
session.delete(product)
|
||||
session.commit()
|
||||
|
||||
11
backend/src/shipments/exceptions.py
Normal file
11
backend/src/shipments/exceptions.py
Normal file
@@ -0,0 +1,11 @@
|
||||
class ShipmentServiceError(Exception):
|
||||
def __init__(self, message: str):
|
||||
super().__init__(message)
|
||||
|
||||
class ShipmentNotFoundError(ShipmentServiceError):
|
||||
pass
|
||||
|
||||
class ShipmentCreateError(ShipmentServiceError):
|
||||
def __init__(self, message: str, field: str | None = None):
|
||||
super().__init__(message)
|
||||
self.field = field
|
||||
@@ -1,12 +1,15 @@
|
||||
from sqlmodel import Session, select
|
||||
import src.models as models
|
||||
import src.shipments.exceptions as exceptions
|
||||
import src.messages as messages
|
||||
import datetime
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
user: models.User,
|
||||
names: list[str],
|
||||
dates: list[str],
|
||||
forms: list[int]
|
||||
forms: list[str]
|
||||
) -> list[models.ShipmentPublic]:
|
||||
statement = select(models.Shipment)\
|
||||
.join(models.Form, models.Shipment.form_id == models.Form.id)\
|
||||
@@ -16,15 +19,17 @@ def get_all(
|
||||
if len(names) > 0:
|
||||
statement = statement.where(models.Shipment.name.in_(names))
|
||||
if len(dates) > 0:
|
||||
statement = statement.where(models.Shipment.date.in_(list(map(lambda x: datetime.strptime(x, '%Y-%m-%d'), dates))))
|
||||
statement = statement.where(models.Shipment.date.in_(list(map(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d').date(), dates))))
|
||||
if len(forms) > 0:
|
||||
statement = statement.join(models.Form).where(models.Form.name.in_(forms))
|
||||
statement = statement.where(models.Form.name.in_(forms))
|
||||
return session.exec(statement.order_by(models.Shipment.name)).all()
|
||||
|
||||
def get_one(session: Session, shipment_id: int) -> models.ShipmentPublic:
|
||||
return session.get(models.Shipment, shipment_id)
|
||||
|
||||
def create_one(session: Session, shipment: models.ShipmentCreate) -> models.ShipmentPublic:
|
||||
if shipment is None:
|
||||
raise exceptions.ShipmentCreateError(messages.Messages.invalid_input('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)
|
||||
@@ -34,11 +39,13 @@ def create_one(session: Session, shipment: models.ShipmentCreate) -> models.Ship
|
||||
return new_shipment
|
||||
|
||||
def update_one(session: Session, id: int, shipment: models.ShipmentUpdate) -> models.ShipmentPublic:
|
||||
if shipment is None:
|
||||
raise exceptions.ShipmentCreateError(messages.Messages.invalid_input('shipment', 'input cannot be None'))
|
||||
statement = select(models.Shipment).where(models.Shipment.id == id)
|
||||
result = session.exec(statement)
|
||||
new_shipment = result.first()
|
||||
if not new_shipment:
|
||||
return None
|
||||
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()
|
||||
new_shipment.products.clear()
|
||||
@@ -59,7 +66,8 @@ def delete_one(session: Session, id: int) -> models.ShipmentPublic:
|
||||
result = session.exec(statement)
|
||||
shipment = result.first()
|
||||
if not shipment:
|
||||
return None
|
||||
raise exceptions.ShipmentNotFoundError(messages.Messages.not_found('shipment'))
|
||||
|
||||
result = models.ShipmentPublic.model_validate(shipment)
|
||||
session.delete(shipment)
|
||||
session.commit()
|
||||
|
||||
@@ -4,6 +4,7 @@ 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
|
||||
from src.auth.auth import get_current_user
|
||||
|
||||
router = APIRouter(prefix='/shipments')
|
||||
@@ -32,7 +33,7 @@ def get_shipment(
|
||||
):
|
||||
result = service.get_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('shipment'))
|
||||
return result
|
||||
|
||||
@router.post('', response_model=models.ShipmentPublic)
|
||||
@@ -41,17 +42,23 @@ def create_shipment(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
return service.create_one(session, shipment)
|
||||
try:
|
||||
result = service.create_one(session, shipment)
|
||||
except exceptions.ShipmentCreateError as error:
|
||||
raise HTTPException(status_code=400, detail=str(error))
|
||||
return result
|
||||
|
||||
@router.put('/{id}', response_model=models.ShipmentPublic)
|
||||
def update_shipment(
|
||||
id: int, shipment: models.ShipmentUpdate,
|
||||
id: int,
|
||||
shipment: models.ShipmentUpdate,
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.update_one(session, id, shipment)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
try:
|
||||
result = service.update_one(session, id, shipment)
|
||||
except exceptions.ShipmentNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.ShipmentPublic)
|
||||
@@ -60,7 +67,8 @@ def delete_shipment(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
try:
|
||||
result = service.delete_one(session, id)
|
||||
except exceptions.ShipmentNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error))
|
||||
return result
|
||||
|
||||
@@ -23,7 +23,7 @@ def get_template(
|
||||
):
|
||||
result = service.get_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('template'))
|
||||
return result
|
||||
|
||||
@router.post('', response_model=models.TemplatePublic)
|
||||
@@ -42,7 +42,7 @@ def update_template(
|
||||
):
|
||||
result = service.update_one(session, id, template)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('template'))
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.TemplatePublic)
|
||||
@@ -53,5 +53,5 @@ def delete_template(
|
||||
):
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('template'))
|
||||
return result
|
||||
|
||||
11
backend/src/users/exceptions.py
Normal file
11
backend/src/users/exceptions.py
Normal file
@@ -0,0 +1,11 @@
|
||||
class UserServiceError(Exception):
|
||||
def __init__(self, message: str):
|
||||
super().__init__(message)
|
||||
|
||||
class UserNotFoundError(UserServiceError):
|
||||
pass
|
||||
|
||||
class UserCreateError(UserServiceError):
|
||||
def __init__(self, message: str, field: str | None = None):
|
||||
super().__init__(message)
|
||||
self.field = field
|
||||
@@ -1,5 +1,9 @@
|
||||
from sqlmodel import Session, select
|
||||
|
||||
import src.models as models
|
||||
import src.messages as messages
|
||||
|
||||
import src.users.exceptions as exceptions
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
@@ -49,6 +53,8 @@ def get_roles(session: Session):
|
||||
return session.exec(statement.order_by(models.ContractType.name)).all()
|
||||
|
||||
def create_one(session: Session, user: models.UserCreate) -> models.UserPublic:
|
||||
if user is None:
|
||||
raise exceptions.UserCreateError(messages.Messages.invalid_input('user', 'input cannot be None'))
|
||||
new_user = models.User(
|
||||
name=user.name,
|
||||
email=user.email
|
||||
@@ -63,12 +69,13 @@ def create_one(session: Session, user: models.UserCreate) -> models.UserPublic:
|
||||
return new_user
|
||||
|
||||
def update_one(session: Session, id: int, user: models.UserCreate) -> models.UserPublic:
|
||||
if user is None:
|
||||
raise exceptions.UserCreateError(messages.s.invalid_input('user', 'input cannot be None'))
|
||||
statement = select(models.User).where(models.User.id == id)
|
||||
result = session.exec(statement)
|
||||
new_user = result.first()
|
||||
if not new_user:
|
||||
return None
|
||||
|
||||
raise exceptions.UserNotFoundError(f'User {id} not found')
|
||||
new_user.email = user.email
|
||||
new_user.name = user.name
|
||||
|
||||
@@ -84,7 +91,7 @@ def delete_one(session: Session, id: int) -> models.UserPublic:
|
||||
result = session.exec(statement)
|
||||
user = result.first()
|
||||
if not user:
|
||||
return None
|
||||
raise exceptions.UserNotFoundError(f'User {id} not found')
|
||||
result = models.UserPublic.model_validate(user)
|
||||
session.delete(user)
|
||||
session.commit()
|
||||
|
||||
@@ -5,6 +5,7 @@ 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
|
||||
|
||||
router = APIRouter(prefix='/users')
|
||||
|
||||
@@ -36,7 +37,7 @@ def get_users(
|
||||
):
|
||||
result = service.get_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('user'))
|
||||
return result
|
||||
|
||||
@router.post('', response_model=models.UserPublic)
|
||||
@@ -45,7 +46,11 @@ def create_user(
|
||||
logged_user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
return service.create_one(session, user)
|
||||
try:
|
||||
user = service.create_one(session, user)
|
||||
except exceptions.UserCreateError as error:
|
||||
raise HTTPException(status_code=400, detail=str(error))
|
||||
return user
|
||||
|
||||
@router.put('/{id}', response_model=models.UserPublic)
|
||||
def update_user(
|
||||
@@ -54,9 +59,10 @@ def update_user(
|
||||
logged_user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.update_one(session, id, user)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
try:
|
||||
result = service.update_one(session, id, user)
|
||||
except exceptions.UserNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('user'))
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.UserPublic)
|
||||
@@ -65,7 +71,8 @@ def delete_user(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
try:
|
||||
result = service.delete_one(session, id)
|
||||
except exceptions.UserNotFoundError as error:
|
||||
raise HTTPException(status_code=404, detail=messages.Messages.not_found('user'))
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user