From e45813b855fc85675cd9e1ed0cff5bbeff5a7c9a Mon Sep 17 00:00:00 2001
From: Boris Ponsioen <b.g.t.ponsioen@uva.nl>
Date: Tue, 8 May 2018 10:34:04 +0200
Subject: [PATCH] Adds truncate_list template tag for long author lists in
 citables

---
 metacore/models.py                                    |  7 +++++--
 metacore/templates/partials/citable_card_content.html |  4 +++-
 metacore/templatetags/__init__.py                     |  0
 metacore/templatetags/metacore_extras.py              | 11 +++++++++++
 4 files changed, 19 insertions(+), 3 deletions(-)
 create mode 100644 metacore/templatetags/__init__.py
 create mode 100644 metacore/templatetags/metacore_extras.py

diff --git a/metacore/models.py b/metacore/models.py
index fd8962ba4..84354e4e6 100644
--- a/metacore/models.py
+++ b/metacore/models.py
@@ -54,8 +54,11 @@ class Citable(DynamicDocument):
     def times_cited(self):
         return []
 
-    def author_list(self):
-        return '; '.join(self.authors)
+    def author_list(self, max_n=None):
+        if max_n and max_n < len(self.authors):
+            return '; '.join(self.authors[:max_n]) + ' et al.'
+        else:
+            return '; '.join(self.authors)
 
     def crossref_ref_count(self):
         return self.metadata['is-referenced-by-count']
diff --git a/metacore/templates/partials/citable_card_content.html b/metacore/templates/partials/citable_card_content.html
index a704cc518..3f69c07b4 100644
--- a/metacore/templates/partials/citable_card_content.html
+++ b/metacore/templates/partials/citable_card_content.html
@@ -1,9 +1,11 @@
+{% load metacore_extras %}
+
 <div class="submission_title">
     <!-- <h5 class="pb-0 subject_area">{{ submission.get_subject_area_display }}</h5> -->
     <h3 class="card-title mb-0 submisssion_title">
         {{ citable.title }}
     </h3>
-    <p class="mb-3 author_list">by {{ citable.author_list }}</p>
+    <p class="mb-3 author_list">by {{ citable.authors|truncate_list:10 }}</p>
 </div>
 
 {% block card_footer %}
diff --git a/metacore/templatetags/__init__.py b/metacore/templatetags/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/metacore/templatetags/metacore_extras.py b/metacore/templatetags/metacore_extras.py
new file mode 100644
index 000000000..3235b2c2d
--- /dev/null
+++ b/metacore/templatetags/metacore_extras.py
@@ -0,0 +1,11 @@
+from django import template
+
+register = template.Library()
+
+@register.filter
+def truncate_list(authors, max_n):
+    """ Returns author list, truncated to max_n authors when the list is longer """
+    if max_n and max_n < len(authors):
+        return '; '.join(authors[:max_n]) + ' et al.'
+    else:
+        return '; '.join(authors)
-- 
GitLab