From 88e400489779a3d8fbb0d118f926f8e5e7123335 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sun, 27 Sep 2020 16:06:47 +0200
Subject: [PATCH] Finish removal of discipline and subject_area for theses

---
 .../commentaries/commentary_list.html         |  2 +-
 theses/factories.py                           | 21 ++++++++++++++++---
 theses/forms.py                               |  2 +-
 theses/migrations/0015_auto_20200927_1430.py  | 21 +++++++++++++++++++
 theses/models.py                              | 11 ++--------
 .../theses/_thesislink_card_content.html      |  7 ++++++-
 .../theses/_thesislink_information.html       | 10 +++++++--
 theses/templates/theses/thesislink_list.html  |  4 ++--
 theses/tests/test_forms.py                    |  5 +++++
 theses/tests/test_views.py                    |  3 ++-
 theses/urls.py                                |  2 +-
 theses/views.py                               |  9 ++------
 12 files changed, 69 insertions(+), 28 deletions(-)
 create mode 100644 theses/migrations/0015_auto_20200927_1430.py

diff --git a/commentaries/templates/commentaries/commentary_list.html b/commentaries/templates/commentaries/commentary_list.html
index 4efeafa91..6a535aca3 100644
--- a/commentaries/templates/commentaries/commentary_list.html
+++ b/commentaries/templates/commentaries/commentary_list.html
@@ -31,7 +31,7 @@
       <div class="p-3 mb-3 bg-light scipost-bar border min-height-190">
         <h2>View SciPost Commentaries</h2>
         <ul>
-          <li>Physics: last <a href="{% url 'commentaries:browse' nrweeksback=1 %}">week</a>, <a href="{% url 'commentaries:browse' nrweeksback=4 %}">month</a> or <a href="{% url 'commentaries:browse' nrweeksback=52 %}">year</a> </li>
+          <li>Last <a href="{% url 'commentaries:browse' nrweeksback=1 %}">week</a>, <a href="{% url 'commentaries:browse' nrweeksback=4 %}">month</a> or <a href="{% url 'commentaries:browse' nrweeksback=52 %}">year</a> </li>
         </ul>
       </div>
     </div>
diff --git a/theses/factories.py b/theses/factories.py
index 6361bd2b7..727f6bfc0 100644
--- a/theses/factories.py
+++ b/theses/factories.py
@@ -5,7 +5,8 @@ __license__ = "AGPL v3"
 import factory
 
 from common.helpers.factories import FormFactory
-from scipost.constants import SCIPOST_DISCIPLINES, SCIPOST_SUBJECT_AREAS, SCIPOST_APPROACHES
+from ontology.models import AcademicField, Specialty
+from scipost.constants import SCIPOST_APPROACHES
 from scipost.models import Contributor
 
 from .models import ThesisLink
@@ -23,8 +24,7 @@ class BaseThesisLinkFactory(factory.django.DjangoModelFactory):
     vetted = True
 
     type = factory.Iterator(THESIS_TYPES, getter=lambda c: c[0])
-    discipline = factory.Iterator(SCIPOST_DISCIPLINES[2][1], getter=lambda c: c[0])
-    subject_area = factory.Iterator(SCIPOST_SUBJECT_AREAS[0][1], getter=lambda c: c[0])
+    acad_field = factory.SubFactory('ontology.factories.AcademicFieldFactory')
     approaches = factory.Iterator(SCIPOST_APPROACHES, getter=lambda c: [c[0],])
     title = factory.Faker('sentence')
     pub_link = factory.Faker('uri')
@@ -34,6 +34,21 @@ class BaseThesisLinkFactory(factory.django.DjangoModelFactory):
     defense_date = factory.Faker('date_this_decade')
     abstract = factory.Faker('paragraph')
 
+    @classmethod
+    def create(cls, **kwargs):
+        if AcademicField.objects.count() < 5:
+            from ontology.factories import AcademicFieldactory
+            AcademicFieldFactory.create_batch(5)
+        if Specialty.objects.count() < 5:
+            from ontology.factories import SpecialtyFactory
+            SpecialtyFactory.create_batch(5)
+        return super().create(**kwargs)
+
+    @factory.post_generation
+    def add_specialties(self, create, extracted, **kwargs):
+        if create:
+            self.specialties.set(Specialty.objects.order_by('?')[:3])
+
     @factory.post_generation
     def author_as_cont(self, create, extracted, **kwargs):
         if not create:
diff --git a/theses/forms.py b/theses/forms.py
index 6cbf7606d..2a05dbdde 100644
--- a/theses/forms.py
+++ b/theses/forms.py
@@ -16,7 +16,7 @@ from .helpers import past_years
 class BaseRequestThesisLinkForm(forms.ModelForm):
     class Meta:
         model = ThesisLink
