Skip to content

Commit

Permalink
Merge pull request #84 from acdh-oeaw/subject-controlled-vocabulary
Browse files Browse the repository at this point in the history
Subject controlled vocabulary
  • Loading branch information
gythaogg authored Jul 2, 2024
2 parents ba46694 + 66ed898 commit b92121b
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 6 deletions.
45 changes: 45 additions & 0 deletions apis_ontology/management/commands/transfer_subjects_to_vocab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.core.management.base import BaseCommand

from apis_ontology.models import Subject, Work
from apis_core.relations.models import Relation
import re
from tqdm.auto import tqdm

DELIMITERS = [",", ";", "+", "/"]
PATTERN = "|".join(map(re.escape, DELIMITERS))


class Command(BaseCommand):
help = "Populate subject vocabulary from works and apply it to Works"

def handle(self, *args, **options):
def split_subjects(old_subject_field):
return [
si.strip().capitalize() for si in re.split(PATTERN, old_subject_field)
]

for w in tqdm(Work.objects.all()):
if w.subject:
subject_list = split_subjects(w.subject)
for si in subject_list:
new_sub, _ = Subject.objects.get_or_create(name=si)
w.skip_history_when_saving = True
w.subject_vocab.add(new_sub)

rels_updated = 0
for rel in tqdm(Relation.objects.all()):
if hasattr(rel, "subject_of_teaching"):
if rel.subject_of_teaching:
subject_list = split_subjects(rel.subject_of_teaching)
for si in subject_list:
new_sub, _ = Subject.objects.get_or_create(name=si)
rel.skip_history_when_saving = True
rel.subject_vocab.add(new_sub)
rels_updated += 1

self.stdout.write(
self.style.SUCCESS(
f"Subject vocabulary now contains {len(Subject.objects.all())} subjects."
)
)
print(f"{rels_updated} relations were updated with subject as a vocabulary")
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Generated by Django 4.2.13 on 2024-07-02 09:23

import apis_core.generic.abc
from django.db import migrations, models
import django.db.models.deletion
import simple_history.models


class Migration(migrations.Migration):
dependencies = [
("apis_ontology", "0018_alter_place_label_alter_versionplace_label"),
]

operations = [
migrations.CreateModel(
name="Subject",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255, unique=True)),
],
options={
"verbose_name": "Subject",
"verbose_name_plural": "Subjects",
},
bases=(apis_core.generic.abc.GenericModel, models.Model),
),
migrations.AlterField(
model_name="versionwork",
name="subject",
field=models.CharField(
blank=True,
editable=False,
max_length=255,
null=True,
verbose_name="subject",
),
),
migrations.AlterField(
model_name="work",
name="subject",
field=models.CharField(
blank=True,
editable=False,
max_length=255,
null=True,
verbose_name="subject",
),
),
migrations.CreateModel(
name="VersionWork_subject_vocab",
fields=[
(
"id",
models.IntegerField(
auto_created=True, blank=True, db_index=True, verbose_name="ID"
),
),
("m2m_history_id", models.AutoField(primary_key=True, serialize=False)),
(
"history",
models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
to="apis_ontology.versionwork",
),
),
(
"subject",
models.ForeignKey(
blank=True,
db_constraint=False,
db_tablespace="",
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="apis_ontology.subject",
),
),
(
"work",
models.ForeignKey(
blank=True,
db_constraint=False,
db_tablespace="",
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="apis_ontology.work",
),
),
],
options={
"verbose_name": "VersionWork_subject_vocab",
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
migrations.AddField(
model_name="work",
name="subject_vocab",
field=models.ManyToManyField(
to="apis_ontology.subject", verbose_name="Subject"
),
),
]
22 changes: 22 additions & 0 deletions apis_ontology/migrations/0020_alter_work_subject_vocab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.13 on 2024-07-02 09:46

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
(
"apis_ontology",
"0019_subject_alter_versionwork_subject_alter_work_subject_and_more",
),
]

