From c4709fa300fba4f6f398f6d1f95c5c28f2a54cfd Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Wed, 16 Dec 2015 21:10:44 +0100
Subject: [PATCH] Begun with introducing email functionality.

---
 commentaries/views.py                         | 15 +++++++++++--
 contributors/models.py                        |  7 ++++---
 .../templates/contributors/personal_page.html | 16 +++++++++++---
 contributors/views.py                         | 21 +++++++++++++------
 4 files changed, 45 insertions(+), 14 deletions(-)

diff --git a/commentaries/views.py b/commentaries/views.py
index b13d0b4d2..1dbd64d32 100644
--- a/commentaries/views.py
+++ b/commentaries/views.py
@@ -3,7 +3,8 @@ from django.utils import timezone
 from django.shortcuts import get_object_or_404, render
 from django.contrib.auth import authenticate, login, logout
 from django.contrib.auth.models import User
-from django.core.mail import send_mail
+#from django.core.mail import send_mail
+from django.core.mail import EmailMessage
 from django.core.urlresolvers import reverse
 from django.http import HttpResponse, HttpResponseRedirect
 from django.views.decorators.csrf import csrf_protect
@@ -69,15 +70,25 @@ def vet_commentary_request_ack(request, commentary_id):
                 # accept the commentary as is
                 commentary.vetted = True
                 commentary.save()
+                email_text = 'The Commentary Page you have requested, concerning publication with title ' + commentary.pub_title + ' by ' + commentary.author_list + ', has been activated. You are now welcome to submit your comments.' + '\n\nThank you for your contribution, \nThe SciPost Team.'
+                emailmessage = EmailMessage('SciPost Commentary Page activated', email_text, 'noreply@scipost.org', ['jscaux@gmail.com'], reply_to=['J.S.Caux@uva.nl'])
+                emailmessage.send(fail_silently=False)                
             elif form.cleaned_data['action_option'] == '0':
                 # re-edit the form starting from the data provided
                 form2 = RequestCommentaryForm(initial={'pub_title': commentary.pub_title, 'arxiv_link': commentary.arxiv_link, 'pub_DOI_link': commentary.pub_DOI_link, 'author_list': commentary.author_list, 'pub_date': commentary.pub_date, 'pub_abstract': commentary.pub_abstract})
                 commentary.delete()
+                email_text = 'The 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.'
+                emailmessage = EmailMessage('SciPost Commentary Page activated', email_text, 'noreply@scipost.org', ['jscaux@gmail.com'], reply_to=['J.S.Caux@uva.nl'])
+                emailmessage.send(fail_silently=False)                
                 context = {'form': form2 }
                 return render(request, 'commentaries/request_commentary.html', context)
             elif form.cleaned_data['action_option'] == '2':
                 # the commentary request is simply rejected
-                # email Contributor about it...
+                email_text = 'The Commentary Page you have requested, concerning publication with title ' + commentary.pub_title + ' by ' + commentary.author_list + ', has not been activated for the following reason: ' + 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: ' + form.cleaned_data['email_response_field']
+                emailmessage = EmailMessage('SciPost Commentary Page activated', email_text, 'noreply@scipost.org', ['jscaux@gmail.com'], reply_to=['J.S.Caux@uva.nl'])
+                emailmessage.send(fail_silently=False)                
                 commentary.delete()
 
 #    context = {'option': form.cleaned_data['action_option'], 'reason': form.cleaned_data['refusal_reason'] }
diff --git a/contributors/models.py b/contributors/models.py
index b3332ebc3..f1cb08c7a 100644
--- a/contributors/models.py
+++ b/contributors/models.py
@@ -47,12 +47,13 @@ class Contributor(models.Model):
     affiliation = models.CharField(max_length=300, verbose_name='affiliation')
     address = models.CharField(max_length=1000, blank=True, verbose_name="address")
     personalwebpage = models.URLField(blank=True, verbose_name='personal web page')
+    #vetted_by = models.OneToOneField(Contributor, related_name='vetted_by') TO ACTIVATE
 
