diff --git a/commentaries/models.py b/commentaries/models.py index d64ea58e36c671ef90467009434f1627d75f81e2..c6c2feb947188658361f96e6345f81a54f3db313 100644 --- a/commentaries/models.py +++ b/commentaries/models.py @@ -173,7 +173,7 @@ class Commentary(TimeStampedModel): template = Template(header) return template.render(context) - def parse_links_into_urls(self): + def parse_links_into_urls(self, commit=False): """ Takes the arXiv nr or DOI and turns it into the urls """ if self.pub_DOI: self.arxiv_or_DOI_string = self.pub_DOI @@ -181,9 +181,9 @@ class Commentary(TimeStampedModel): elif self.arxiv_identifier: self.arxiv_or_DOI_string = 'arXiv:' + self.arxiv_identifier self.arxiv_link = 'http://arxiv.org/abs/' + self.arxiv_identifier - else: # should never come here - pass - self.save() + + if commit: + self.save() def scipost_url(self): """ Returns the url of the SciPost Commentary Page """ diff --git a/commentaries/urls.py b/commentaries/urls.py index f1abd660ccbe2fbc1fbb313f318b57fa2f81171d..ea3b9ad1818c519693ae439551373c70390f72de 100644 --- a/commentaries/urls.py +++ b/commentaries/urls.py @@ -1,4 +1,4 @@ -from django.conf.urls import include, url +from django.conf.urls import url from django.views.generic import TemplateView from . import views @@ -16,7 +16,7 @@ urlpatterns = [ url(r'^(?P<arxiv_or_DOI_string>arXiv:[0-9]{4,}.[0-9]{5,}(v[0-9]+)?)/$', views.commentary_detail, name='commentary'), # old style identifiers: url(r'^(?P<arxiv_or_DOI_string>arXiv:[a-z-]+/[0-9]{7,}(v[0-9]+)?)/$', views.commentary_detail, name='commentary'), - url(r'^request_commentary$', views.request_commentary, name='request_commentary'), + url(r'^request_commentary$', views.RequestCommentary.as_view(), name='request_commentary'), url(r'^prefill_using_DOI$', views.prefill_using_DOI, name='prefill_using_DOI'), url(r'^prefill_using_identifier$', views.prefill_using_identifier, name='prefill_using_identifier'), url(r'^vet_commentary_requests$', views.vet_commentary_requests, name='vet_commentary_requests'), diff --git a/commentaries/views.py b/commentaries/views.py index 8c9288b1c8fb1c56e0f3d38d9d7db71c0f8dc430..742cccd8ab3027d2a360c78833ec083825ed0f77 100644 --- a/commentaries/views.py +++ b/commentaries/views.py @@ -5,11 +5,15 @@ import requests from django.utils import timezone from django.shortcuts import get_object_or_404, render -from django.contrib.auth.decorators import login_required, permission_required +from django.contrib import messages +from django.contrib.auth.decorators import permission_required +from django.contrib.auth.mixins import LoginRequiredMixin from django.core.mail import EmailMessage -from django.core.urlresolvers import reverse +from django.core.urlresolvers import reverse, reverse_lazy from django.shortcuts import redirect from django.template.loader import render_to_string +from django.views.generic.edit import CreateView +from django.utils.decorators import method_decorator from .models import Commentary from .forms import RequestCommentaryForm, DOIToQueryForm, IdentifierToQueryForm @@ -18,42 +22,41 @@ from .forms import VetCommentaryForm, CommentarySearchForm from comments.models import Comment from comments.forms import CommentForm from scipost.models import Contributor +import strings + ################ # Commentaries ################ - -@login_required -@permission_required('scipost.can_request_commentary_pages', raise_exception=True) -def request_commentary(request): - form = RequestCommentaryForm(request.POST or None, user=request.user) - if request.method == 'POST': - if form.is_valid(): - commentary = form.save(commit=False) - commentary.parse_links_into_urls() - commentary.save() - - context = {'ack_header': 'Thank you for your request for a Commentary Page', - 'ack_message': 'Your request will soon be handled by an Editor. ', - 'followup_message': 'Return to your ', - 'followup_link': reverse('scipost:personal_page'), - 'followup_link_label': 'personal page'} - return render(request, 'scipost/acknowledgement.html', context) - - else: - doiform = DOIToQueryForm() - existing_commentary = form.get_existing_commentary() - identifierform = IdentifierToQueryForm() - context = {'form': form, 'doiform': doiform, 'identifierform': identifierform, - 'errormessage': form.errors, - 'existing_commentary': existing_commentary} - return render(request, 'commentaries/request_commentary.html', context) - - doiform = DOIToQueryForm() - identifierform = IdentifierToQueryForm() - context = {'form': form, 'doiform': doiform, 'identifierform': identifierform} - return render(request, 'commentaries/request_commentary.html', context) +@method_decorator(permission_required( + 'scipost.can_request_commentary_pages', raise_exception=True), name='dispatch') +class RequestCommentary(LoginRequiredMixin, CreateView): + form_class = RequestCommentaryForm + template_name = 'commentaries/request_commentary.html' + success_url = reverse_lazy('scipost:personal_page') + + def get_context_data(self, *args, **kwargs): + '''Pass the DOI and identifier forms to the context.''' + context = super(RequestCommentary, self).get_context_data(*args, **kwargs) + doiform = DOIToQueryForm() + identifierform = IdentifierToQueryForm() + + context['existing_commentary'] = context['form'].get_existing_commentary() + context['doiform'] = doiform + context['identifierform'] = identifierform + return context + + def get_form_kwargs(self, *args, **kwargs): + '''User should be included in the arguments to have a valid form.''' + form_kwargs = super(RequestCommentary, self).get_form_kwargs(*args, **kwargs) + form_kwargs['user'] = self.request.user + return form_kwargs + + def form_valid(self, form): + form.instance.parse_links_into_urls() + messages.success(self.request, strings.acknowledge_request_commentary) + return super(RequestCommentary, self).form_valid(form) @permission_required('scipost.can_request_commentary_pages', raise_exception=True) diff --git a/strings/__init__.py b/strings/__init__.py index 4aa362a9cb4689d7212cb18ff1284ed5cd7b6635..bed3b4266a7950a0cd9f065df61db429a7a847bc 100644 --- a/strings/__init__.py +++ b/strings/__init__.py @@ -3,3 +3,7 @@ acknowledge_request_thesis_link = ( " will soon be handled by an editor." ) acknowledge_vet_thesis_link = "Thesis Link request vetted." +acknowledge_request_commentary = ( + "Your request will soon be handled by an Editor. <br>" + "Thank you for your request for a Commentary Page." +)