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 customizable demographic field entry to facility admin interface #12032

Merged
merged 6 commits into from
Apr 1, 2024
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
1 change: 1 addition & 0 deletions kolibri/core/auth/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ class FacilityUserViewSet(ValuesViewset):
"id_number",
"gender",
"birth_year",
"extra_demographics",
)

field_map = {
Expand Down
153 changes: 153 additions & 0 deletions kolibri/core/auth/constants/demographics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
from __future__ import unicode_literals

from django.core.exceptions import ValidationError
from django.utils.deconstruct import deconstructible

from kolibri.core.utils.validators import JSON_Schema_Validator
from kolibri.core.utils.validators import NoRepeatedValueJSONArrayValidator
from kolibri.utils.i18n import KOLIBRI_SUPPORTED_LANGUAGES


MALE = "MALE"
FEMALE = "FEMALE"
NOT_SPECIFIED = "NOT_SPECIFIED"
Expand All @@ -14,3 +22,148 @@
)

DEMO_FIELDS = ("gender", "birth_year", "id_number")


# '"optional":True' is obsolete but needed while we keep using an
# old json_schema_validator version compatible with python 2.7.
# "additionalProperties": False must be avoided for backwards compatibility
translations_schema = {
"type": "array",
"items": {
"type": "object",
"properties": {
# KOLIBRI_SUPPORTED_LANGUAGES is a set of strings, so we use sorted
# to coerce it to a list with a consistent ordering.
# If we don't do this, every time we initialize, Django thinks it has changed
# and will try to create a new migration.
"language": {"type": "string", "enum": sorted(KOLIBRI_SUPPORTED_LANGUAGES)},
"message": {"type": "string"},
},
},
"optional": True,
}


custom_demographic_field_schema = {
"type": "object",
"properties": {
"id": {
"type": "string",
},
"description": {"type": "string"},
"enumValues": {
"type": "array",
"items": {
"type": "object",
"properties": {
"value": {"type": "string"},
"defaultLabel": {"type": "string"},
"translations": translations_schema,
},
},
},
"translations": translations_schema,
},
}


custom_demographics_schema = {
"type": "array",
"items": custom_demographic_field_schema,
"optional": True,
}


@deconstructible
class UniqueIdsValidator(NoRepeatedValueJSONArrayValidator):
def __init__(self, custom_demographics_key):
super(UniqueIdsValidator, self).__init__(
array_key=custom_demographics_key, object_key="id"
)


unique_translations_validator = NoRepeatedValueJSONArrayValidator(
array_key="translations",
object_key="language",
)


@deconstructible
class DescriptionTranslationValidator(object):
def __init__(self, custom_demographics_key):
self.custom_demographics_key = custom_demographics_key

def __call__(self, value):
for item in value.get(self.custom_demographics_key, []):
try:
unique_translations_validator(item)
except ValidationError:
raise ValidationError(
"User facing description translations for '{} ({})' must be unique by language".format(
item["description"], item["id"]
),
code="invalid",
)
return value


unique_value_validator = NoRepeatedValueJSONArrayValidator(
object_key="value",
)


@deconstructible
class EnumValuesValidator(object):
def __init__(self, custom_demographics_key):
self.custom_demographics_key = custom_demographics_key

def __call__(self, value):
for item in value.get(self.custom_demographics_key, []):
enum_values = item.get("enumValues", [])
try:
unique_value_validator(enum_values)
except ValidationError:
raise ValidationError(
"Possible values for '{} ({})' must be unique".format(
item["description"], item["id"]
),
code="invalid",
)
return value


@deconstructible
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked up what @deconstructible does and thought it was weird that the FacilityUserDemographicValidator class didn't have the decorator -- but then I looked at JSON_Schema_Validator and saw that it was decorated.

So, just to be sure then, class decorators are inherited -- or, are they not inherited and the class below doesn't need the @deconstructible decorator at all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't explicitly add it to the FacilityUserDemographicValidator because it isn't used in the validation for any field definitions within a model (and so doesn't need to be deconstructed and used within a migration) - it may incidentally be marked as such because of the inheritance though.

