From 5471611bac011c91e01a2ec374d34e00234d1628 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Fri, 22 Apr 2016 15:25:48 +0200
Subject: [PATCH] Check for existing Commentary already in the DOI prefill form

---
 commentaries/views.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/commentaries/views.py b/commentaries/views.py
index eff5ca6a5..503227938 100644
--- a/commentaries/views.py
+++ b/commentaries/views.py
@@ -93,11 +93,19 @@ def prefill_using_DOI(request):
             # Check if given doi is of expected form:
             doipattern = re.compile("10.[0-9]{4,9}/[-._;()/:a-zA-Z0-9]+")
             errormessage = ''
+            existing_commentary = None
             if not doipattern.match(doiform.cleaned_data['doi']):
                 errormessage = 'The form of the DOI you entered is improperly formatted.'
+            elif Commentary.objects.filter(pub_DOI=doiform.cleaned_data['doi']).exists():
+                errormessage = 'There already exists a Commentary Page on this publication, see'
+                existing_commentary = get_object_or_404(Commentary, pub_DOI=doiform.cleaned_data['pub_DOI'])
+            if errormessage != '':
                 form = RequestCommentaryForm()
-                context = {'form': form, 'doiform': doiform, 'errormessage': errormessage}
+                context = {'form': form, 'doiform': doiform, 
+                           'errormessage': errormessage,
+                           'existing_commentary': existing_commentary}
                 return render(request, 'commentaries/request_commentary.html', context)
+            # Otherwise we query Crossref for the information:
             try:
                 queryurl = 'http://api.crossref.org/works/%s' % doiform.cleaned_data['doi']
                 doiquery = requests.get(queryurl)
-- 
GitLab