SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 6a303b8f authored by Jorran de Wit's avatar Jorran de Wit
Browse files

Rewrite requestcommentary as CBV

RequestCommentary is transformed to a class based view. Further
 moved strings to `/strings` and used django.messages middleware
 to pass messages to the user instead of context data.
parent bc9198a5
No related branches found
No related tags found
No related merge requests found
......@@ -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 """
......
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'),
......
......@@ -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)
......
......@@ -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."
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment