From 2780dd8284242eb06e7e28964151b9fa7907cbfc Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Tue, 10 Apr 2018 12:04:05 +0200
Subject: [PATCH] Fix ArXiv query

---
 submissions/views.py | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/submissions/views.py b/submissions/views.py
index 30a3e980f..b1c816f5c 100644
--- a/submissions/views.py
+++ b/submissions/views.py
@@ -1392,8 +1392,7 @@ def prepare_for_voting(request, rec_id):
     fellows_with_expertise = recommendation.submission.fellows.filter(
         contributor__expertises__contains=[recommendation.submission.subject_area])
 
-    coauthorships = {}
-
+    coauthorships = []
     eligibility_form = VotingEligibilityForm(request.POST or None, instance=recommendation)
     if eligibility_form.is_valid():
         eligibility_form.save()
@@ -1408,21 +1407,26 @@ def prepare_for_voting(request, rec_id):
     else:
         # Identify possible co-authorships in last 3 years, disqualifying Fellow from voting:
         if recommendation.submission.metadata is not None:
+            author_last_names = []
+            for author in recommendation.submission.metadata['entries'][0]['authors']:
+                # Gather author data to do conflict-of-interest queries with
+                author_last_names.append(author['name'].split()[-1])
+
+            fellow_last_names = []
             for fellow in fellows_with_expertise:
-                sub_auth_boolean_str = '((' + (recommendation.submission
-                                               .metadata['entries'][0]['authors'][0]['name']
-                                               .split()[-1])
-                for author in recommendation.submission.metadata['entries'][0]['authors'][1:]:
-                    sub_auth_boolean_str += '+OR+' + author['name'].split()[-1]
-                    sub_auth_boolean_str += ')+AND+'
-                    search_str = sub_auth_boolean_str + fellow.contributor.user.last_name + ')'
-                    queryurl = ('http://export.arxiv.org/api/query?search_query=au:%s'
-                                % search_str + '&sortBy=submittedDate&sortOrder=descending'
-                                '&max_results=5')
-                    arxivquery = feedparser.parse(queryurl)
-                    queryresults = arxivquery
-                    if queryresults.entries:
-                        coauthorships[fellow.contributor.user.last_name] = queryresults
+                # For each fellow found, so a query with the authors to check for conflicts
+                fellow_last_names.append(fellow.contributor.user.last_name)
+
+            search_query = 'au:(({fellows})+AND+({authors}))'.format(
+                fellows='+OR+'.join(fellow_last_names),
+                authors='+OR+'.join(author_last_names))
+            queryurl = 'https://export.arxiv.org/api/query?search_query={sq}'.format(
+                sq=search_query)
+            queryurl += '&sortBy=submittedDate&sortOrder=descending&max_results=5'
+            queryurl = queryurl.replace(' ', '+')  # Fallback for some last names with spaces
+            queryresults = feedparser.parse(queryurl)
+            if queryresults.entries:
+                coauthorships = queryresults
 
     context = {
         'recommendation': recommendation,
-- 
GitLab