Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add schema for validating meta field of assets #935

Merged
merged 5 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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, ValidationError
from rest_framework.mixins import (
CreateModelMixin,
ListModelMixin,
Expand Down
5 changes: 3 additions & 2 deletions care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from django.db.models import Q

from care.facility.models.facility import Facility
from care.facility.models.json_schema.asset import ASSET_META
from care.facility.models.mixins.permissions.asset import AssetsPermissionMixin
from care.users.models import User, phone_number_regex
from care.utils.assetintegration.asset_classes import AssetClasses
from care.utils.models.base import BaseModel
from care.utils.models.validators import JSONFieldSchemaValidator


def get_random_asset_id():
Expand Down Expand Up @@ -67,7 +69,7 @@ class Status(enum.Enum):
not_working_reason = models.CharField(max_length=1024, blank=True, null=True)
serial_number = models.CharField(max_length=1024, blank=True, null=True)
warranty_details = models.TextField(null=True, blank=True, default="")
meta = JSONField(default=dict, blank=True)
meta = JSONField(default=dict, blank=True, validators=[JSONFieldSchemaValidator(ASSET_META)])
# Vendor Details
vendor_name = models.CharField(max_length=1024, blank=True, null=True)
support_name = models.CharField(max_length=1024, blank=True, null=True)
Expand All @@ -89,7 +91,6 @@ class Meta:
def __str__(self):
return self.name


class UserDefaultAssetLocation(BaseModel):
user = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False)
location = models.ForeignKey(
Expand Down
41 changes: 41 additions & 0 deletions care/facility/models/json_schema/asset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
HL7_META = {
"type": "object",
"required": ["local_ip_address", "middleware_hostname"],
"properties": {
"local_ip_address": {"type": "string"},
"middleware_hostname": {"type": "string"},
"insecure_connection": {"type": "boolean"},
},
"additionalProperties": False,
}

ONVIF_META = {
"type": "object",
"required": [
"local_ip_address",
"middleware_hostname",
"camera_access_key",
"port",
],
"properties": {
"local_ip_address": {"type": "string"},
"middleware_hostname": {"type": "string"},
"camera_access_key": {"type": "string"},
"port": {"type": "number"},
"insecure_connection": {"type": "boolean"},
},
"additionalProperties": False,
}

EMPTY_META = {"type": "object", "additionalProperties": False}

ASSET_META = {
"$schema": f"http://json-schema.org/draft-07/schema#",
"anyOf": [
{"$ref": "#/definitions/onvif"},
{"$ref": "#/definitions/hl7monitor"},
{"$ref": "#/definitions/empty"},
],
"definitions": {"onvif": ONVIF_META, "hl7monitor": HL7_META, "empty": EMPTY_META},
}