Skip to content

Commit

Permalink
feat(models): add alternative_names_new field using the JSONEditorField
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Dec 10, 2024
1 parent a870a7b commit 52aae7d
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 5 deletions.
7 changes: 6 additions & 1 deletion apis_ontology/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -19,6 +22,8 @@
# helpers
#########

ABSTRACT_ENTITY_FILTERS_EXCLUDE += ["alternative_names"]


def remove_quotes(token):
return token.strip('"')
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
),
]
61 changes: 57 additions & 4 deletions apis_ontology/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 52aae7d

Please sign in to comment.