+    nr_comments = models.PositiveSmallIntegerField(default=0)
     nr_comment_relevance_ratings = models.IntegerField(default=0)
     comment_relevance_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
     nr_comment_importance_ratings = models.IntegerField(default=0)
     comment_importance_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
-    nr_comments = models.PositiveSmallIntegerField(default=0)
     nr_comment_clarity_ratings = models.IntegerField(default=0)
     comment_clarity_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
     nr_comment_validity_ratings = models.IntegerField(default=0)
@@ -60,11 +61,11 @@ class Contributor(models.Model):
     nr_comment_rigour_ratings = models.IntegerField(default=0)
     comment_rigour_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
 
+    nr_authorreplies = models.PositiveSmallIntegerField(default=0)
     nr_authorreply_relevance_ratings = models.IntegerField(default=0)
     authorreply_relevance_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
     nr_authorreply_importance_ratings = models.IntegerField(default=0)
     authorreply_importance_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
-    nr_authorreplys = models.PositiveSmallIntegerField(default=0)
     nr_authorreply_clarity_ratings = models.IntegerField(default=0)
     authorreply_clarity_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
     nr_authorreply_validity_ratings = models.IntegerField(default=0)
@@ -72,11 +73,11 @@ class Contributor(models.Model):
     nr_authorreply_rigour_ratings = models.IntegerField(default=0)
     authorreply_rigour_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
 
+    nr_reports = models.PositiveSmallIntegerField(default=0)
     nr_report_relevance_ratings = models.IntegerField(default=0)
     report_relevance_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
     nr_report_importance_ratings = models.IntegerField(default=0)
     report_importance_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
-    nr_reports = models.PositiveSmallIntegerField(default=0)
     nr_report_clarity_ratings = models.IntegerField(default=0)
     report_clarity_rating = models.DecimalField(default=0, max_digits=3, decimal_places=0)
     nr_report_validity_ratings = models.IntegerField(default=0)
diff --git a/contributors/templates/contributors/personal_page.html b/contributors/templates/contributors/personal_page.html
index 3bfeab56e..d31671494 100644
--- a/contributors/templates/contributors/personal_page.html
+++ b/contributors/templates/contributors/personal_page.html
@@ -58,10 +58,10 @@
   <h1>Your SciPost Account</h1>
   <hr>
   <div class="row">
-    <div class="col-4">
+    <div class="col-3">
       <h3>Your ratings:</h3>
     </div>
-    <div class="col-4">
+    <div class="col-3">
       <table class="ratings">
 	<th>Reports</th><th>(nr: {{ contributor.nr_reports }})</th>
 	<tr><td>relevance:</td><td>{{ contributor.report_relevance_rating }}% ({{ contributor.nr_report_relevance_ratings }})</td></tr>
@@ -71,7 +71,7 @@
 	<tr><td>rigour:</td><td>{{ contributor.report_rigour_rating }}% ({{ contributor.nr_report_rigour_ratings }})</td></tr>
       </table>
     </div>
-    <div class="col-4">
+    <div class="col-3">
       <table class="ratings">
 	<th>Comments</th><th>(nr: {{ contributor.nr_comments }})</th>
 	<tr><td>relevance:</td><td>{{ contributor.comment_relevance_rating }}% ({{ contributor.nr_comment_relevance_ratings }})</td></tr>
@@ -81,6 +81,16 @@
 	<tr><td>rigour:</td><td>{{ contributor.comment_rigour_rating }}% ({{ contributor.nr_comment_rigour_ratings }})</td></tr>
       </table>
     </div>
+    <div class="col-3">
+      <table class="ratings">
+	<th>Author Replies</th><th>(nr: {{ contributor.nr_authorreplies }})</th>
+	<tr><td>relevance:</td><td>{{ contributor.authorreply_relevance_rating }}% ({{ contributor.nr_authorreply_relevance_ratings }})</td></tr>
+	<tr><td>importance:</td><td>{{ contributor.authorreply_importance_rating }}% ({{ contributor.nr_authorreply_importance_ratings }})</td></tr>
+	<tr><td>clarity:</td><td>{{ contributor.authorreply_clarity_rating }}% ({{ contributor.nr_authorreply_clarity_ratings }})</td></tr>
+	<tr><td>validity:</td><td>{{ contributor.authorreply_validity_rating }}% ({{ contributor.nr_authorreply_validity_ratings }})</td></tr>
+	<tr><td>rigour:</td><td>{{ contributor.authorreply_rigour_rating }}% ({{ contributor.nr_authorreply_rigour_ratings }})</td></tr>
+      </table>
+    </div>
   </div>
 </section>
 
