Skip to content

Commit

Permalink
HAN-50: fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Agustinefe committed Mar 7, 2024
1 parent 20c9567 commit 296393c
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 60 deletions.
4 changes: 4 additions & 0 deletions app/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.analysis.autoImportCompletions": true,
"python.analysis.typeCheckingMode": "basic"
}
68 changes: 48 additions & 20 deletions app/controller/device_plant_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
from schemas.device_plant import (
DevicePlantCreateSchema,
DevicePlantPartialUpdateSchema,
DevicePlantSchema,
DevicePlantUpdateSchema,
)
import logging
from psycopg2.errors import UniqueViolation
from sqlalchemy.exc import PendingRollbackError, IntegrityError, NoResultFound
from fastapi.responses import JSONResponse
from service.plant_service import PlantService

logger = logging.getLogger("app")
logger.setLevel("DEBUG")


def handle_common_errors(err):

if isinstance(err, IntegrityError):
Expand All @@ -24,23 +23,25 @@ def handle_common_errors(err):
status_code=status.HTTP_400_BAD_REQUEST,
content=parsed_error,
)

if isinstance(err, PendingRollbackError):
logger.warning(format(err))
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=format(err)
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=format(err)
)

if isinstance(err, NoResultFound):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail=format(err)
)

logger.error(format(err))
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=format(err)
)


def withSQLExceptionsHandle(async_mode: bool):

def decorator(func):
Expand All @@ -56,37 +57,51 @@ def handleSyncSQLException(*args, **kwargs):
except Exception as err:
return handle_common_errors(err)

return handleAsyncSQLException if async_mode else handleSyncSQLException

return (
handleAsyncSQLException if async_mode else handleSyncSQLException
)

return decorator


@withSQLExceptionsHandle(async_mode=True)
async def create_device_plant_relation(req: Request, device_plant: DevicePlantCreateSchema):
async def create_device_plant_relation(
req: Request, device_plant: DevicePlantCreateSchema
):
try:
plant = await PlantService.get_plant(device_plant.id_plant)
if not plant:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"plant_id": f"Could not found any plant with id {device_plant.id_plant}"},
content={
"plant_id": (
"Could not found any plant "
f"with id {device_plant.id_plant}"
)
},
)

plant_type = await PlantService.get_plant_type(plant.scientific_name)
if not plant_type:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"scientific_name": f"Could not found any plant type with scientific name {plant.scientific_name}"},
content={
"scientific_name": (
"Could not found any plant type "
f"with scientific name {plant.scientific_name}"
)
},
)

device_plant = DevicePlant(
id_device=device_plant.id_device,
id_plant=device_plant.id_plant,
plant_type=plant_type.id,
id_user=plant.id_user,
)
) # type: ignore
req.app.database.add(device_plant)
return device_plant

except Exception as err:
req.app.database.rollback()
raise err
Expand All @@ -108,14 +123,24 @@ async def update_device_plant(
if not plant:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"plant_id": f"Could not found any plant with id {device_plant_update_set.id_plant}"},
content={
"plant_id": (
"Could not found any plant with "
f"id {device_plant_update_set.id_plant}"
)
},
)

plant_type = await PlantService.get_plant_type(plant.scientific_name)
if not plant_type:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"scientific_name": f"Could not found any plant type with scientific name {plant.scientific_name}"},
content={
"scientific_name": (
"Could not found any plant type "
f"with scientific name {plant.scientific_name}"
)
},
)

