diff --git a/apis_ontology/filtersets.py b/apis_ontology/filtersets.py index d00868e..c039b2c 100644 --- a/apis_ontology/filtersets.py +++ b/apis_ontology/filtersets.py @@ -5,7 +5,10 @@ from django.db import models import unicodedata -from apis_core.apis_entities.filtersets import AbstractEntityFilterSet +from apis_core.apis_entities.filtersets import ( + AbstractEntityFilterSet, + ABSTRACT_ENTITY_FILTERS_EXCLUDE, +) from apis_core.collections.models import SkosCollection, SkosCollectionContentObject from django.contrib.contenttypes.models import ContentType @@ -19,6 +22,8 @@ # helpers ######### +ABSTRACT_ENTITY_FILTERS_EXCLUDE += ["alternative_names"] + def remove_quotes(token): return token.strip('"') diff --git a/apis_ontology/migrations/0044_person_alternative_names_new_and_more.py b/apis_ontology/migrations/0044_person_alternative_names_new_and_more.py new file mode 100644 index 0000000..d0a1356 --- /dev/null +++ b/apis_ontology/migrations/0044_person_alternative_names_new_and_more.py @@ -0,0 +1,71 @@ +# Generated by Django 5.1.4 on 2024-12-06 08:35 + +import django_json_editor_field.fields +from django.db import migrations +import json + + +def convert_alternative_names(apps, schema_editor): + """Convert alternative_names string field to JSON field alternative_names_new""" + Person = apps.get_model("apis_ontology", "Person") + + for person in Person.objects.all(): + if person.alternative_names: + # Split on newlines and filter out empty strings + names = [ + {"art": "", "end": "", "name": name.strip(), "start": ""} + for name in person.alternative_names.split("\n") + if name.strip() + ] + # Convert to JSON format + person.alternative_names_new = names + person.save() + + +def reverse_convert(apps, schema_editor): + """Reverse operation: convert JSON back to newline-separated string""" + Person = apps.get_model("apis_ontology", "Person") + + for person in Person.objects.all(): + if person.alternative_names_new: + names = json.loads(person.alternative_names_new) + person.alternative_names = "\n".join(names) + person.save() + + +class Migration(migrations.Migration): + dependencies = [ + ("apis_ontology", "0043_fandstattin_versionfandstattin"), + ] + + operations = [ + migrations.AddField( + model_name="person", + name="alternative_names_new", + field=django_json_editor_field.fields.JSONEditorField(null=True), + ), + migrations.AddField( + model_name="versionperson", + name="alternative_names_new", + field=django_json_editor_field.fields.JSONEditorField(null=True), + ), + migrations.RunPython(convert_alternative_names, reverse_code=reverse_convert), + migrations.RemoveField( + model_name="person", + name="alternative_names", + ), + migrations.RemoveField( + model_name="versionperson", + name="alternative_names", + ), + migrations.RenameField( + model_name="person", + old_name="alternative_names_new", + new_name="alternative_names", + ), + migrations.RenameField( + model_name="versionperson", + old_name="alternative_names_new", + new_name="alternative_names", + ), + ] diff --git a/apis_ontology/models.py b/apis_ontology/models.py index f132932..cc84f2d 100644 --- a/apis_ontology/models.py +++ b/apis_ontology/models.py @@ -11,7 +11,7 @@ from apis_core.generic.abc import GenericModel from apis_core.apis_entities.abc import E53_Place from apis_core.history.models import VersionMixin -from apis_core.utils.fields import NewlineSeparatedListField +from django_json_editor_field.fields import JSONEditorField from auditlog.registry import auditlog @@ -171,9 +171,62 @@ class Person( ) references = models.TextField(blank=True, null=True) notes = models.TextField(blank=True, null=True, verbose_name=_("Notes")) - alternative_names = NewlineSeparatedListField( - blank=True, verbose_name=_("Alternative Names") - ) + schema = { + "title": "Alternative Names", + "type": "array", + "format": "table", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string", + "pattern": "^.+$", + "options": { + "inputAttributes": { + "required": True, + }, + }, + }, + "art": {"type": "string", "enum": ["", "Ehename", "Pseudonym"]}, + "start": { + "type": "string", + "pattern": "^$|^\d\d\d\d$", + "options": { + "inputAttributes": { + "placeholder": "YYYY", + }, + "containerAttributes": { + "class": "yearinput", + }, + }, + }, + "end": { + "type": "string", + "pattern": "^$|^\d\d\d\d$", + "options": { + "inputAttributes": { + "placeholder": "YYYY", + }, + "containerAttributes": { + "class": "yearinput", + }, + }, + }, + }, + }, + } + options = { + "theme": "bootstrap4", + "disable_collapse": True, + "disable_edit_json": True, + "disable_properties": True, + "disable_array_reorder": True, + "disable_array_delete_last_row": True, + "disable_array_delete_all_rows": True, + "prompt_before_delete": False, + } + + alternative_names = JSONEditorField(schema=schema, options=options) # texts # "ÖBL Haupttext"