From e0203c0b26812e585f3259dea0192d43aa5b5870 Mon Sep 17 00:00:00 2001 From: Geert Kapteijns <ghkapteijns@gmail.com> Date: Fri, 10 Feb 2017 18:29:07 +0100 Subject: [PATCH] Make request keyword arg of RequestThesisLinkForm and VetThesisLinkForm instead of user Change all involved tests. Start moving email to templates. --- theses/forms.py | 35 +++++++++++-------- .../templates/theses/thesislink_accepted.html | 0 .../templates/theses/thesislink_accepted.txt | 8 +++++ theses/test_forms.py | 22 +++++++----- theses/test_views.py | 9 +++-- theses/views.py | 4 +-- 6 files changed, 51 insertions(+), 27 deletions(-) create mode 100644 theses/templates/theses/thesislink_accepted.html create mode 100644 theses/templates/theses/thesislink_accepted.txt diff --git a/theses/forms.py b/theses/forms.py index a98241a7e..758b58609 100644 --- a/theses/forms.py +++ b/theses/forms.py @@ -1,5 +1,7 @@ from django import forms from django.core.mail import EmailMessage +from django.template.loader import render_to_string +from django.urls import reverse from scipost.models import Contributor, title_dict @@ -21,7 +23,8 @@ class RequestThesisLinkForm(forms.ModelForm): } def __init__(self, *args, **kwargs): - self.user = kwargs.pop("user") + self.request = kwargs.pop("request") + self.user = self.request.user super(RequestThesisLinkForm, self).__init__(*args, **kwargs) def save(self, *args, **kwargs): @@ -60,24 +63,28 @@ class VetThesisLinkForm(RequestThesisLinkForm): self.order_fields(['action_option', 'refusal_reason', 'justification']) def vet_request(self, thesislink, user): + mail_params = { + 'vocative_title': title_dict[thesislink.requested_by.title], + 'thesislink': thesislink, + 'full_url': self.request.build_absolute_uri( + reverse('theses:thesis', kwargs={'thesislink_id': thesislink.id})) + } if int(self.cleaned_data['action_option']) == VetThesisLinkForm.ACCEPT: thesislink.vetted = True thesislink.vetted_by = Contributor.objects.get(user=user) thesislink.save() - email_text = ('Dear ' + title_dict[thesislink.requested_by.title] + ' ' - + thesislink.requested_by.user.last_name - + ', \n\nThe Thesis Link you have requested, concerning thesis with' - + ' title ' + thesislink.title + ' by ' + thesislink.author - + ', has been activated at https://scipost.org/thesis/' - + str(thesislink.id) + '.' - + '\n\nThank you for your contribution, \nThe SciPost Team.') - emailmessage = EmailMessage('SciPost Thesis Link activated', email_text, - 'SciPost Theses <theses@scipost.org>', - [thesislink.requested_by.user.email], - ['theses@scipost.org'], - reply_to=['theses@scipost.org']) - emailmessage.send(fail_silently=False) + message_plain = render_to_string('theses/thesislink_accepted.txt', mail_params) + print(message_plain) + email = EmailMessage( + 'SciPost Thesis Link activated', + message_plain, + 'SciPost Theses <theses@scipost.org>', + [thesislink.requested_by.user.email], + ['theses@scipost.org'], + reply_to=['theses@scipost.org'] + ) + email.send(fail_silently=False) elif int(self.cleaned_data['action_option']) == VetThesisLinkForm.REFUSE: email_text = ('Dear ' + title_dict[thesislink.requested_by.title] + ' ' + thesislink.requested_by.user.last_name diff --git a/theses/templates/theses/thesislink_accepted.html b/theses/templates/theses/thesislink_accepted.html new file mode 100644 index 000000000..e69de29bb diff --git a/theses/templates/theses/thesislink_accepted.txt b/theses/templates/theses/thesislink_accepted.txt new file mode 100644 index 000000000..d1ccc5414 --- /dev/null +++ b/theses/templates/theses/thesislink_accepted.txt @@ -0,0 +1,8 @@ +Dear {{ vocative_title }} {{ thesislink.requested_by.user.last_name }}, + +The Thesis Link you have requested, concerning thesis with title `{{ thesislink.title }}' by {{ thesislink.author }}, +has been activated at {{ full_url }}. + +Thank you for your contribution, + +The SciPost Team diff --git a/theses/test_forms.py b/theses/test_forms.py index f8f42137b..dd649cedb 100644 --- a/theses/test_forms.py +++ b/theses/test_forms.py @@ -1,6 +1,6 @@ import factory -from django.test import TestCase +from django.test import TestCase, RequestFactory from scipost.factories import ContributorFactory from .factories import ThesisLinkFactory, VetThesisLinkFormFactory @@ -14,30 +14,34 @@ class TestRequestThesisLink(TestCase): def setUp(self): self.contributor = ContributorFactory() self.user = self.contributor.user + self.request = RequestFactory() + self.request.user = self.user self.valid_form_data = model_form_data( - ThesisLinkFactory(), RequestThesisLinkForm, form_kwargs={'user': self.user}) + ThesisLinkFactory(), RequestThesisLinkForm, form_kwargs={'request': self.request}) def test_valid_data_is_valid(self): form_data = self.valid_form_data - form = RequestThesisLinkForm(self.valid_form_data, user=self.user) + form = RequestThesisLinkForm(self.valid_form_data, request=self.request) self.assertTrue(form.is_valid()) def test_data_without_user_is_not_valid(self): form_data = self.valid_form_data - with self.assertRaises(KeyError): - RequestThesisLinkForm(self.valid_form_data) + request = RequestFactory() + with self.assertRaises(AttributeError): + RequestThesisLinkForm(self.valid_form_data, request=request) + # Should we define a more semantic error like UserNotDefinedError? + self.assertTrue(False) def test_empty_domain_is_invalid(self): form_data = self.valid_form_data form_data['domain'] = '' - form = RequestThesisLinkForm(form_data, user=self.user) + form = RequestThesisLinkForm(form_data, request=self.request) self.assertEqual(form.errors['domain'], ['This field is required.']) def test_thesislink_is_requested_by_correct_contributor(self): form_data = self.valid_form_data - contributor = ContributorFactory() - form = RequestThesisLinkForm(form_data, user=contributor.user) + form = RequestThesisLinkForm(form_data, request=self.request) # Check if the user is properly saved to the new ThesisLink as `requested_by` thesislink = form.save() - self.assertEqual(thesislink.requested_by, contributor) + self.assertEqual(thesislink.requested_by, self.contributor) diff --git a/theses/test_views.py b/theses/test_views.py index 7894dc480..f049bbc38 100644 --- a/theses/test_views.py +++ b/theses/test_views.py @@ -102,7 +102,9 @@ class TestVetThesisLinkRequests(TestCase): def test_thesislink_is_vetted_by_correct_contributor_and_mail_is_sent(self): contributor = ContributorFactory() contributor.user.groups.add(Group.objects.get(name="Vetting Editors")) - post_data = model_form_data(ThesisLinkFactory(), VetThesisLinkForm, form_kwargs={'user': contributor.user}) + request = RequestFactory().get(self.target) + request.user = contributor.user + post_data = model_form_data(ThesisLinkFactory(), VetThesisLinkForm, form_kwargs={'request': request}) post_data["action_option"] = VetThesisLinkForm.ACCEPT target = reverse('theses:vet_thesislink', kwargs={'pk': self.thesislink.id}) @@ -124,7 +126,10 @@ class TestVetThesisLinkRequests(TestCase): def test_thesislink_that_is_refused_is_deleted_and_mail_is_sent(self): contributor = ContributorFactory() contributor.user.groups.add(Group.objects.get(name="Vetting Editors")) - post_data = model_form_data(ThesisLinkFactory(), VetThesisLinkForm, form_kwargs={'user': contributor.user}) + request = RequestFactory().get(self.target) + request.user = contributor.user + + post_data = model_form_data(ThesisLinkFactory(), VetThesisLinkForm, form_kwargs={'request': request}) post_data["action_option"] = VetThesisLinkForm.REFUSE target = reverse('theses:vet_thesislink', kwargs={'pk': self.thesislink.id}) diff --git a/theses/views.py b/theses/views.py index da81c1b47..8a56ea9a9 100644 --- a/theses/views.py +++ b/theses/views.py @@ -40,7 +40,7 @@ class RequestThesisLink(CreateView): def get_form_kwargs(self): kwargs = super(RequestThesisLink, self).get_form_kwargs() - kwargs['user'] = self.request.user + kwargs['request'] = self.request return kwargs @@ -82,7 +82,7 @@ class VetThesisLink(UpdateView): def get_form_kwargs(self): kwargs = super(VetThesisLink, self).get_form_kwargs() - kwargs['user'] = self.request.user + kwargs['request'] = self.request return kwargs -- GitLab