From 5055125abb0f654970c32c82fb86c0e7d321e0a8 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Tue, 30 Oct 2018 13:33:23 +0100
Subject: [PATCH] Improve display of Profiles for given Topic

---
 ontology/templates/ontology/_topic_card.html | 23 +++++++++++++++++++-
 profiles/models.py                           | 20 +++++++++++++++++
 profiles/templatetags/profiles_extras.py     | 14 ++++++++++++
 3 files changed, 56 insertions(+), 1 deletion(-)
 create mode 100644 profiles/templatetags/profiles_extras.py

diff --git a/ontology/templates/ontology/_topic_card.html b/ontology/templates/ontology/_topic_card.html
index ff13e9153..fe8c09ded 100644
--- a/ontology/templates/ontology/_topic_card.html
+++ b/ontology/templates/ontology/_topic_card.html
@@ -1,5 +1,7 @@
 {% load bootstrap %}
 
+{% load profiles_extras %}
+
 <script>
   $(document).ready(function() {
   $("#id_A_text").keyup(function() {
@@ -78,7 +80,7 @@
     </div>
     <hr/>
     {% endif %}
-    <div class="card-deck">
+    <div class="card-columns">
       <div class="card">
 	<div class="card-header">
 	  Publications
@@ -119,6 +121,25 @@
 	  </ul>
 	</div>
       </div>
+      <div class="card">
+	<div class="card-header">
+	  Profiles
+	</div>
+	<div class="card-body">
+	  <ul>
+	    {% get_profiles topic.slug as profiles %}
+	    {% for profile in profiles %}
+	    {% if profile.contributor %}
+	    <li><a href="{{ profile.contributor.get_absolute_url }}">{{ profile }}</a></li>
+	    {% else %}
+	    <li>{{ profile }}</li>
+	    {% endif %}
+	    {% empty %}
+	    <li>No Profile found</li>
+	    {% endfor %}
+	  </ul>
+	</div>
+      </div>
     </div>
   </div>
 </div>
diff --git a/profiles/models.py b/profiles/models.py
index 62f347c8d..051121332 100644
--- a/profiles/models.py
+++ b/profiles/models.py
@@ -3,12 +3,16 @@ __license__ = "AGPL v3"
 
 
 from django.db import models
+from django.shortcuts import get_object_or_404
 
 from scipost.behaviors import orcid_validator
 from scipost.constants import (
     TITLE_CHOICES, SCIPOST_DISCIPLINES, DISCIPLINE_PHYSICS, SCIPOST_SUBJECT_AREAS)
 from scipost.fields import ChoiceArrayField
 
+from ontology.models import Topic
+from journals.models import PublicationAuthorsTable
+
 from .managers import ProfileQuerySet
 
 
@@ -81,3 +85,19 @@ class ProfileEmail(models.Model):
 
     def __str__(self):
         return self.email
+
+
+def get_profiles(slug):
+    """
+    Returns a list of Profiles for which there exists at least one
+    Publication/Submission object carrying this Topic.
+    """
+    topic = get_object_or_404(Topic, slug=slug)
+    publications = PublicationAuthorsTable.objects.filter(publication__topics__in=[topic,])
+    cont_id_list = [tbl.contributor.id for tbl in publications.all() \
+                    if tbl.contributor is not None]
+    unreg_id_list = [tbl.unregistered_author.id for tbl in publications.all() \
+                     if tbl.unregistered_author is not None]
+    print (unreg_id_list)
+    return Profile.objects.filter(models.Q(contributor__id__in=cont_id_list) |
+                                  models.Q(unregisteredauthor__id__in=unreg_id_list))
diff --git a/profiles/templatetags/profiles_extras.py b/profiles/templatetags/profiles_extras.py
new file mode 100644
index 000000000..f8c0ec829
--- /dev/null
+++ b/profiles/templatetags/profiles_extras.py
@@ -0,0 +1,14 @@
+__copyright__ = "Copyright 2016-2018, Stichting SciPost (SciPost Foundation)"
+__license__ = "AGPL v3"
+
+
+from django import template
+
+from ..models import get_profiles as profiles_get_profiles
+
+register = template.Library()
+
+
+@register.simple_tag
+def get_profiles(slug):
+    return profiles_get_profiles(slug)
-- 
GitLab