Skip to content

Commit 7dcb2a5

Browse files
committed
Change: Camera api model hierarchy
1 parent 1075f5b commit 7dcb2a5

3 files changed

Lines changed: 75 additions & 51 deletions

File tree

src/api/api.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,8 @@ async def get_camera(camera_id: int):
221221
)
222222

223223
@self.app.put("/cameras/{camera_id}")
224-
async def update_camera(camera_id: int, updated_fields: Request):
224+
async def update_camera(camera_id: int, updated_fields: CameraBase):
225225
try:
226-
updated_fields = await updated_fields.json()
227-
228226
camera = self.db_manager.update_camera(camera_id, updated_fields)
229227

230228
if camera is None:

src/api/models.py

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,74 @@
22
from pydantic import BaseModel, field_validator, Field
33
import json
44

5-
class CreateCamera(BaseModel):
6-
title: str
7-
source: str
8-
image_width: int
9-
image_height: int
10-
calib: Any
11-
latitude: float
12-
longitude: float
5+
# class CreateCamera(BaseModel):
6+
# title: str
7+
# source: str
8+
# image_width: int
9+
# image_height: int
10+
# calib: Any
11+
# latitude: float
12+
# longitude: float
1313

14-
@field_validator('title')
15-
@classmethod
16-
def validate_title(cls, title):
17-
if len(title) < 1 or len(title) > 200:
18-
raise ValueError(f"Invalid camera title: {title}")
19-
return title
14+
# @field_validator('title')
15+
# @classmethod
16+
# def validate_title(cls, title):
17+
# if len(title) < 1 or len(title) > 200:
18+
# raise ValueError(f"Invalid camera title: {title}")
19+
# return title
2020

21-
@field_validator('source')
22-
@classmethod
23-
def validate_source(cls, source):
24-
return source
21+
# @field_validator('source')
22+
# @classmethod
23+
# def validate_source(cls, source):
24+
# return source
2525

26-
@field_validator('latitude')
27-
@classmethod
28-
def validate_latitude(cls, latitude):
29-
if latitude > 90 or latitude < -90:
30-
raise ValueError(f"Invalid latitude value: {latitude}")
31-
return latitude
26+
# @field_validator('latitude')
27+
# @classmethod
28+
# def validate_latitude(cls, latitude):
29+
# if latitude > 90 or latitude < -90:
30+
# raise ValueError(f"Invalid latitude value: {latitude}")
31+
# return latitude
3232

33-
@field_validator('longitude')
34-
@classmethod
35-
def validate_longitude(cls, longitude):
36-
if longitude > 180 or longitude < -180:
37-
raise ValueError(f"Invalid longitude value: {longitude}")
38-
return longitude
33+
# @field_validator('longitude')
34+
# @classmethod
35+
# def validate_longitude(cls, longitude):
36+
# if longitude > 180 or longitude < -180:
37+
# raise ValueError(f"Invalid longitude value: {longitude}")
38+
# return longitude
3939

40-
@field_validator('image_width')
41-
@classmethod
42-
def validate_image_width(cls, image_width):
43-
if image_width <= 0:
44-
raise ValueError(f"Invalid image_width value: {image_width}")
45-
return image_width
40+
# @field_validator('image_width')
41+
# @classmethod
42+
# def validate_image_width(cls, image_width):
43+
# if image_width <= 0:
44+
# raise ValueError(f"Invalid image_width value: {image_width}")
45+
# return image_width
4646

47-
@field_validator('image_height')
48-
@classmethod
49-
def validate_image_height(cls, image_height):
50-
if image_height <= 0:
51-
raise ValueError(f"Invalid image_height value: {image_height}")
52-
return image_height
47+
# @field_validator('image_height')
48+
# @classmethod
49+
# def validate_image_height(cls, image_height):
50+
# if image_height <= 0:
51+
# raise ValueError(f"Invalid image_height value: {image_height}")
52+
# return image_height
5353

54+
# @field_validator('calib')
55+
# @classmethod
56+
# def validate_calib(cls, calib):
57+
# if calib is not None:
58+
# try:
59+
# json.dumps(calib)
60+
# except:
61+
# raise ValueError(f"Invalid calibration data")
62+
# return calib
63+
64+
class CameraBase(BaseModel):
65+
title: Optional[str] = Field(None, min_length=3, max_length=120)
66+
source: Optional[str] = Field(None, max_length=250)
67+
image_width: Optional[int] = Field(None, gt=0)
68+
image_height: Optional[int] = Field(None, gt=0)
69+
calib: Any
70+
latitude: Optional[float] = Field(None, ge=-90, le=90)
71+
longitude: Optional[float] = Field(None, ge=-180, le=180)
72+
5473
@field_validator('calib')
5574
@classmethod
5675
def validate_calib(cls, calib):
@@ -60,6 +79,15 @@ def validate_calib(cls, calib):
6079
except:
6180
raise ValueError(f"Invalid calibration data")
6281
return calib
82+
83+
class CreateCamera(CameraBase):
84+
title: str = Field(min_length=3, max_length=120)
85+
source: str = Field(max_length=250)
86+
image_width: int = Field(gt=0)
87+
image_height: int = Field(gt=0)
88+
calib: Any
89+
latitude: float = Field(ge=-90, le=90)
90+
longitude: float = Field(ge=-180, le=180)
6391

6492
class Point(BaseModel):
6593
latitude: float = Field(ge=-90, le=90)

src/db_manager/db_manager.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,13 @@ def get_camera(self, camera_id):
303303
return camera.serialize() if camera is not None else None
304304

305305
def update_camera(self, camera_id, updated_fields):
306-
if not isinstance(updated_fields, dict):
307-
raise HTTPException(
308-
status_code=400,
309-
detail="Request body must be a JSON dict"
310-
)
311-
312306
with self.get_session() as session:
313307
stmt = update(Camera).where(Camera.id == camera_id)
314308

309+
updated_field = updated_fields.model_dump(
310+
exclude_none=True
311+
)
312+
315313
stmt = stmt.values(updated_fields)
316314

317315
session.execute(stmt)

0 commit comments

Comments
 (0)