From c372086996f0712946e95978a32c10e122aea4bd Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sat, 26 Sep 2020 22:11:36 +0200
Subject: [PATCH] Add acad_field and specialties to ThesisLink; populate

---
 theses/migrations/0012_auto_20200926_2206.py  | 30 +++++++++++++++++++
 ...ulate_thesislink_acad_field_specialties.py | 28 +++++++++++++++++
 theses/migrations/0014_auto_20200926_2210.py  | 24 +++++++++++++++
 theses/models.py                              | 15 ++++++++++
 4 files changed, 97 insertions(+)
 create mode 100644 theses/migrations/0012_auto_20200926_2206.py
 create mode 100644 theses/migrations/0013_populate_thesislink_acad_field_specialties.py
 create mode 100644 theses/migrations/0014_auto_20200926_2210.py

diff --git a/theses/migrations/0012_auto_20200926_2206.py b/theses/migrations/0012_auto_20200926_2206.py
new file mode 100644
index 000000000..7373c6fa5
--- /dev/null
+++ b/theses/migrations/0012_auto_20200926_2206.py
@@ -0,0 +1,30 @@
+# Generated by Django 2.2.16 on 2020-09-26 20:06
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('ontology', '0007_Branch_Field_Specialty'),
+        ('theses', '0011_auto_20191017_0949'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='thesislink',
+            name='acad_field',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='theses', to='ontology.AcademicField'),
+        ),
+        migrations.AddField(
+            model_name='thesislink',
+            name='specialties',
+            field=models.ManyToManyField(blank=True, related_name='theses', to='ontology.Specialty'),
+        ),
+        migrations.AddField(
+            model_name='thesislink',
+            name='topics',
+            field=models.ManyToManyField(blank=True, to='ontology.Topic'),
+        ),
+    ]
diff --git a/theses/migrations/0013_populate_thesislink_acad_field_specialties.py b/theses/migrations/0013_populate_thesislink_acad_field_specialties.py
new file mode 100644
index 000000000..7f5ca953a
--- /dev/null
+++ b/theses/migrations/0013_populate_thesislink_acad_field_specialties.py
@@ -0,0 +1,28 @@
+# Generated by Django 2.2.16 on 2020-09-26 20:06
+
+from django.db import migrations
+from django.utils.text import slugify
+
+
+def populate_acad_field_specialty(apps, schema_editor):
+    ThesisLink = apps.get_model('theses.ThesisLink')
+    AcademicField = apps.get_model('ontology', 'AcademicField')
+    Specialty = apps.get_model('ontology', 'Specialty')
+
+    for t in ThesisLink.objects.all():
+        t.acad_field = AcademicField.objects.get(slug=t.discipline)
+        t.specialties.add(
+            Specialty.objects.get(slug=slugify(t.subject_area.replace(':', '-'))))
+        t.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('theses', '0012_auto_20200926_2206'),
+    ]
+
+    operations = [
+        migrations.RunPython(populate_acad_field_specialty,
+                             reverse_code=migrations.RunPython.noop),
+    ]
diff --git a/theses/migrations/0014_auto_20200926_2210.py b/theses/migrations/0014_auto_20200926_2210.py
new file mode 100644
index 000000000..7f45b021f
--- /dev/null
+++ b/theses/migrations/0014_auto_20200926_2210.py
@@ -0,0 +1,24 @@
+# Generated by Django 2.2.16 on 2020-09-26 20:10
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('theses', '0013_populate_thesislink_acad_field_specialties'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='thesislink',
+            name='acad_field',
+            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='theses', to='ontology.AcademicField'),
+        ),
+        migrations.AlterField(
+            model_name='thesislink',
+            name='specialties',
+            field=models.ManyToManyField(related_name='theses', to='ontology.Specialty'),
+        ),
+    ]
diff --git a/theses/models.py b/theses/models.py
index 862c00418..3992b3cb0 100644
--- a/theses/models.py
+++ b/theses/models.py
@@ -25,6 +25,7 @@ class ThesisLink(models.Model):
         'scipost.Contributor', blank=True, null=True,
         on_delete=models.CASCADE)
     type = models.CharField(choices=THESIS_TYPES, max_length=3)
+    # TODO: Next 2 fields to be deprecated
     discipline = models.CharField(
         max_length=20, choices=SCIPOST_DISCIPLINES,
         default='physics')
@@ -32,6 +33,20 @@ class ThesisLink(models.Model):
         max_length=10,
         choices=SCIPOST_SUBJECT_AREAS,
         default='Phys:QP')
+    # Ontology-based semantic linking
+    acad_field = models.ForeignKey(
+        'ontology.AcademicField',
+        on_delete=models.PROTECT,
+        related_name='theses'
+    )
+    specialties = models.ManyToManyField(
+        'ontology.Specialty',
+        related_name='theses'
+    )
+    topics = models.ManyToManyField(
+        'ontology.Topic',
+        blank=True
+    )
     approaches = ChoiceArrayField(
         models.CharField(max_length=24, choices=SCIPOST_APPROACHES),
         blank=True, null=True, verbose_name='approach(es) [optional]')
-- 
GitLab