From b96b90b8d236f5a598c8565912a8c74af7cf0bd0 Mon Sep 17 00:00:00 2001
From: Geert Kapteijns <ghkapteijns@gmail.com>
Date: Sat, 6 May 2017 14:12:41 +0200
Subject: [PATCH] preliminary test for request commentary view

---
 commentaries/forms.py      | 20 +++++++++++--------
 commentaries/test_views.py | 17 +++++++++++++++-
 scipost/services.py        | 41 +++++++++++++++++++++++++++++++++++---
 3 files changed, 66 insertions(+), 12 deletions(-)

diff --git a/commentaries/forms.py b/commentaries/forms.py
index b74c0f00a..b5b332e27 100644
--- a/commentaries/forms.py
+++ b/commentaries/forms.py
@@ -10,16 +10,20 @@ from scipost.models import Contributor
 
 
 class DOIToQueryForm(forms.Form):
-    VALID_DOI_REGEXP = r'^(?i)10.\d{4,9}/[-._;()/:A-Z0-9]+$'
-    doi = forms.RegexField(regex=VALID_DOI_REGEXP, strip=False, widget=forms.TextInput(
+    doi = forms.CharField(widget=forms.TextInput(
         {'label': 'DOI', 'placeholder': 'ex.: 10.21468/00.000.000000'}))
 
-    def clean_doi(self):
-        input_doi = self.cleaned_data['doi']
-        if Commentary.objects.filter(pub_DOI=input_doi).exists():
-            error_message = 'There already exists a Commentary Page on this publication.'
-            raise forms.ValidationError(error_message)
-        return input_doi
+# class DOIToQueryForm(forms.Form):
+#     VALID_DOI_REGEXP = r'^(?i)10.\d{4,9}/[-._;()/:A-Z0-9]+$'
+#     doi = forms.RegexField(regex=VALID_DOI_REGEXP, strip=False, widget=forms.TextInput(
+#         {'label': 'DOI', 'placeholder': 'ex.: 10.21468/00.000.000000'}))
+#
+#     def clean_doi(self):
+#         input_doi = self.cleaned_data['doi']
+#         if Commentary.objects.filter(pub_DOI=input_doi).exists():
+#             error_message = 'There already exists a Commentary Page on this publication.'
+#             raise forms.ValidationError(error_message)
+#         return input_doi
 
 
 class IdentifierToQueryForm(forms.Form):
diff --git a/commentaries/test_views.py b/commentaries/test_views.py
index 17ea43b4c..82ad9463e 100644
--- a/commentaries/test_views.py
+++ b/commentaries/test_views.py
@@ -7,7 +7,7 @@ from scipost.factories import ContributorFactory, UserFactory
 from .factories import UnvettedCommentaryFactory, VettedCommentaryFactory, UnpublishedVettedCommentaryFactory
 from .forms import CommentarySearchForm
 from .models import Commentary
-from .views import RequestCommentary
+from .views import RequestCommentary, prefill_using_DOI
 from common.helpers.test import add_groups_and_permissions
 
 
@@ -35,6 +35,21 @@ class RequestCommentaryTest(TestCase):
         raise NotImplementedError
 
 
+class PrefillUsingDOITest(TestCase):
+    def setUp(self):
+        add_groups_and_permissions()
+        self.target = reverse('commentaries:prefill_using_DOI')
+        self.physrev_doi = '10.1103/PhysRevB.92.214427'
+
+    def test_submit_valid_physrev_doi(self):
+        post_data = {'doi': self.physrev_doi}
+        request = RequestFactory().post(self.target, post_data)
+        request.user = UserFactory()
+
+        response = prefill_using_DOI(request)
+        self.assertRedirects(response, reverse('commentaries:request_commentary'))
+
+
 class VetCommentaryRequestsTest(TestCase):
     """Test cases for `vet_commentary_requests` view method"""
 
diff --git a/scipost/services.py b/scipost/services.py
index 085b86986..56c403df8 100644
--- a/scipost/services.py
+++ b/scipost/services.py
@@ -17,10 +17,45 @@ class DOICaller:
 
     def _call_crosslink(self):
         url = 'http://api.crossref.org/works/%s' % self.doi_string
-        self.crossref_data = requests.get(url).json()
+        self.crossref_data = requests.get(url).json()['message']
+
+    def _format_data(self):
+        data = self.crossref_data
+        pub_title = data['title'][0]
+        authorlist = ['{} {}'.format(author['given'], author['family']) for author in data['author']]
+        journal = data['container-title'][0]
+        volume = data.get('volume', '')
+        pages = self._get_pages(data)
+        pub_data = self._get_pub_date(data)
+
+
+    def _get_pages(self, data):
+        # For Physical Review
+        pages = data.get('article-number', '')
+        # For other journals?
+        pages = data.get('page', '')
+        return pages
+
+    def _get_pub_date(self, data):
+        date_parts = data.get('issued', {}).get('date_parts', {})
+        date_parts = data['issued']['date-parts'][0]
+        year = date_parts[0]
+        month = date_parts[1]
+        day = date_parts[2]
+        pub_date = "{}-{}-{}".format(year, month, day)
+
+        pub_date = ''
+        try:
+            pub_date = (str(data['message']['issued']['date-parts'][0][0]) + '-' +
+                        str(data['message']['issued']['date-parts'][0][1]))
+            try:
+                pub_date += '-' + str(
+                    data['message']['issued']['date-parts'][0][2])
+            except (IndexError, KeyError):
+                pass
+        except (IndexError, KeyError):
+            pass
 
-    def _collect_data(self):
-        # read out json here.
 
 
 
-- 
GitLab