diff --git a/commentaries/forms.py b/commentaries/forms.py index 49c5e7bf7bc79044b65cd7a25ece983d33635cac..f64ee17ff35431d5b942aee1a892fe36e5def447 100644 --- a/commentaries/forms.py +++ b/commentaries/forms.py @@ -9,9 +9,11 @@ from .models import Commentary from scipost.services import DOICaller from scipost.models import Contributor + def commentary_exists(input_doi): return Commentary.objects.filter(pub_DOI=input_doi).exists() + 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( @@ -39,49 +41,67 @@ class DOIToQueryForm(forms.Form): return {**self.crossref_data, **additional_form_data} -class IdentifierToQueryForm(forms.Form): - identifier = forms.CharField(widget=forms.TextInput( - {'label': 'arXiv identifier', - 'placeholder': 'new style ####.####(#)v# or old-style e.g. cond-mat/#######'})) - - def clean(self, *args, **kwargs): - cleaned_data = super(IdentifierToQueryForm, self).clean(*args, **kwargs) - - 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}$") - - if not (identifierpattern_new.match(cleaned_data['identifier']) or - identifierpattern_old.match(cleaned_data['identifier'])): - msg = ('The identifier you entered is improperly formatted ' - '(did you forget the version number?)') - self.add_error('identifier', msg) - - try: - commentary = Commentary.objects.get(arxiv_identifier=cleaned_data['identifier']) - except (Commentary.DoesNotExist, KeyError): - # Commentary either does not exists or form is invalid - commentary = None - - if commentary: - msg = 'There already exists a Commentary Page on this preprint, see %s' % ( - commentary.title_label()) - self.add_error('identifier', msg) - return cleaned_data - +class ArxivQueryForm(forms.Form): + identifier = forms.CharField() + +# 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}$") + + + + +# class IdentifierToQueryForm(forms.Form): +# identifier = forms.CharField(widget=forms.TextInput( +# {'label': 'arXiv identifier', +# 'placeholder': 'new style ####.####(#)v# or old-style e.g. cond-mat/#######'})) +# +# def clean(self, *args, **kwargs): +# cleaned_data = super(IdentifierToQueryForm, self).clean(*args, **kwargs) +# +# 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}$") +# +# if not (identifierpattern_new.match(cleaned_data['identifier']) or +# identifierpattern_old.match(cleaned_data['identifier'])): +# msg = ('The identifier you entered is improperly formatted ' +# '(did you forget the version number?)') +# self.add_error('identifier', msg) +# +# try: +# commentary = Commentary.objects.get(arxiv_identifier=cleaned_data['identifier']) +# except (Commentary.DoesNotExist, KeyError): +# # Commentary either does not exists or form is invalid +# commentary = None +# +# if commentary: +# msg = 'There already exists a Commentary Page on this preprint, see %s' % ( +# commentary.title_label()) +# self.add_error('identifier', msg) +# return cleaned_data -class RequestPublishedArticleForm(forms.ModelForm): +class RequestCommentaryForm(forms.ModelForm): class Meta: model = Commentary fields = [ - 'discipline', 'domain', 'subject_area', 'pub_title', 'author_list', 'journal', 'volume', - 'pages', 'pub_date', 'pub_DOI', 'pub_abstract' + 'discipline', 'domain', 'subject_area', 'pub_title', 'author_list', 'pub_date', 'pub_DOI', 'pub_abstract' ] placeholders = { - 'pub_DOI': 'ex.: 10.21468/00.000.000000', - 'pub_date': 'Format: YYYY-MM-DD', + 'pub_date': 'Format: YYYY-MM-DD' } +class RequestArxivPreprintForm(RequestCommentaryForm): + class Meta(RequestCommentaryForm.Meta): + model = Commentary + fields = RequestCommentaryForm.Meta.fields + ['arxiv_identifier'] + + +class RequestPublishedArticleForm(RequestCommentaryForm): + class Meta(RequestCommentaryForm.Meta): + fields = RequestCommentaryForm.Meta.fields + ['journal', 'volume', 'pages', 'pub_DOI'] + placeholders = {**RequestCommentaryForm.Meta.placeholders, + **{'pub_DOI': 'ex.: 10.21468/00.000.000000'}} + def __init__(self, *args, **kwargs): super(RequestPublishedArticleForm, self).__init__(*args, **kwargs) # We want pub_DOI to be a required field. @@ -98,66 +118,66 @@ class RequestPublishedArticleForm(forms.ModelForm): return input_doi -class RequestCommentaryForm(forms.ModelForm): - """Create new valid Commetary by user request""" - existing_commentary = None - - class Meta: - model = Commentary - fields = ['type', 'discipline', 'domain', 'subject_area', - 'pub_title', 'author_list', - 'metadata', - 'journal', 'volume', 'pages', 'pub_date', - 'arxiv_identifier', - 'pub_DOI', 'pub_abstract'] - - def __init__(self, *args, **kwargs): - self.user = kwargs.pop('user', None) - super(RequestCommentaryForm, self).__init__(*args, **kwargs) - self.fields['metadata'].widget = forms.HiddenInput() - self.fields['pub_date'].widget.attrs.update({'placeholder': 'Format: YYYY-MM-DD'}) - self.fields['arxiv_identifier'].widget.attrs.update( - {'placeholder': 'ex.: 1234.56789v1 or cond-mat/1234567v1'}) - self.fields['pub_DOI'].widget.attrs.update({'placeholder': 'ex.: 10.21468/00.000.000000'}) - self.fields['pub_abstract'].widget.attrs.update({'cols': 100}) - - def clean(self, *args, **kwargs): - """Check if form is valid and contains an unique identifier""" - cleaned_data = super(RequestCommentaryForm, self).clean(*args, **kwargs) - - # Either Arxiv-ID or DOI is given - if not cleaned_data['arxiv_identifier'] and not cleaned_data['pub_DOI']: - msg = ('You must provide either a DOI (for a published paper) ' - 'or an arXiv identifier (for a preprint).') - self.add_error('arxiv_identifier', msg) - self.add_error('pub_DOI', msg) - elif (cleaned_data['arxiv_identifier'] and - (Commentary.objects - .filter(arxiv_identifier=cleaned_data['arxiv_identifier']).exists())): - msg = 'There already exists a Commentary Page on this preprint, see' - self.existing_commentary = get_object_or_404( - Commentary, - arxiv_identifier=cleaned_data['arxiv_identifier']) - self.add_error('arxiv_identifier', msg) - elif (cleaned_data['pub_DOI'] and - Commentary.objects.filter(pub_DOI=cleaned_data['pub_DOI']).exists()): - msg = 'There already exists a Commentary Page on this publication, see' - self.existing_commentary = get_object_or_404( - Commentary, pub_DOI=cleaned_data['pub_DOI']) - self.add_error('pub_DOI', msg) - - # Current user is not known - if not self.user or not Contributor.objects.filter(user=self.user).exists(): - self.add_error(None, 'Sorry, current user is not known to SciPost.') - - def save(self, *args, **kwargs): - """Prefill instance before save""" - self.instance.requested_by = Contributor.objects.get(user=self.user) - return super(RequestCommentaryForm, self).save(*args, **kwargs) - - def get_existing_commentary(self): - """Get Commentary if found after validation""" - return self.existing_commentary +# class RequestCommentaryForm(forms.ModelForm): +# """Create new valid Commetary by user request""" +# existing_commentary = None +# +# class Meta: +# model = Commentary +# fields = ['type', 'discipline', 'domain', 'subject_area', +# 'pub_title', 'author_list', +# 'metadata', +# 'journal', 'volume', 'pages', 'pub_date', +# 'arxiv_identifier', +# 'pub_DOI', 'pub_abstract'] +# +# def __init__(self, *args, **kwargs): +# self.user = kwargs.pop('user', None) +# super(RequestCommentaryForm, self).__init__(*args, **kwargs) +# self.fields['metadata'].widget = forms.HiddenInput() +# self.fields['pub_date'].widget.attrs.update({'placeholder': 'Format: YYYY-MM-DD'}) +# self.fields['arxiv_identifier'].widget.attrs.update( +# {'placeholder': 'ex.: 1234.56789v1 or cond-mat/1234567v1'}) +# self.fields['pub_DOI'].widget.attrs.update({'placeholder': 'ex.: 10.21468/00.000.000000'}) +# self.fields['pub_abstract'].widget.attrs.update({'cols': 100}) +# +# def clean(self, *args, **kwargs): +# """Check if form is valid and contains an unique identifier""" +# cleaned_data = super(RequestCommentaryForm, self).clean(*args, **kwargs) +# +# # Either Arxiv-ID or DOI is given +# if not cleaned_data['arxiv_identifier'] and not cleaned_data['pub_DOI']: +# msg = ('You must provide either a DOI (for a published paper) ' +# 'or an arXiv identifier (for a preprint).') +# self.add_error('arxiv_identifier', msg) +# self.add_error('pub_DOI', msg) +# elif (cleaned_data['arxiv_identifier'] and +# (Commentary.objects +# .filter(arxiv_identifier=cleaned_data['arxiv_identifier']).exists())): +# msg = 'There already exists a Commentary Page on this preprint, see' +# self.existing_commentary = get_object_or_404( +# Commentary, +# arxiv_identifier=cleaned_data['arxiv_identifier']) +# self.add_error('arxiv_identifier', msg) +# elif (cleaned_data['pub_DOI'] and +# Commentary.objects.filter(pub_DOI=cleaned_data['pub_DOI']).exists()): +# msg = 'There already exists a Commentary Page on this publication, see' +# self.existing_commentary = get_object_or_404( +# Commentary, pub_DOI=cleaned_data['pub_DOI']) +# self.add_error('pub_DOI', msg) +# +# # Current user is not known +# if not self.user or not Contributor.objects.filter(user=self.user).exists(): +# self.add_error(None, 'Sorry, current user is not known to SciPost.') +# +# def save(self, *args, **kwargs): +# """Prefill instance before save""" +# self.instance.requested_by = Contributor.objects.get(user=self.user) +# return super(RequestCommentaryForm, self).save(*args, **kwargs) +# +# def get_existing_commentary(self): +# """Get Commentary if found after validation""" +# return self.existing_commentary class VetCommentaryForm(forms.Form): diff --git a/commentaries/templates/commentaries/request_arxiv_preprint.html b/commentaries/templates/commentaries/request_arxiv_preprint.html new file mode 100644 index 0000000000000000000000000000000000000000..b0737c4825728f29fa8f0f8c4dc6786163131a15 --- /dev/null +++ b/commentaries/templates/commentaries/request_arxiv_preprint.html @@ -0,0 +1,25 @@ +{% extends 'scipost/base.html' %} +{% load bootstrap %} +{% load scipost_extras %} + +{% block pagetitle %}: request Commentary{% endblock pagetitle %} + +{% block content %} + +<div class="row"> + <form action="{% url 'commentaries:prefill_using_arxiv_identifier' %}" method="post"> + {% csrf_token %} + {{ arxiv_query_form|bootstrap }} + <input class="btn btn-secondary" type="submit" value="Query arXiv"/> + </form> +</div> + +<div class="row"> + <form id="requestForm" action="{% url 'commentaries:request_published_article' %}" method="post"> + {% csrf_token %} + {{ form|bootstrap }} + <input class="btn btn-primary" type="submit" value="Submit"/> + </form> +</div> + +{% endblock content%} diff --git a/commentaries/urls.py b/commentaries/urls.py index 1f5afec9401e2021a582e5104fde727d43011ea2..03be372fb2a4229fc4ca73cb866c92ba2a56ee1e 100644 --- a/commentaries/urls.py +++ b/commentaries/urls.py @@ -24,10 +24,10 @@ urlpatterns = [ url(r'^request_commentary$', views.request_commentary, name='request_commentary'), url(r'^request_commentary/published_article$', views.RequestPublishedArticle.as_view(), name='request_published_article'), - url(r'^request_commentary/arxiv_preprint$', views.request_arxiv_preprint, name='request_arxiv_preprint'), + url(r'^request_commentary/arxiv_preprint$', views.RequestArxivPreprint.as_view(), name='request_arxiv_preprint'), url(r'^prefill_using_DOI$', views.prefill_using_DOI, name='prefill_using_DOI'), - # url(r'^prefill_using_identifier$', views.PrefillUsingIdentifierView.as_view(), - # name='prefill_using_identifier'), + url(r'^prefill_using_arxiv_identifier$', views.prefill_using_arxiv_identifier, + name='prefill_using_arxiv_identifier'), url(r'^vet_commentary_requests$', views.vet_commentary_requests, name='vet_commentary_requests'), url(r'^vet_commentary_request_ack/(?P<commentary_id>[0-9]+)$', diff --git a/commentaries/views.py b/commentaries/views.py index f1ce56e669f6e2505834bd33e6bdda31cc53c563..4255f6f90b75fbebbb6434c138f804ba864f29b9 100644 --- a/commentaries/views.py +++ b/commentaries/views.py @@ -16,8 +16,8 @@ from django.utils.decorators import method_decorator from django.http import Http404 from .models import Commentary -from .forms import RequestCommentaryForm, DOIToQueryForm, IdentifierToQueryForm, VetCommentaryForm, \ - CommentarySearchForm, RequestPublishedArticleForm +from .forms import DOIToQueryForm, ArxivQueryForm, VetCommentaryForm, \ + CommentarySearchForm, RequestPublishedArticleForm, RequestArxivPreprintForm from comments.models import Comment from comments.forms import CommentForm @@ -81,6 +81,19 @@ class RequestPublishedArticle(CreateView): return context +@method_decorator(permission_required( + 'scipost.can_request_commentary_pages', raise_exception=True), name='dispatch') +class RequestArxivPreprint(CreateView): + form_class = RequestArxivPreprintForm + template_name = 'commentaries/request_arxiv_preprint.html' + success_url = reverse_lazy('scipost:personal_page') + + def get_context_data(self, **kwargs): + context = super(RequestArxivPreprint, self).get_context_data(**kwargs) + context['arxiv_query_form'] = ArxivQueryForm() + return context + + @permission_required('scipost.can_request_commentary_pages', raise_exception=True) def prefill_using_DOI(request): if request.method == "POST": @@ -100,40 +113,13 @@ def prefill_using_DOI(request): else: raise Http404 -# @permission_required('scipost.can_request_commentary_pages', raise_exception=True) -# def request_published_article(request): -# if request.method == "POST": -# doi_form = DOIToQueryForm(request.POST) -# identifier_form = IdentifierToQueryForm() -# # The form checks if doi is valid and commentary doesn't already exist. -# if doi_form.is_valid(): -# doi = doi_form.cleaned_data['doi'] -# crossref_data = DOICaller(doi).data -# additional_form_data = {'type': 'published', 'pub_DOI': doi} -# total_form_data = {**crossref_data, **additional_form_data} -# commentary_form = RequestCommentaryForm(initial=total_form_data) -# context = { -# 'request_commentary_form': commentary_form, -# 'doiform': doi_form, -# 'identifierform': identifier_form, -# } -# return render(request, 'commentaries/request_commentary.html', context) -# else: -# context = { -# 'request_commentary_form': RequestCommentaryForm(), -# 'doiform': doi_form, -# 'identifierform': identifier_form -# } -# return render(request, 'commentaries/request_commentary.html', context) -# elif request.method == "GET": -# context = { 'form': RequestCommentaryForm() } -# return render(request, 'commentaries/request_published_article.html', context) - @permission_required('scipost.can_request_commentary_pages', raise_exception=True) -def request_arxiv_preprint(request): - return 1 - +def prefill_using_arxiv_identifier(request): + if request.method == "POST": + return 1 + else: + raise Http404 # def prefill_using_DOI(request):