operations = [
migrations.AlterField(
model_name="work",
name="subject_vocab",
field=models.ManyToManyField(
blank=True, to="apis_ontology.subject", verbose_name="Subject"
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Generated by Django 4.2.13 on 2024-07-02 20:03

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("apis_ontology", "0020_alter_work_subject_vocab"),
]

operations = [
migrations.AddField(
model_name="persondirectpredecessorinlineageofperson",
name="subject_of_teaching_vocab",
field=models.ManyToManyField(
blank=True,
to="apis_ontology.subject",
verbose_name="Subject of teaching",
),
),
migrations.AddField(
model_name="persondiscipleofperson",
name="subject_of_teaching_vocab",
field=models.ManyToManyField(
blank=True,
to="apis_ontology.subject",
verbose_name="Subject of teaching",
),
),
migrations.AddField(
model_name="personreferswithnametotheviewsofperson",
name="subject_of_teaching_vocab",
field=models.ManyToManyField(
blank=True,
to="apis_ontology.subject",
verbose_name="Subject of teaching",
),
),
migrations.AddField(
model_name="personreferswithoutnametotheviewsofperson",
name="subject_of_teaching_vocab",
field=models.ManyToManyField(
blank=True,
to="apis_ontology.subject",
verbose_name="Subject of teaching",
),
),
migrations.AddField(
model_name="personrequestorofperson",
name="subject_of_teaching_vocab",
field=models.ManyToManyField(
blank=True,
to="apis_ontology.subject",
verbose_name="Subject of teaching",
),
),
migrations.AlterField(
model_name="persondirectpredecessorinlineageofperson",
name="subject_of_teaching",
field=models.CharField(
blank=True,
editable=False,
max_length=255,
null=True,
verbose_name="subject of teaching",
),
),
migrations.AlterField(
model_name="persondiscipleofperson",
name="subject_of_teaching",
field=models.CharField(
blank=True,
editable=False,
max_length=255,
null=True,
verbose_name="subject of teaching",
),
),
migrations.AlterField(
model_name="personreferswithnametotheviewsofperson",
name="subject_of_teaching",
field=models.CharField(
blank=True,
editable=False,
max_length=255,
null=True,
verbose_name="subject of teaching",
),
),
migrations.AlterField(
model_name="personreferswithoutnametotheviewsofperson",
name="subject_of_teaching",
field=models.CharField(
blank=True,
editable=False,
max_length=255,
null=True,
verbose_name="subject of teaching",
),
),
migrations.AlterField(
model_name="personstudentofperson",
name="subject_of_teaching",
field=models.CharField(
blank=True,
editable=False,
max_length=255,
null=True,
verbose_name="subject of teaching",
),
),
]
37 changes: 33 additions & 4 deletions apis_ontology/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
logger = logging.getLogger(__name__)


class Subject(GenericModel, models.Model):
name = models.CharField(max_length=255, unique=True)

def __str__(self):
return self.name

class Meta:
verbose_name = _("Subject")
verbose_name_plural = _("Subjects")


class TibScholEntityMixin(models.Model):
class Meta:
abstract = True
Expand Down Expand Up @@ -167,12 +178,10 @@ class Work(
("Other", "Other"),
]
subject = models.CharField(
max_length=255,
blank=True,
null=True,
verbose_name="subject",
max_length=255, blank=True, null=True, verbose_name="subject", editable=False
) # should be a controlled vocabulary field

subject_vocab = models.ManyToManyField(Subject, verbose_name="Subject", blank=True)
name = models.CharField(max_length=255, blank=True, default="", verbose_name="Name")
sde_dge_ref = models.CharField(
max_length=25, blank=True, null=True, verbose_name="Derge reference"
Expand Down Expand Up @@ -491,8 +500,12 @@ class PersonDirectPredecessorInLineageOfPerson(TibScholRelationMixin):
blank=True,
null=True,
verbose_name="subject of teaching",
editable=False,
)
)
subject_of_teaching_vocab = models.ManyToManyField(
Subject, verbose_name="Subject of teaching", blank=True
)


class PersonDiscipleOfPerson(TibScholRelationMixin):
Expand All @@ -508,8 +521,12 @@ class PersonDiscipleOfPerson(TibScholRelationMixin):
blank=True,
null=True,
verbose_name="subject of teaching",
editable=False,
)
)
subject_of_teaching_vocab = models.ManyToManyField(
Subject, verbose_name="Subject of teaching", blank=True
)


class PersonEditorOfInstance(TibScholRelationMixin):
Expand Down Expand Up @@ -705,8 +722,12 @@ class PersonRefersWithNameToTheViewsOfPerson(TibScholRelationMixin):
blank=True,
null=True,
verbose_name="subject of teaching",
editable=False,
)
)
subject_of_teaching_vocab = models.ManyToManyField(
Subject, verbose_name="Subject of teaching", blank=True
)


class PersonRefersWithoutNameToTheViewsOfPerson(TibScholRelationMixin):
Expand All @@ -722,8 +743,12 @@ class PersonRefersWithoutNameToTheViewsOfPerson(TibScholRelationMixin):
blank=True,
null=True,
verbose_name="subject of teaching",
editable=False,
)
)
subject_of_teaching_vocab = models.ManyToManyField(
Subject, verbose_name="Subject of teaching", blank=True
)


class PersonRequestorOfPerson(TibScholRelationMixin):
Expand All @@ -741,6 +766,9 @@ class PersonRequestorOfPerson(TibScholRelationMixin):
verbose_name="subject of teaching",
)
)
subject_of_teaching_vocab = models.ManyToManyField(
Subject, verbose_name="Subject of teaching", blank=True
)


class PersonScribeOfInstance(TibScholRelationMixin):
Expand Down Expand Up @@ -792,6 +820,7 @@ class PersonStudentOfPerson(TibScholRelationMixin):
blank=True,
null=True,
verbose_name="subject of teaching",
editable=False,
)
)

Expand Down
Loading

0 comments on commit b92121b

Please sign in to comment.