Skip to content

Commit

Permalink
chg ! use svelte jsoneditor
Browse files Browse the repository at this point in the history
  • Loading branch information
vitali-yanushchyk-valor committed Nov 7, 2024
1 parent 183d759 commit 152d894
Show file tree
Hide file tree
Showing 18 changed files with 342 additions and 119 deletions.
17 changes: 16 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies = [
"setuptools>=74.1.2",
"django-smart-env>=0.1.0",
"jsonschema>=4.23.0",
"django-svelte-jsoneditor>=0.4.2",
]

[build-system]
Expand Down
6 changes: 0 additions & 6 deletions src/hope_dedup_engine/apps/api/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from django.contrib import admin

from .config import ConfigAdmin # noqa
from .deduplicationset import DeduplicationSetAdmin # noqa
from .duplicate import DuplicateAdmin # noqa
from .hdetoken import HDETokenAdmin # noqa
from .image import ImageAdmin # noqa

admin.site.site_header = "HOPE Dedup Engine"
admin.site.site_title = "HOPE Deduplication Admin"
admin.site.index_title = "Welcome to the HOPE Deduplication Engine Admin"
73 changes: 71 additions & 2 deletions src/hope_dedup_engine/apps/api/admin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,46 @@
from typing import Any

from django.contrib import messages
from django.contrib.admin import ModelAdmin, register
from django.contrib.admin import ModelAdmin, register, site
from django.core.exceptions import ValidationError
from django.db import models
from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect, render
from django.urls import path, reverse

from admin_extra_buttons.api import button
from admin_extra_buttons.mixins import ExtraButtonsMixin
from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget

from hope_dedup_engine.apps.api.forms import EditSchemaForm
from hope_dedup_engine.apps.api.models import Config
from hope_dedup_engine.apps.api.utils.config_settings_schema import (
get_schema,
set_schema,
)
from hope_dedup_engine.apps.api.validators import DefaultValidatingValidator
from hope_dedup_engine.utils.security import is_root


@register(Config)
class ConfigAdmin(ModelAdmin):
class ConfigAdmin(ExtraButtonsMixin, ModelAdmin):
list_display = ("name", "settings")
change_list_template = "admin/api/config/change_list.html"

formfield_overrides = {
models.JSONField: {
"widget": SvelteJSONEditorWidget,
}
}

def get_changeform_initial_data(self, request: HttpRequest) -> dict[str, str]:
initial_data = super().get_changeform_initial_data(request)
initial_data["settings"] = {}
try:
DefaultValidatingValidator(get_schema()).validate(initial_data["settings"])
except ValidationError as e:
self.message_user(request, e.message, level=messages.ERROR)
return initial_data

def get_urls(self):
urls = super().get_urls()
Expand All @@ -22,6 +51,11 @@ def get_urls(self):
self.admin_site.admin_view(self.confirm_save),
name="confirm_save_config",
),
path(
"change-settings-schema/",
self.admin_site.admin_view(self.change_settings_schema),
name="change_settings_schema",
),
]
return custom_urls + urls

Expand Down Expand Up @@ -59,3 +93,38 @@ def confirm_save(self, request, object_id): # pragma: no cover
"form_data": request.session.get("unsaved_data"),
},
)

@button(permission=is_root)
def change_settings_schema(self, request: HttpRequest) -> HttpResponse:
context = {
"opts": self.model._meta,
"site_header": site.site_header,
"title": "Change settings shema",
"trail_label": "Settings schema",
"has_view_permission": self.has_view_permission(request),
}
if request.method == "POST":
form = EditSchemaForm(request.POST)
if form.is_valid():
try:
set_schema(form.cleaned_data["schema"])
except ValidationError as e:
self.message_user(request, e.message, level=messages.ERROR)
else:
self.message_user(request, "Schema has been updated.")
return redirect(reverse("admin:api_config_changelist"))
else:
try:
form = EditSchemaForm(initial={"schema": get_schema()})
except ValidationError as e:
self.message_user(request, e.message, level=messages.ERROR)
return redirect(reverse("admin:api_config_changelist"))

return render(
request,
"admin/api/config/change_settings_schema.html",
{
"form": form,
**context,
},
)
63 changes: 63 additions & 0 deletions src/hope_dedup_engine/apps/api/config_settings_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"type": "object",
"properties": {
"detection": {
"type": "object",
"properties": {
"confidence": {
"type": "number",
"exclusiveMinimum": 0,
"maximum": 1,
"default": "constance.config.FACE_DETECTION_CONFIDENCE"
}
},
"default": {}
},
"recognition": {
"type": "object",
"properties": {
"properties": {
"type": "array",
"items": {
"type": "string"
}
},
"num_jitters": {
"type": "integer",
"minimum": 1,
"default": "constance.config.FACE_ENCODINGS_NUM_JITTERS"
},
"model": {
"type": "string",
"enum": [
"small",
"large"
],
"default": "constance.config.FACE_ENCODINGS_MODEL"
},
"preprocessors": {
"type": "array",
"items": {
"type": "string",
"enum": []
},
"uniquItems": true,
"default": []
}
},
"default": {}
},
"duplicates": {
"type": "object",
"properties": {
"tolerance": {
"type": "number",
"exclusiveMinimum": 0,
"maximum": 1,
"default": "constance.config.FACE_DISTANCE_THRESHOLD"
}
},
"default": {}
}
}
}
7 changes: 7 additions & 0 deletions src/hope_dedup_engine/apps/api/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django import forms

from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget


class EditSchemaForm(forms.Form):
schema = forms.JSONField(widget=SvelteJSONEditorWidget())
12 changes: 4 additions & 8 deletions src/hope_dedup_engine/apps/api/models/config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from django.core.exceptions import ValidationError
from django.db import models

from jsonschema import ValidationError as JSONSchemaValidationError

from hope_dedup_engine.apps.api.utils.config_schema import (
DefaultValidatingValidator,
settings_schema,
)
from hope_dedup_engine.apps.api.utils.config_settings_schema import get_schema
from hope_dedup_engine.apps.api.validators import DefaultValidatingValidator


class Config(models.Model):
Expand All @@ -20,6 +16,6 @@ def __str__(self) -> str:

def clean(self) -> None:
try:
DefaultValidatingValidator(settings_schema).validate(self.settings)
except JSONSchemaValidationError as e:
DefaultValidatingValidator(get_schema()).validate(self.settings)
except Exception as e:
raise ValidationError({"settings": e.message})
4 changes: 2 additions & 2 deletions src/hope_dedup_engine/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
IgnoredReferencePkPair,
Image,
)
from hope_dedup_engine.apps.api.utils.config_schema import settings_schema
from src.hope_dedup_engine.apps.api.utils.config_settings_schema import get_schema


class ConfigSerializer(serializers.ModelSerializer):
Expand All @@ -20,7 +20,7 @@ class Meta:
exclude = ("id",)

def validate_settings(self, value):
validator = Draft202012Validator(settings_schema)
validator = Draft202012Validator(get_schema)
try:
validator.validate(value)
except JSONSchemaValidationError as e:
Expand Down
97 changes: 0 additions & 97 deletions src/hope_dedup_engine/apps/api/utils/config_schema.py

This file was deleted.

Loading

0 comments on commit 152d894

Please sign in to comment.