-
Notifications
You must be signed in to change notification settings - Fork 0
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 #84 from acdh-oeaw/subject-controlled-vocabulary
Subject controlled vocabulary
- Loading branch information
Showing
6 changed files
with
332 additions
and
6 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
apis_ontology/management/commands/transfer_subjects_to_vocab.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,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") |
112 changes: 112 additions & 0 deletions
112
...ontology/migrations/0019_subject_alter_versionwork_subject_alter_work_subject_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,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" | ||
), | ||
), | ||
] |
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,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" | ||
), | ||
), | ||
] |
112 changes: 112 additions & 0 deletions
112
...tions/0021_persondirectpredecessorinlineageofperson_subject_of_teaching_vocab_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,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", | ||
), | ||
), | ||
] |
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.