diff --git a/contributors/views.py b/contributors/views.py
index b2f3ba289..a6fe4152f 100644
--- a/contributors/views.py
+++ b/contributors/views.py
@@ -3,7 +3,8 @@ from django.utils import timezone
 from django.shortcuts import get_object_or_404, render
 from django.contrib.auth import authenticate, login, logout
 from django.contrib.auth.models import User
-from django.core.mail import send_mail
+#from django.core.mail import send_mail
+from django.core.mail import EmailMessage
 from django.core.urlresolvers import reverse
 from django.http import HttpResponse, HttpResponseRedirect
 from django.views.decorators.csrf import csrf_protect
@@ -73,14 +74,18 @@ def vet_registration_request_ack(request, contributor_id):
         contributor = Contributor.objects.get(pk=contributor_id)
         if form.is_valid():
             if form.cleaned_data['promote_to_rank_1']:
-#            if request.POST['promote_to_rank_1']:
-#            if form['promote_to_rank_1']:
                 contributor.rank = 1
                 contributor.save()
+                email_text = 'Dear' + contributor.title + ' ' + contributor.user.last_name + ', \n Your registration to the SciPost publication portal has been accepted. You can now login and contribute. \n\n The SciPost Team.'
+                #send_mail('SciPost registration accepted', email_text, 'noreply@scipost.org', [contributor.user.email, 'noreply@scipost.org'], fail_silently=False)
+                emailmessage = EmailMessage('SciPost registration accepted', email_text, 'noreply@scipost.org', [contributor.user.email, 'jscaux@gmail.com'], reply_to=['J.S.Caux@uva.nl'])
+                emailmessage.send(fail_silently=False)
             else:
-                #            email_text = 'Dear ' . contributor.title . ' ' . contributor.user.last_name . ', \n Your registration to the SciPost publication portal has been turned down (you can still view all the content, just not submit papers, comments or votes). We nonetheless thank you for your interest. \n\n The SciPost team.' # Syntax error here, don't see it.
-                email_text = 'Dear'
-            #            send_mail('SciPost registration: unauthorized', email_text, 'admin@scipost.org', [contributor.user.email, 'admin@scipost.org'], fail_silently=False) # Activate later, when scipost email is running
+                email_text = 'Dear ' + contributor.title + ' ' + contributor.user.last_name + ', \n Your registration to the SciPost publication portal has been turned down, the reason being: ' + form.cleaned_data['refusal_reason'] + '. You can however still view all SciPost contents, just not submit papers, comments or votes. We nonetheless thank you for your interest. \n\n The SciPost Team.'
+                if form.cleaned_data['email_response_field']:
+                    email_text += '\n\nFurther explanations: ' + form.cleaned_data['email_response_field']
+                emailmessage = EmailMessage('SciPost registration: unsuccessful', email_text, 'noreply@scipost.org', [contributor.user.email, 'jscaux@gmail.com'], reply_to=['J.S.Caux@uva.nl'])
+                emailmessage.send(fail_silently=False)
                 contributor.rank = form.cleaned_data['refusal_reason']
                 contributor.save()
 
@@ -116,6 +121,10 @@ def logout_view(request):
 def personal_page(request):
     if request.user.is_authenticated():
         contributor = Contributor.objects.get(user=request.user)
+        # email testing 2015-12-16: works!
+        #emailmessage = EmailMessage('Welcome to your personal page.', 'Hello', 'noreply@scipost.org', [contributor.user.email, 'jscaux@gmail.com'], reply_to=['J.S.Caux@uva.nl'])
+        #emailmessage.send(fail_silently=False)
+
         # if an editor, count the number of actions required:
         nr_reg_to_vet = 0
         nr_submissions_to_process = 0
-- 
GitLab