req.app.database.update_device_plant(
Expand All @@ -131,7 +156,7 @@ async def update_device_plant(


@withSQLExceptionsHandle(async_mode=False)
def get_device_plant_relation(req: Request, id_plant: str):
def get_device_plant_relation(req: Request, id_plant: int):
return req.app.database.find_by_plant_id(id_plant)


Expand All @@ -142,7 +167,10 @@ def get_all_device_plant_relations(req: Request, limit: int):

@withSQLExceptionsHandle(async_mode=False)
def delete_device_plant_relation(
req: Request, response: Response, type_id: Literal["id_device", "id_plant"], id: str
req: Request,
response: Response,
type_id: Literal["id_device", "id_plant"],
id: str
):
result_rowcount = 0
if type_id == "id_device":
Expand Down
2 changes: 1 addition & 1 deletion app/controller/measurement_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi import Request


@withSQLExceptionsHandle
@withSQLExceptionsHandle(async_mode=False)
def last_measurement_made_by_plant(req: Request, id_plant: int):
try:
return req.app.database.get_last_measurement(id_plant)
Expand Down
13 changes: 5 additions & 8 deletions app/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from sqlalchemy.orm import Session
from dotenv import load_dotenv
from os import environ
from typing import Optional, Union
from typing import Optional, Sequence, Union

from database.models.device_plant import DevicePlant
from database.models.measurement import Measurement
from typing import List

load_dotenv()

Expand All @@ -18,7 +17,7 @@ class SQLAlchemyClient():
username=environ["POSTGRES_USER"],
password=environ["POSTGRES_PASSWORD"],
host=environ["POSTGRES_HOST"],
port=environ["POSTGRES_PORT"]
port=int(environ["POSTGRES_PORT"])
)

engine = create_engine(db_url)
Expand All @@ -35,7 +34,7 @@ def rollback(self):
self.session.rollback()

def clean_table(self, table: Union[DevicePlant, Measurement]):
query = delete(table)
query = delete(table) # type: ignore
self.session.execute(query)
self.session.commit()

Expand All @@ -53,9 +52,9 @@ def find_by_plant_id(self, id_plant: str) -> DevicePlant:
result = self.session.scalars(query).one()
return result

def find_all(self, limit: int) -> List[DevicePlant]:
def find_all(self, limit: int) -> Sequence[DevicePlant]:
query = select(DevicePlant).limit(limit)
result = self.session.scalars(query)
result = self.session.scalars(query).all()
return result

def update_device_plant(self,
Expand All @@ -79,9 +78,7 @@ def get_last_measurement(self, id_plant: int) -> Measurement:
query = select(Measurement).where(
Measurement.id_plant == id_plant
).order_by(Measurement.id.desc()).limit(1)

result: Measurement = self.session.scalars(query).one()

return result

def delete_by_field(self, field: str, value: str) -> int:
Expand Down
1 change: 0 additions & 1 deletion app/database/models/device_plant.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class DevicePlant(Base):
plant_type: Mapped[int] = mapped_column(SmallInteger)
id_user: Mapped[int] = mapped_column(Integer)


def __repr__(self) -> str:
return (f"DevicePlant(id_device={self.id_device}, "
f"id_plant={self.id_plant}, "
Expand Down
10 changes: 8 additions & 2 deletions app/database/models/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ class Measurement(Base):
watering: Mapped[Optional[int]] = mapped_column(SmallInteger)

__table_args__ = (
CheckConstraint('humidity >= 0 AND humidity <= 100', name='check_humidity'),
CheckConstraint(
'humidity >= 0 AND humidity <= 100',
name='check_humidity'
),
CheckConstraint('humidity >= 0 ', name='check_light'),
CheckConstraint('humidity >= 0 AND humidity <= 100', name='check_watering'),
CheckConstraint(
'humidity >= 0 AND humidity <= 100',
name='check_watering'
),
{'schema': 'dev'}
)

Expand Down
5 changes: 4 additions & 1 deletion app/exceptions/empty_package.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class EmptyPackageError(Exception):
def __init__(self, empty_folds: list):
self.empty_folds = empty_folds
super().__init__(f"Package received with empty folds: {self.empty_folds}.")
super().__init__((
"Package received with empty "
f"folds: {self.empty_folds}."
))
44 changes: 28 additions & 16 deletions app/router/device_plant_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
status_code=status.HTTP_201_CREATED,
response_model=DevicePlantSchema
)
async def create_device_plant_relation(req: Request,
device_plant: DevicePlantCreateSchema = Body(...)):
async def create_device_plant_relation(
req: Request,
device_plant: DevicePlantCreateSchema = Body(...)
):
return await controller.create_device_plant_relation(req, device_plant)


Expand All @@ -27,21 +29,27 @@ async def create_device_plant_relation(req: Request,
status_code=status.HTTP_200_OK,
response_model=DevicePlantSchema
)
async def update_fields_in_device_plant(id_device: str,
req: Request,
device_plant_update_set:
DevicePlantPartialUpdateSchema = Body(...)):
return await controller.update_device_plant(req, id_device, device_plant_update_set)
async def update_fields_in_device_plant(
id_device: str,
req: Request,
device_plant_update_set:
DevicePlantPartialUpdateSchema = Body(...)
):
return await controller.update_device_plant(
req, id_device, device_plant_update_set
)


@device_plant.put(
"/{id_device}",
status_code=status.HTTP_200_OK,
response_model=DevicePlantSchema
)
async def update_all_in_device_plant(id_device: str,
req: Request,
device_plant: DevicePlantUpdateSchema = Body(...)):
async def update_all_in_device_plant(
id_device: str,
req: Request,
device_plant: DevicePlantUpdateSchema = Body(...)
):
return await controller.update_device_plant(req, id_device, device_plant)


Expand All @@ -50,9 +58,11 @@ async def update_all_in_device_plant(id_device: str,
status_code=status.HTTP_200_OK,
response_model=List[DevicePlantSchema]
)
async def get_device_plant(req: Request,
id_plant: int = Query(None),
limit: int = Query(10)):
async def get_device_plant(
req: Request,
id_plant: int = Query(None),
limit: int = Query(10)
):
if id_plant is None:
return controller.get_all_device_plant_relations(req, limit)
return [controller.get_device_plant_relation(req, id_plant)]
Expand All @@ -62,7 +72,9 @@ async def get_device_plant(req: Request,
"/{id}",
status_code=status.HTTP_200_OK
)
async def delete_device_plant_relation(response: Response, req: Request,
type_id: Literal["id_device", "id_plant"],
id: str):
async def delete_device_plant_relation(
response: Response, req: Request,
type_id: Literal["id_device", "id_plant"],
id: str
):
return controller.delete_device_plant_relation(req, response, type_id, id)
2 changes: 2 additions & 0 deletions app/schemas/device_plant.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydantic import BaseModel, Field
from typing import Optional


class DevicePlantCreateSchema(BaseModel):
id_device: str = Field(...)
id_plant: int = Field(..., gt=0)
Expand All @@ -13,6 +14,7 @@ class Config:
}
}


