-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from unicef/feature/config
Feature/config
- Loading branch information
Showing
27 changed files
with
1,040 additions
and
572 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
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
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 |
---|---|---|
@@ -1,8 +1,61 @@ | ||
import json | ||
from typing import Any | ||
|
||
from django.contrib import messages | ||
from django.contrib.admin import ModelAdmin, register | ||
from django.http import HttpRequest, HttpResponse | ||
from django.shortcuts import redirect, render | ||
from django.urls import path, reverse | ||
|
||
from hope_dedup_engine.apps.api.models import Config | ||
|
||
|
||
@register(Config) | ||
class ConfigAdmin(ModelAdmin): | ||
pass | ||
list_display = ("name", "settings") | ||
|
||
def get_urls(self): | ||
urls = super().get_urls() | ||
custom_urls = [ | ||
path( | ||
"confirm-save/<int:object_id>/", | ||
self.admin_site.admin_view(self.confirm_save), | ||
name="confirm_save_config", | ||
), | ||
] | ||
return custom_urls + urls | ||
|
||
def response_change(self, request: HttpRequest, obj: Any) -> HttpResponse: | ||
dd_set = ", ".join([str(d) for d in obj.deduplicationset_set.all()]) | ||
if dd_set: | ||
request.session["unsaved_data"] = request.POST.dict() | ||
confirm_url = reverse("admin:confirm_save_config", args=[obj.pk]) | ||
self.message_user( | ||
request, | ||
f"Related deduplication sets {dd_set} was found. Please confirm saving.", | ||
level=messages.WARNING, | ||
) | ||
return redirect(confirm_url) | ||
return super().response_change(request, obj) | ||
|
||
def confirm_save(self, request, object_id): # pragma: no cover | ||
obj = self.get_object(request, object_id) | ||
if request.method == "POST": | ||
form_data = request.session.get("unsaved_data", None) | ||
if form_data: | ||
for field, value in form_data.items(): | ||
if field == "settings": | ||
value = json.loads(value) | ||
setattr(obj, field, value) | ||
obj.save() | ||
|
||
return redirect(reverse("admin:api_config_changelist")) | ||
|
||
return render( | ||
request, | ||
"admin/api/config/confirm_save.html", | ||
{ | ||
"object": obj, | ||
"form_data": request.session.get("unsaved_data"), | ||
}, | ||
) |
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
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
37 changes: 37 additions & 0 deletions
37
...ne/apps/api/migrations/0009_remove_config_face_distance_threshold_config_name_and_more.py
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 @@ | ||
# Generated by Django 5.0.7 on 2024-10-31 06:31 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("api", "0008_ignoredfilenamepair"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="config", | ||
name="face_distance_threshold", | ||
), | ||
migrations.AddField( | ||
model_name="config", | ||
name="name", | ||
field=models.CharField( | ||
blank=True, db_index=True, max_length=128, null=True, unique=True | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="config", | ||
name="settings", | ||
field=models.JSONField(blank=True, default=dict, null=True), | ||
), | ||
migrations.AlterField( | ||
model_name="deduplicationset", | ||
name="config", | ||
field=models.ForeignKey( | ||
null=True, on_delete=django.db.models.deletion.SET_NULL, to="api.config" | ||
), | ||
), | ||
] |
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
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,25 @@ | ||
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, | ||
) | ||
|
||
|
||
class Config(models.Model): | ||
name = models.CharField( | ||
max_length=128, unique=True, null=True, blank=True, db_index=True | ||
) | ||
settings = models.JSONField(default=dict, null=True, blank=True) | ||
|
||
def __str__(self) -> str: | ||
return f"{self.name}" if self.name else f"ID: {self.pk}" | ||
|
||
def clean(self) -> None: | ||
try: | ||
DefaultValidatingValidator(settings_schema).validate(self.settings) | ||
except JSONSchemaValidationError as e: | ||
raise ValidationError({"settings": e.message}) |
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
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
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,97 @@ | ||
from django.conf import settings | ||
|
||
from constance import config | ||
from jsonschema import Draft202012Validator, validators | ||
|
||
settings_schema: dict = { | ||
"type": "object", | ||
"properties": { | ||
"detection": { | ||
"type": "object", | ||
"properties": { | ||
"confidence": { | ||
"type": "number", | ||
"exclusiveMinimum": 0, | ||
"maximum": 1.0, | ||
"default": "constance.config.FACE_DETECTION_CONFIDENCE", | ||
}, | ||
}, | ||
"default": {}, | ||
}, | ||
"recognition": { | ||
"type": "object", | ||
"properties": { | ||
"num_jitters": { | ||
"type": "integer", | ||
"minimum": 1, | ||
"default": "constance.config.FACE_ENCODINGS_NUM_JITTERS", | ||
}, | ||
"model": { | ||
"type": "string", | ||
"enum": tuple( | ||
ch[0] | ||
for ch in settings.CONSTANCE_ADDITIONAL_FIELDS.get( | ||
"face_encodings_model" | ||
)[1].get("choices") | ||
), | ||
"default": "constance.config.FACE_ENCODINGS_MODEL", | ||
}, | ||
"preprocessors": { | ||
type: "array", | ||
"items": { | ||
"type": "string", | ||
"enum": ["contrast"], | ||
}, | ||
"uniqueItems": True, | ||
"default": [], | ||
}, | ||
}, | ||
"default": {}, | ||
}, | ||
"duplicates": { | ||
"type": "object", | ||
"properties": { | ||
"tolerance": { | ||
"type": "number", | ||
"exclusiveMinimum": 0, | ||
"maximum": 1.0, | ||
"default": "constance.config.FACE_DISTANCE_THRESHOLD", | ||
}, | ||
}, | ||
"default": {}, | ||
}, | ||
}, | ||
} | ||
|
||
|
||
def extend_with_default(validator_class): | ||
validate_properties = validator_class.VALIDATORS["properties"] | ||
|
||
def set_defaults(validator, properties, instance, schema): | ||
for property, subschema in properties.items(): | ||
|
||
if "default" in subschema: | ||
default_value = subschema["default"] | ||
if isinstance(default_value, str) and default_value.startswith( | ||
"constance.config." | ||
): | ||
config_name = default_value.split(".")[-1] | ||
default_value = getattr(config, config_name, None) | ||
|
||
instance.setdefault(property, default_value) | ||
|
||
for error in validate_properties( | ||
validator, | ||
properties, | ||
instance, | ||
schema, | ||
): | ||
yield error | ||
|
||
return validators.extend( | ||
validator_class, | ||
{"properties": set_defaults}, | ||
) | ||
|
||
|
||
DefaultValidatingValidator = extend_with_default(Draft202012Validator) |
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
Oops, something went wrong.