diff --git a/commentaries/forms.py b/commentaries/forms.py index d3d30be9172a0f972611c91e953bc532ed89c12f..976aa10f2818ecff6f1d74dc2b74cdfc3632b367 100644 --- a/commentaries/forms.py +++ b/commentaries/forms.py @@ -6,7 +6,7 @@ from django.urls import reverse from .models import Commentary -from scipost.services import DOICaller +from scipost.services import DOICaller, ArxivCaller from scipost.models import Contributor @@ -48,10 +48,21 @@ class ArxivQueryForm(forms.Form): identifier = forms.RegexField(regex=VALID_ARXIV_IDENTIFIER_REGEX, strip=True) -# identifierpattern_new = re.compile("^[0-9]{4,}.[0-9]{4,5}v[0-9]{1,2}$") -# identifierpattern_old = re.compile("^[-.a-z]+/[0-9]{7,}v[0-9]{1,2}$") + def clean_identifier(self): + identifier = self.cleaned_data['identifier'] + + if Commentary.objects.filter(arxiv_identifier=identifier).exists(): + error_message = 'There already exists a Commentary Page on this arXiv preprint.' + raise forms.ValidationError(error_message) + caller = ArxivCaller(identifier) + if caller.is_valid: + self.arxiv_data = ArxivCaller(identifier).data + else: + error_message = 'Could not find a resource for that arXiv identifier.' + raise forms.ValidationError(error_message) + return identifier # class IdentifierToQueryForm(forms.Form): diff --git a/commentaries/test_forms.py b/commentaries/test_forms.py index 84d600a71d78480c70c2e9035287983e58fff993..2dcbe54c618a0d576b41da5e4a1f6034251d7e46 100644 --- a/commentaries/test_forms.py +++ b/commentaries/test_forms.py @@ -12,6 +12,9 @@ from common.helpers.test import add_groups_and_permissions class TestArxivQueryForm(TestCase): + def setUp(self): + add_groups_and_permissions() + def test_new_arxiv_identifier_is_valid(self): new_identifier_data = {'identifier': '1612.07611v1'} form = ArxivQueryForm(new_identifier_data) @@ -37,6 +40,19 @@ class TestArxivQueryForm(TestCase): form = ArxivQueryForm(data) self.assertFalse(form.is_valid()) + def test_arxiv_identifier_that_already_has_commentary_page_is_invalid(self): + unvetted_commentary = UnvettedCommentaryFactory() + invalid_data = {'identifier': unvetted_commentary.arxiv_identifier} + form = ArxivQueryForm(invalid_data) + self.assertFalse(form.is_valid()) + error_message = form.errors['identifier'][0] + self.assertRegexpMatches(error_message, re.compile('already exist')) + + def test_valid_but_non_existent_identifier_is_invalid(self): + invalid_data = {'identifier': '1613.07611v1'} + form = ArxivQueryForm(invalid_data) + self.assertFalse(form.is_valid()) + class TestDOIToQueryForm(TestCase): def setUp(self): diff --git a/scipost/services.py b/scipost/services.py index b566c28ec8dcd5692e20d849853cf082094b3b1d..495b580d29b8f31d69ed9a57d965662d9208ae2b 100644 --- a/scipost/services.py +++ b/scipost/services.py @@ -77,9 +77,10 @@ class ArxivCaller: def _call_arxiv(self): url = self.query_base_url % self.identifier request = requests.get(url) - if request.ok: + arxiv_data = feedparser.parse(request.content)['entries'][0] + if self._search_result_present(arxiv_data): self.is_valid = True - self._arxiv_data = feedparser.parse(request.content)['entries'][0] + self._arxiv_data = arxiv_data else: self.is_valid = False @@ -97,6 +98,9 @@ class ArxivCaller: 'pub_abstract': abstract, } + def _search_result_present(self, data): + return 'title' in data + # I'm going to revamp this whole thing... class BaseCaller(object): diff --git a/scipost/test_services.py b/scipost/test_services.py index bb48a7066a5c48f92980a61e65742d263e2e1beb..5316c60b058349bd33e5e40a3e52cedd39727e91 100644 --- a/scipost/test_services.py +++ b/scipost/test_services.py @@ -24,7 +24,7 @@ class ArxivCallerTest(TestCase): def valid_but_nonexistent_identifier(self): caller = ArxivCaller('1613.07611v1') - self.assertFalse(caller.is_valid) + self.assertEqual(caller.is_valid, False) class DOICallerTest(TestCase):