class LabelTranslationValidator(object):
def __init__(self, custom_demographics_key):
self.custom_demographics_key = custom_demographics_key

def __call__(self, value):
for item in value.get(self.custom_demographics_key, []):
for enumValue in item.get("enumValues", []):
try:
unique_translations_validator(enumValue)
except ValidationError:
raise ValidationError(
"User facing label translations for value '{} ({})' in '{} ({})' must be unique by language".format(
enumValue["defaultLabel"],
enumValue["value"],
item["description"],
item["id"],
),
code="invalid",
)
return value


class FacilityUserDemographicValidator(JSON_Schema_Validator):
def __init__(self, custom_schema):
schema = {
"type": "object",
"properties": {},
}
for field in custom_schema:
schema["properties"][field["id"]] = {
"type": "string",
"enum": [enum["value"] for enum in field["enumValues"]],
}
super(FacilityUserDemographicValidator, self).__init__(schema)
182 changes: 182 additions & 0 deletions kolibri/core/auth/migrations/0025_custom_demographic_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2024-03-28 15:34
from __future__ import unicode_literals

from django.db import migrations

import kolibri.core.auth.constants.demographics
import kolibri.core.fields
import kolibri.core.utils.validators


class Migration(migrations.Migration):

dependencies = [
("kolibriauth", "0024_extend_username_length"),
]

operations = [
migrations.AddField(
model_name="facilityuser",
name="extra_demographics",
field=kolibri.core.fields.JSONField(blank=True, null=True),
),
migrations.AlterField(
model_name="facilitydataset",
name="extra_fields",
field=kolibri.core.fields.JSONField(
blank=True,
default={"facility": {}, "on_my_own_setup": False, "pin_code": ""},
null=True,
validators=[
kolibri.core.utils.validators.JSON_Schema_Validator(
{
"properties": {
"demographic_fields": {
"items": {
"properties": {
"description": {"type": "string"},
"enumValues": {
"items": {
"properties": {
"defaultLabel": {
"type": "string"
},
"translations": {
"items": {
"properties": {
"language": {
"enum": [
"ar",
"bg-bg",
"bn-bd",
"de",
"el",
"en",
"es-419",
"es-es",
"fa",
"ff-cm",
"fr-fr",
"gu-in",
"ha",
"hi-in",
"ht",
"id",
"it",
"ka",
"km",
"ko",
"mr",
"my",
"nyn",
"pt-br",
"pt-mz",
"sw-tz",
"te",
"uk",
"ur-pk",
"vi",
"yo",
"zh-hans",
],
"type": "string",
},
"message": {
"type": "string"
},
},
"type": "object",
},
"optional": True,
"type": "array",
},
"value": {"type": "string"},
},
"type": "object",
},
"type": "array",
},
"id": {"type": "string"},
"translations": {
"items": {
"properties": {
"language": {
"enum": [
"ar",
"bg-bg",
"bn-bd",
"de",
"el",
"en",
"es-419",
"es-es",
"fa",
"ff-cm",
"fr-fr",
"gu-in",
"ha",
"hi-in",
"ht",
"id",
"it",
"ka",
"km",
"ko",
"mr",
"my",
"nyn",
"pt-br",
"pt-mz",
"sw-tz",
"te",
"uk",
"ur-pk",
"vi",
"yo",
"zh-hans",
],
"type": "string",
},
"message": {"type": "string"},
},
"type": "object",
},
"optional": True,
"type": "array",
},
},
"type": "object",
},
"optional": True,
"type": "array",
},
"facility": {"optional": True, "type": "object"},
"on_my_own_setup": {
"optional": True,
"type": "boolean",
},
"pin_code": {
"optional": True,
"type": ["string", "null"],
},
},
"type": "object",
}
),
kolibri.core.auth.constants.demographics.UniqueIdsValidator(
"demographic_fields"
),
kolibri.core.auth.constants.demographics.DescriptionTranslationValidator(
"demographic_fields"
),
kolibri.core.auth.constants.demographics.EnumValuesValidator(
"demographic_fields"
),
kolibri.core.auth.constants.demographics.LabelTranslationValidator(
"demographic_fields"
),
],
),
),
]
Loading
Loading