-        fields = ['type', 'discipline', 'subject_area', 'approaches',
+        fields = ['type', 'acad_field', 'specialties', 'approaches',
                   'title', 'author', 'supervisor', 'institution',
                   'defense_date', 'pub_link', 'abstract']
         widgets = {
diff --git a/theses/migrations/0015_auto_20200927_1430.py b/theses/migrations/0015_auto_20200927_1430.py
new file mode 100644
index 000000000..5e1316ab4
--- /dev/null
+++ b/theses/migrations/0015_auto_20200927_1430.py
@@ -0,0 +1,21 @@
+# Generated by Django 2.2.16 on 2020-09-27 12:30
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('theses', '0014_auto_20200926_2210'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='thesislink',
+            name='discipline',
+        ),
+        migrations.RemoveField(
+            model_name='thesislink',
+            name='subject_area',
+        ),
+    ]
diff --git a/theses/models.py b/theses/models.py
index 3992b3cb0..66ac7e244 100644
--- a/theses/models.py
+++ b/theses/models.py
@@ -7,7 +7,7 @@ from django.contrib.contenttypes.fields import GenericRelation
 from django.urls import reverse
 from django.utils import timezone
 
-from scipost.constants import SCIPOST_DISCIPLINES, SCIPOST_SUBJECT_AREAS, SCIPOST_APPROACHES
+from scipost.constants import SCIPOST_APPROACHES
 from scipost.fields import ChoiceArrayField
 
 from .constants import THESIS_TYPES
@@ -25,14 +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')
-    subject_area = models.CharField(
-        max_length=10,
-        choices=SCIPOST_SUBJECT_AREAS,
-        default='Phys:QP')
+
     # Ontology-based semantic linking
     acad_field = models.ForeignKey(
         'ontology.AcademicField',
diff --git a/theses/templates/theses/_thesislink_card_content.html b/theses/templates/theses/_thesislink_card_content.html
index ee187eacf..93163e6a4 100644
--- a/theses/templates/theses/_thesislink_card_content.html
+++ b/theses/templates/theses/_thesislink_card_content.html
@@ -1,6 +1,11 @@
 <div class="card-body px-0">
   <div class="li thesis">
-    <h5 class="subject">{{ thesislink.get_discipline_display }} &middot; {{ thesislink.get_subject_area_display }}</h5>
+    <h5 class="subject">{{ thesislink.acad_field }}</h5>
+    <ul class="list-inline">
+      {% for spec in thesislink.specialties.all %}
+	<li class="list-inline-item">{{ spec }}</li>
+      {% endfor %}
+    </ul>
     <h3 class="title">
       <a href="{% url 'theses:thesis' thesislink_id=thesislink.id %}">{{ thesislink.title }}</a>
     </h3>
diff --git a/theses/templates/theses/_thesislink_information.html b/theses/templates/theses/_thesislink_information.html
index bfd2a9d83..7c63e5ab1 100644
--- a/theses/templates/theses/_thesislink_information.html
+++ b/theses/templates/theses/_thesislink_information.html
@@ -19,10 +19,16 @@
     <td>Type: </td><td></td><td>  {{ thesislink.get_type_display }}</td>
   </tr>
   <tr>
-    <td>Discipline: </td><td></td><td>{{ thesislink.get_discipline_display }}</td>
+    <td>Field: </td><td></td><td>{{ thesislink.acad_field }}</td>
   </tr>
   <tr>
-    <td>Subject area: </td><td></td><td> {{ thesislink.get_subject_area_display }} </td>
+    <td>Specialties: </td><td></td><td>
+      <ul class="list-inline mb-0">
+	{% for spec in thesislink.specialties.all %}
+	  <li class="list-inline-item">{{ spec }}</li>
+	{% endfor %}
+      </ul>
+    </td>
   </tr>
   {% if thesislink.approaches %}
     <tr>
diff --git a/theses/templates/theses/thesislink_list.html b/theses/templates/theses/thesislink_list.html
index 04033e3f2..7d7af3cc3 100644
--- a/theses/templates/theses/thesislink_list.html
+++ b/theses/templates/theses/thesislink_list.html
@@ -27,7 +27,7 @@
       <div class="p-3 mb-3 bg-light scipost-bar border min-height-190">
         <h2>View SciPost Theses</h2>
         <ul>
-          <li>Physics: last <a href="{% url 'theses:browse' discipline='physics' nrweeksback=1 %}">week</a>, <a href="{% url 'theses:browse' discipline='physics' nrweeksback=4 %}">month</a> or <a href="{% url 'theses:browse' discipline='physics' nrweeksback=52 %}">year</a> </li>
+          <li>Last <a href="{% url 'theses:browse' nrweeksback=1 %}">week</a>, <a href="{% url 'theses:browse' nrweeksback=4 %}">month</a> or <a href="{% url 'theses:browse' nrweeksback=52 %}">year</a> </li>
         </ul>
       </div>
     </div>
@@ -39,7 +39,7 @@
       {% if recent %}
         <h2>Recently active Thesis Links:</h2>
       {% elif browse %}
-        <h2>Thesis Links in {{ discipline }} in the last {{ nrweeksback }} week{{ nrweeksback|pluralize }}:</h2>
+        <h2>Thesis Links in the last {{ nrweeksback }} week{{ nrweeksback|pluralize }}:</h2>
       {% else %}
         <h2>Search results:</h3>
       {% endif %}
diff --git a/theses/tests/test_forms.py b/theses/tests/test_forms.py
index 96ef6a3e1..664b14564 100644
--- a/theses/tests/test_forms.py
+++ b/theses/tests/test_forms.py
@@ -6,6 +6,7 @@ import factory
 
 from django.test import TestCase, RequestFactory
 
+from ontology.models import AcademicField, Specialty
 from scipost.factories import ContributorFactory
 from ..factories import ThesisLinkFactory, VetThesisLinkFormFactory
 from ..forms import RequestThesisLinkForm, VetThesisLinkForm
@@ -22,6 +23,8 @@ class TestRequestThesisLink(TestCase):
         self.request.user = self.user
         self.valid_form_data = model_form_data(
             ThesisLinkFactory(), RequestThesisLinkForm, form_kwargs={'request': self.request})
+        self.valid_form_data['acad_field'] = AcademicField.objects.order_by('?').first().id
+        self.valid_form_data['specialties'] = [s.id for s in Specialty.objects.order_by('?')[:3]]
 
     def test_valid_data_is_valid(self):
         form_data = self.valid_form_data
@@ -40,5 +43,7 @@ class TestRequestThesisLink(TestCase):
         form = RequestThesisLinkForm(form_data, request=self.request)
 
         # Check if the user is properly saved to the new ThesisLink as `requested_by`
+        print(form.is_valid())
+        print(form.errors)
         thesislink = form.save()
         self.assertEqual(thesislink.requested_by, self.contributor)
diff --git a/theses/tests/test_views.py b/theses/tests/test_views.py
index 234392ae6..5d24dab25 100644
--- a/theses/tests/test_views.py
+++ b/theses/tests/test_views.py
@@ -99,6 +99,7 @@ class TestVetThesisLinkRequests(TestCase):
         post_data["action_option"] = VetThesisLinkForm.ACCEPT
         target = reverse('theses:vet_thesislink', kwargs={'pk': self.thesislink.id})
 
+        print("post_data:\n\t%s" % post_data)
         request = RequestFactory().post(target, post_data)
         request.user = contributor.user
 
@@ -109,6 +110,7 @@ class TestVetThesisLinkRequests(TestCase):
         setattr(request, '_messages', messages)
 
         response = VetThesisLink.as_view()(request, pk=self.thesislink.id)
+        print("response:\n\t%s" % response)
         self.thesislink.refresh_from_db()
         self.assertEqual(self.thesislink.vetted_by, contributor)
         self.assertEqual(len(mail.outbox), 1)
@@ -140,7 +142,6 @@ class TestVetThesisLinkRequests(TestCase):
         self.assertEqual(len(mail.outbox), 1)
         self.assertEqual(mail.outbox[0].subject, 'SciPost Thesis Link')
 
-
     def test_thesislink_is_vetted_by_correct_contributor_and_mail_is_sent_when_modified(self):
         contributor = ContributorFactory()
         contributor.user.groups.add(Group.objects.get(name="Vetting Editors"))
diff --git a/theses/urls.py b/theses/urls.py
index c20a8df2b..957ae942d 100644
--- a/theses/urls.py
+++ b/theses/urls.py
@@ -12,7 +12,7 @@ app_name = 'theses'
 urlpatterns = [
     # Thesis Links
     url(r'^$', views.ThesisListView.as_view(), name='theses'),
-    url(r'^browse/(?P<discipline>[a-z]+)/(?P<nrweeksback>[0-9]{1,3})/$', views.ThesisListView.as_view(), name='browse'),
+    url(r'^browse/(?P<nrweeksback>[0-9]{1,3})/$', views.ThesisListView.as_view(), name='browse'),
     url(r'^(?P<thesislink_id>[0-9]+)/$', views.thesis_detail, name='thesis'),
     url(r'^request_thesislink$', views.RequestThesisLink.as_view(), name='request_thesislink'),
     url(r'^unvetted_thesislinks$', views.UnvettedThesisLinks.as_view(), name='unvetted_thesislinks'),
diff --git a/theses/views.py b/theses/views.py
index 02707cfdb..c57f01cb6 100644
--- a/theses/views.py
+++ b/theses/views.py
@@ -92,16 +92,11 @@ class ThesisListView(PaginationMixin, ListView):
         # Context is not saved to View object by default
         self.pre_context = self.kwargs
 
-        # Browse if discipline is given
-        if 'discipline' in self.kwargs:
-            self.pre_context['browse'] = True
-
         # Queryset for browsing
         if self.kwargs.get('browse', False):
             return (self.model.objects.vetted()
-                    .filter(discipline=self.kwargs['discipline'],
-                            latest_activity__gte=timezone.now() + datetime.timedelta(
-                                        weeks=-int(self.kwargs['nrweeksback'])))
+                    .filter(latest_activity__gte=timezone.now() + datetime.timedelta(
+                        weeks=-int(self.kwargs['nrweeksback'])))
                     .order_by('-latest_activity'))
 
         # Queryset for searchform is processed by managers
-- 
GitLab