diff --git a/commentaries/forms.py b/commentaries/forms.py index acf9558fa30b5963f18a6cd0f8cfe9b5b1a1e391..87ee558e0e0df1244524a0eb2ec296a6710549e4 100644 --- a/commentaries/forms.py +++ b/commentaries/forms.py @@ -5,18 +5,6 @@ from .models import Commentary from scipost.models import Contributor -REFUSAL_EMPTY = 0 -REFUSAL_PAPER_EXISTS = -1 -REFUSAL_UNTRACEBLE = -2 -REFUSAL_ARXIV_EXISTS = -3 -COMMENTARY_REFUSAL_CHOICES = ( - (REFUSAL_EMPTY, '-'), - (REFUSAL_PAPER_EXISTS, 'a commentary on this paper already exists'), - (REFUSAL_UNTRACEBLE, 'this paper cannot be traced'), - (REFUSAL_ARXIV_EXISTS, 'there exists a more revent version of this arXiv preprint'), - ) -commentary_refusal_dict = dict(COMMENTARY_REFUSAL_CHOICES) - class DOIToQueryForm(forms.Form): doi = forms.CharField(widget=forms.TextInput( @@ -112,6 +100,18 @@ class VetCommentaryForm(forms.Form): (ACTION_ACCEPT, 'accept'), (ACTION_REFUSE, 'refuse (give reason below)'), ) + REFUSAL_EMPTY = 0 + REFUSAL_PAPER_EXISTS = -1 + REFUSAL_UNTRACEBLE = -2 + REFUSAL_ARXIV_EXISTS = -3 + COMMENTARY_REFUSAL_CHOICES = ( + (REFUSAL_EMPTY, '-'), + (REFUSAL_PAPER_EXISTS, 'a commentary on this paper already exists'), + (REFUSAL_UNTRACEBLE, 'this paper cannot be traced'), + (REFUSAL_ARXIV_EXISTS, 'there exists a more revent version of this arXiv preprint'), + ) + COMMENTARY_REFUSAL_DICT = dict(COMMENTARY_REFUSAL_CHOICES) + action_option = forms.ChoiceField(widget=forms.RadioSelect, choices=COMMENTARY_ACTION_CHOICES, required=True, label='Action') @@ -135,7 +135,7 @@ class VetCommentaryForm(forms.Form): self.add_error(None, 'No `commentary_id` provided') return cleaned_data else: - self.commentary = Commentary.objects.get(pk=self.commentary_id) + self.commentary = Commentary.objects.select_related('requested_by__user').get(pk=self.commentary_id) # Check valid `user` if not self.user: @@ -156,10 +156,13 @@ class VetCommentaryForm(forms.Form): self._form_is_cleaned() return self.commentary + def get_refusal_reason(self): + """Return refusal reason""" + if self.commentary_is_refused(): + return self.COMMENTARY_REFUSAL_DICT[int(self.cleaned_data['refusal_reason'])] + def process_commentary(self): """Vet the commentary or delete it from the database""" - self._form_is_cleaned() - if self.commentary_is_accepted(): self.commentary.vetted = True self.commentary.vetted_by = Contributor.objects.get(user=self.user) diff --git a/commentaries/views.py b/commentaries/views.py index 015d357988ba70cac71cb6e687e325f31240856d..aa653f178663c94fbdecb8bca2d708691b73bfe3 100644 --- a/commentaries/views.py +++ b/commentaries/views.py @@ -12,10 +12,11 @@ from django.core.mail import EmailMessage from django.core.urlresolvers import reverse from django.http import HttpResponse from django.shortcuts import redirect +from django.template.loader import render_to_string from .models import Commentary from .forms import RequestCommentaryForm, DOIToQueryForm, IdentifierToQueryForm -from .forms import VetCommentaryForm, CommentarySearchForm, commentary_refusal_dict +from .forms import VetCommentaryForm, CommentarySearchForm from comments.models import Comment from comments.forms import CommentForm @@ -240,33 +241,15 @@ def vet_commentary_request_ack(request, commentary_id): if form.is_valid(): # Get commentary commentary = form.get_commentary() + email_context = { + 'commentary': commentary + } + + # Retrieve email_template for action if form.commentary_is_accepted(): - # Accept the commentary as is - email_args = ( - 'SciPost Commentary Page activated', - ('Dear ' + title_dict[commentary.requested_by.title] + ' ' - + commentary.requested_by.user.last_name - + ', \n\nThe Commentary Page you have requested, ' - 'concerning publication with title ' - + commentary.pub_title + ' by ' + commentary.author_list - + ', has been activated at https://scipost.org/commentary/' - + str(commentary.arxiv_or_DOI_string) - + '. You are now welcome to submit your comments.' - '\n\nThank you for your contribution, \nThe SciPost Team.') - ) + email_template = 'commentaries/vet_commentary_email_accepted.html' elif form.commentary_is_modified(): - # Re-edit the form starting from the data provided - email_args = ( - 'SciPost Commentary Page activated', - ('Dear ' + title_dict[commentary.requested_by.title] + ' ' - + commentary.requested_by.user.last_name - + ', \n\nThe Commentary Page you have requested, ' - 'concerning publication with title ' + commentary.pub_title - + ' by ' + commentary.author_list - + ', has been activated (with slight modifications to your submitted details).' - ' You are now welcome to submit your comments.' - '\n\nThank you for your contribution, \nThe SciPost Team.') - ) + email_template = 'commentaries/vet_commentary_email_modified.html' request_commentary_form = RequestCommentaryForm(initial={ 'pub_title': commentary.pub_title, @@ -277,33 +260,24 @@ def vet_commentary_request_ack(request, commentary_id): 'pub_abstract': commentary.pub_abstract }) elif form.commentary_is_refused(): - # The commentary request is simply rejected - email_text = ('Dear ' + title_dict[commentary.requested_by.title] + ' ' - + commentary.requested_by.user.last_name - + ', \n\nThe Commentary Page you have requested, ' - 'concerning publication with title ' - + commentary.pub_title + ' by ' + commentary.author_list - + ', has not been activated for the following reason: ' - + commentary_refusal_dict[int(form.cleaned_data['refusal_reason'])] - + '.\n\nThank you for your interest, \nThe SciPost Team.') - - if form.cleaned_data['email_response_field']: - email_text += '\n\nFurther explanations: ' - email_text += form.cleaned_data['email_response_field'] - - email_args = ( - 'SciPost Commentary Page activated', - email_text - ) + email_template = 'commentaries/vet_commentary_email_rejected.html' + email_context['refusal_reason'] = form.get_refusal_reason() + email_context['further_explanation'] = form.cleaned_data['email_response_field'] # Send email and process form - email_args += ([commentary.requested_by.user.email], ['commentaries@scipost.org'],) + email_text = render_to_string(email_template, email_context) + email_args = ( + 'SciPost Commentary Page activated', + email_text, + [commentary.requested_by.user.email], + ['commentaries@scipost.org'] + ) emailmessage = EmailMessage(*email_args, reply_to=['commentaries@scipost.org']) emailmessage.send(fail_silently=False) commentary = form.process_commentary() + # For a modified commentary, redirect to request_commentary_form if form.commentary_is_modified(): - # For a modified commentary, redirect to request_commentary_form context = {'form': request_commentary_form} return render(request, 'commentaries/request_commentary.html', context) diff --git a/scipost/models.py b/scipost/models.py index 033c08ec50202e4cc88e200a4db4f8ee523a5be2..9fc4c36328d65f7573f4beec500951e27c832cbd 100644 --- a/scipost/models.py +++ b/scipost/models.py @@ -109,6 +109,9 @@ class Contributor(models.Model): def __str__(self): return '%s, %s' % (self.user.last_name, self.user.first_name) + def get_title(self): + return title_dict[self.title] + def is_currently_available(self): unav_periods = UnavailabilityPeriod.objects.filter(contributor=self)