class DevicePlantSchema(DevicePlantCreateSchema):
plant_type: int = Field(..., gt=0)
id_user: int = Field(..., gt=0)
Expand Down
3 changes: 1 addition & 2 deletions app/schemas/plant.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pydantic import BaseModel, Field
from typing import Optional


class PlantSchema(BaseModel):
id: int = Field(...)
name: str = Field(...)
scientific_name: str = Field(...)
id_user: int = Field(...)
id_user: int = Field(...)
3 changes: 2 additions & 1 deletion app/schemas/plant_type.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from pydantic import BaseModel, Field


class PlantTypeSchema(BaseModel):
botanical_name: str = Field(..., max_length=70)
id: int = Field(..., gt=0)
common_name: str = Field(..., max_length=70)
description: str = Field(..., max_length=1000)
cares: str = Field(..., max_length=1000)
photo_link: str = Field(..., max_length=160)
photo_link: str = Field(..., max_length=160)
4 changes: 3 additions & 1 deletion app/service/calculator_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

class CalculatorService:
def __init__(self):
engine = create_engine(os.environ.get('DATABASE_URL'), echo=True)
engine = create_engine(
os.environ.get('DATABASE_URL'), echo=True # type: ignore
)
self.session = Session(engine)

def add_number(self, number):
Expand Down
Loading

0 comments on commit 296393c

Please sign in to comment.