Skip to content

Commit

Permalink
run schema validations on save
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsdas committed Jul 19, 2022
1 parent 5d1cfb6 commit 970ae76
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
11 changes: 10 additions & 1 deletion care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.exceptions import ValidationError as DjangoValidationError
from django.db.models import Q
from django.http import Http404
from django.shortcuts import get_object_or_404
Expand All @@ -7,7 +8,10 @@
from rest_framework import filters as drf_filters
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError, APIException
from rest_framework.exceptions import APIException
from rest_framework.exceptions import ValidationError
from rest_framework.exceptions import ValidationError as DRFValidationError
from rest_framework.fields import get_error_detail
from rest_framework.mixins import (
CreateModelMixin,
ListModelMixin,
Expand Down Expand Up @@ -135,6 +139,11 @@ def get_queryset(self):
)
return queryset

def handle_exception(self, exc):
if isinstance(exc, DjangoValidationError):
exc = DRFValidationError(detail=get_error_detail(exc))
return super().handle_exception(exc)

@swagger_auto_schema(responses={200: UserDefaultAssetLocationSerializer()})
@action(detail=False, methods=["GET"])
def get_default_user_location(self, request, *args, **kwargs):
Expand Down
20 changes: 20 additions & 0 deletions care/facility/migrations/0302_auto_20220719_1755.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.11 on 2022-07-19 12:25

import care.utils.models.validators
import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('facility', '0301_auto_20220709_2051'),
]

operations = [
migrations.AlterField(
model_name='asset',
name='meta',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict, validators=[care.utils.models.validators.JSONFieldSchemaValidator({'$schema': 'http://json-schema.org/draft-07/schema#', 'anyOf': [{'$ref': '#/definitions/onvif'}, {'$ref': '#/definitions/hl7monitor'}, {'$ref': '#/definitions/empty'}], 'definitions': {'empty': {'additionalProperties': False, 'type': 'object'}, 'hl7monitor': {'additionalProperties': False, 'properties': {'insecure_connection': {'type': 'boolean'}, 'local_ip_address': {'type': 'string'}, 'middleware_hostname': {'type': 'string'}}, 'required': ['local_ip_address', 'middleware_hostname'], 'type': 'object'}, 'onvif': {'additionalProperties': False, 'properties': {'camera_access_key': {'type': 'string'}, 'insecure_connection': {'type': 'boolean'}, 'local_ip_address': {'type': 'string'}, 'middleware_hostname': {'type': 'string'}, 'port': {'type': 'number'}}, 'required': ['local_ip_address', 'middleware_hostname', 'camera_access_key', 'port'], 'type': 'object'}}})]),
),
]
4 changes: 4 additions & 0 deletions care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class Meta:
def __str__(self):
return self.name

def save(self, *args, **kwargs) -> None:
self.clean_fields()
return super().save(*args, **kwargs)


class UserDefaultAssetLocation(BaseModel):
user = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False)
Expand Down

0 comments on commit 970ae76

Please sign in to comment.