Skip to content

Commit

Permalink
Merge pull request #49 from observatorycontrolsystem/feature/update_v…
Browse files Browse the repository at this point in the history
…alidation_schema

Update validation schema to allow some keys to pass through and updat…
  • Loading branch information
jnation3406 authored Oct 8, 2024
2 parents 71e9b9d + a60c97e commit b191570
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ jobs:
pip install --upgrade poetry
poetry install
- name: Run tests
run: poetry run coverage run manage.py test
run: |
poetry run coverage run manage.py test
poetry run coverage xml
env:
SECRET_KEY: test_secret_key
DB_USER: postgres
Expand Down
38 changes: 35 additions & 3 deletions configdb/hardware/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import timedelta
import json

from django.contrib import admin
from django.contrib.admin.models import LogEntry, DELETION, ADDITION, CHANGE
Expand All @@ -9,12 +10,39 @@
from reversion.models import Version
from reversion.admin import VersionAdmin
from reversion.errors import RegistrationError
from cerberus import Validator

from configdb.hardware.models import (
Site, Enclosure, GenericMode, ModeType, GenericModeGroup, Telescope, Instrument, Camera, CameraType,
OpticalElementGroup, OpticalElement, InstrumentType, InstrumentCategory, ConfigurationType, ConfigurationTypeProperties
)
from configdb.hardware.validator import OCSValidator


class ProperJSONField(forms.JSONField):
""" To allow validation_schema JSONFields in the admin interface to accept empty dict {}"""
empty_values= [None, "", [], ()]


class PrettyJSONEncoder(json.JSONEncoder):
""" To pretty print the json validation_schema in the admin interface forms """
def __init__(self, *args, indent, sort_keys, **kwargs):
super().__init__(*args, indent=2, sort_keys=True, **kwargs)


class ConfigurationTypePropertiesAdminForm(forms.ModelForm):
validation_schema = ProperJSONField(encoder=PrettyJSONEncoder)

class Meta:
model = ConfigurationTypeProperties
fields = '__all__'


class GenericModeAdminForm(forms.ModelForm):
validation_schema = ProperJSONField(encoder=PrettyJSONEncoder)

class Meta:
model = GenericMode
fields = '__all__'


class GenericModeGroupAdminForm(forms.ModelForm):
Expand All @@ -31,7 +59,7 @@ def __init__(self, *args, **kwargs):
def clean(self):
if 'validation_schema' in self.cleaned_data:
try:
Validator(self.cleaned_data['validation_schema'])
OCSValidator(self.cleaned_data['validation_schema'])
except Exception as e:
raise ValidationError(f"Invalid cerberus validation_schema: {repr(e)}")

Expand All @@ -51,14 +79,16 @@ def __init__(self, *args, **kwargs):


class InstrumentTypeAdminForm(forms.ModelForm):
validation_schema = ProperJSONField(encoder=PrettyJSONEncoder)

class Meta:
model = InstrumentType
fields = '__all__'

def clean(self):
if 'validation_schema' in self.cleaned_data:
try:
Validator(self.cleaned_data['validation_schema'])
OCSValidator(self.cleaned_data['validation_schema'])
except Exception as e:
raise ValidationError(f"Invalid cerberus validation_schema: {repr(e)}")

Expand Down Expand Up @@ -100,6 +130,7 @@ class ConfigurationTypeAdmin(HardwareAdmin):

@admin.register(ConfigurationTypeProperties)
class ConfigurationTypePropertiesAdmin(HardwareAdmin):
form = ConfigurationTypePropertiesAdminForm
list_display = ('configuration_type', 'instrument_type', 'schedulable', 'config_change_overhead', 'force_acquisition_off', 'requires_optical_elements')
list_filter = ('schedulable', 'configuration_type__code', 'instrument_type__code')
search_fields = ['configuration_type__code', 'instrument_type__code']
Expand Down Expand Up @@ -165,6 +196,7 @@ class GenericModeGroupAdmin(HardwareAdmin):

@admin.register(GenericMode)
class GenericModeAdmin(HardwareAdmin):
form = GenericModeAdminForm
list_display = ('name', 'code', 'overhead', 'schedulable')
search_fields = ('name', 'code')

Expand Down
7 changes: 4 additions & 3 deletions configdb/hardware/serializers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from rest_framework import serializers
from cerberus import Validator

from .models import (
Site, Enclosure, Telescope, OpticalElement, GenericMode, Instrument, Camera, OpticalElementGroup,
CameraType, GenericModeGroup, InstrumentType, ConfigurationTypeProperties
)
from configdb.hardware.validator import OCSValidator


class OpticalElementSerializer(serializers.ModelSerializer):

Expand Down Expand Up @@ -42,7 +43,7 @@ class Meta:

def validate_validation_schema(self, value):
try:
Validator(value)
OCSValidator(value)
except Exception as e:
raise serializers.ValidationError(f"Invalid cerberus validation_schema: {repr(e)}")

Expand Down Expand Up @@ -108,7 +109,7 @@ class Meta:

def validate_validation_schema(self, value):
try:
Validator(value)
OCSValidator(value)
except Exception as e:
raise serializers.ValidationError(f"Invalid cerberus validation_schema: {repr(e)}")

Expand Down
13 changes: 13 additions & 0 deletions configdb/hardware/validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from cerberus import Validator


class OCSValidator(Validator):
""" Custom validator that allows label, show(in UI), and description fields in the schema """
def _validate_description(self, constraint, field, value):
pass

def _validate_label(self, constraint, field, value):
pass

def _validate_show(self, constraint, field, value):
pass

0 comments on commit b191570

Please sign in to comment.