-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added boundary model and validations
- Loading branch information
1 parent
01c66c3
commit 228dbe6
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.db import models | ||
from django.core.exceptions import ValidationError | ||
from jsonschema import validate | ||
from jsonschema.exceptions import ValidationError as JSONSchemaValidationError | ||
from camera.models.json_schema.boundary import ASSET_BED_BOUNDARY_SCHEMA | ||
from care.facility.models import AssetBed | ||
from care.utils.models.base import BaseModel | ||
|
||
|
||
|
||
class AssetBedBoundary(BaseModel): | ||
asset_bed = models.OneToOneField( | ||
AssetBed, on_delete=models.PROTECT, related_name="assetbed_camera_boundary" | ||
) | ||
x0 = models.IntegerField() | ||
y0 = models.IntegerField() | ||
x1 = models.IntegerField() | ||
y1 = models.IntegerField() | ||
|
||
def save(self, *args, **kwargs): | ||
if self.asset_bed.asset.asset_class != "ONVIF": | ||
raise ValidationError("AssetBedBoundary is only applicable for AssetBeds with ONVIF assets.") | ||
|
||
data = { | ||
"x0": self.x0, | ||
"y0": self.y0, | ||
"x1": self.x1, | ||
"y1": self.y1, | ||
} | ||
|
||
try: | ||
validate(instance=data, schema=ASSET_BED_BOUNDARY_SCHEMA) | ||
except JSONSchemaValidationError as e: | ||
error=f"Invalid data: {str(e)}" | ||
raise ValidationError(error) | ||
|
||
super().save(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
ASSET_BED_BOUNDARY_SCHEMA = { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"x0": {"type": "number"}, | ||
"y0": {"type": "number"}, | ||
"x1": {"type": "number"}, | ||
"y1": {"type": "number"}, | ||
}, | ||
"required": ["x0", "y0", "x1", "y1"], | ||
"additionalProperties": False, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters