From 027a9b7338126ce2e0b3bd830205cb6c6858e134 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Thu, 22 Sep 2016 14:06:20 +0200
Subject: [PATCH] Add Ambassadors who can send (signed for type C) invitations

---
 .../commands/add_groups_and_permissions.py    | 10 +++
 .../scipost/registration_invitations.html     |  2 +-
 scipost/utils.py                              | 79 +++++++++++--------
 scipost/views.py                              | 24 +++++-
 4 files changed, 78 insertions(+), 37 deletions(-)

diff --git a/scipost/management/commands/add_groups_and_permissions.py b/scipost/management/commands/add_groups_and_permissions.py
index 07e702260..b7ef2c60d 100644
--- a/scipost/management/commands/add_groups_and_permissions.py
+++ b/scipost/management/commands/add_groups_and_permissions.py
@@ -17,6 +17,7 @@ class Command(BaseCommand):
         VettingEditors, created = Group.objects.get_or_create(name='Vetting Editors')
         RegisteredContributors, created = Group.objects.get_or_create(name='Registered Contributors')
         Testers, created = Group.objects.get_or_create(name='Testers')
+        Ambassadors, created = Group.objects.get_or_create(name='Ambassadors')
 
         # Create Permissions
         content_type = ContentType.objects.get_for_model(Contributor)
@@ -31,6 +32,12 @@ class Command(BaseCommand):
             name= 'Can manage registration invitations',
             content_type=content_type)
 
+        # Invitations
+        can_invite_Fellows, created = Permission.objects.get_or_create(
+            codename='can_invite_Fellows',
+            name= 'Can invite Fellows',
+            content_type=content_type)
+
         # Communications
         can_email_group_members, created = Permission.objects.get_or_create(
             codename='can_email_group_members',
@@ -176,5 +183,8 @@ class Command(BaseCommand):
             can_request_thesislinks,
             can_referee,
         )
+        Ambassadors.permissions.add(
+            can_manage_registration_invitations,
+        )
 
         self.stdout.write(self.style.SUCCESS('Successfully created groups and permissions'))
diff --git a/scipost/templates/scipost/registration_invitations.html b/scipost/templates/scipost/registration_invitations.html
index 8ef350aec..ae1e5ad99 100644
--- a/scipost/templates/scipost/registration_invitations.html
+++ b/scipost/templates/scipost/registration_invitations.html
@@ -41,7 +41,7 @@
     <h2>Send a new invitation:</h2>
   </div>
   {% if errormessage %}
-  <h3>{{ errormessage }}</h3>
+  <h3 style="color: red;">{{ errormessage }}</h3>
   {% endif %}
   <form action="{% url 'scipost:registration_invitations' %}" method="post">
     {% csrf_token %}
diff --git a/scipost/utils.py b/scipost/utils.py
index 0bc63cac7..f3866b71a 100644
--- a/scipost/utils.py
+++ b/scipost/utils.py
@@ -166,6 +166,9 @@ class Utils(object):
 
     @classmethod
     def send_registration_invitation_email(cls, renew=False):
+        signature = (title_dict[cls.invitation.invited_by.title] + ' '
+                     + cls.invitation.invited_by.user.first_name + ' '
+                     + cls.invitation.invited_by.user.last_name)
         if not renew:
             # Generate email activation key and link
             salt = ""
@@ -225,23 +228,26 @@ class Utils(object):
             '\n\nAs a professional academic, you can register at '
             'https://scipost.org/register, enabling you to contribute to the site\'s '
             'contents, for example by offering submissions, reports and comments.'
-            '\n\nFor your convenience, I have prepared a partly pre-filled registration '
-            'form at https://scipost.org/invitation/' + cls.invitation.invitation_key 
+            '\n\nFor your convenience, a partly pre-filled registration '
+            'form has been prepared for you at '
+            'https://scipost.org/invitation/' + cls.invitation.invitation_key 
             + ' (you can in any case still register at '
             'https://scipost.org/register).\n\n'
             'If you do develop sympathy for the initiative, besides participating in the '
-            'online platform, I would be very grateful if you considered submitting a '
+            'online platform, we would be very grateful if you considered submitting a '
             'publication to one of the journals within the near future, in order to help '
-            'establish their reputation. I\'ll also be looking forward to your reaction, '
-            'comments and suggestions about the initiative, which I hope you will find '
+            'establish their reputation. We\'ll also be looking forward to your reaction, '
+            'comments and suggestions about the initiative, which we hope you will find '
             'useful to your work as a professional scientist.'
             '\n\nMany thanks in advance for taking a few minutes to look into it,'
             '\n\nOn behalf of the SciPost Foundation,\n\n'
-            'Prof. dr Jean-Sébastien Caux\n---------------------------------------------'
-            '\nInstitute for Theoretical Physics\nUniversity of Amsterdam\nScience Park 904'
-            '\n1098 XH Amsterdam\nThe Netherlands\n'
-            '---------------------------------------------\ntel.: +31 (0)20 5255775'
-            '\nfax: +31 (0)20 5255778\n---------------------------------------------')
+            #'Prof. dr Jean-Sébastien Caux\n---------------------------------------------'
+            #'\nInstitute for Theoretical Physics\nUniversity of Amsterdam\nScience Park 904'
+            #'\n1098 XH Amsterdam\nThe Netherlands\n'
+            #'---------------------------------------------\ntel.: +31 (0)20 5255775'
+            #'\nfax: +31 (0)20 5255778\n---------------------------------------------'
+            + signature + '\n'
+        )
 
         summary_text_html = (
             '\n<p>In summary, SciPost.org is a publication portal managed by '
@@ -259,28 +265,30 @@ class Utils(object):
             '<a href="https://scipost.org/register">registration page</a>, '
             'enabling you to contribute to the site\'s '
             'contents, for example by offering submissions, reports and comments.</p>'
-            '\n<p>For your convenience, I have prepared a partly pre-filled '
+            '\n<p>For your convenience, a partly pre-filled '
             '<a href="https://scipost.org/invitation/{{ invitation_key }}">registration form</a>'
-            ' (you can in any case still register at the '
+            ' has been prepared for you (you can in any case still register at the '
             '<a href="https://scipost.org/register">registration page</a>).</p>'
             '\n<p>If you do develop sympathy for the initiative, besides participating in the '
-            'online platform, I would be very grateful if you considered submitting a '
+            'online platform, we would be very grateful if you considered submitting a '
             'publication to one of the journals within the near future, in order to help '
-            'establish their reputation. I\'ll also be looking forward to your reaction, '
-            'comments and suggestions about the initiative, which I hope you will find '
+            'establish their reputation. We\'ll also be looking forward to your reaction, '
+            'comments and suggestions about the initiative, which we hope you will find '
             'useful to your work as a professional scientist.</p>'
             '\n<p>Many thanks in advance for taking a few minutes to look into it,</p>'
             '<p>On behalf of the SciPost Foundation,</p>'
-            '<br/>Prof. dr Jean-Sébastien Caux'
-            '<br/>---------------------------------------------'
-            '<br/>Institute for Theoretical Physics'
-            '<br/>University of Amsterdam'
-            '<br/>Science Park 904'
-            '<br/>1098 XH Amsterdam<br/>The Netherlands'
-            '<br/>---------------------------------------------'
-            '<br/>tel.: +31 (0)20 5255775'
-            '<br/>fax: +31 (0)20 5255778'
-            '<br/>---------------------------------------------')
+            # '<br/>Prof. dr Jean-Sébastien Caux'
+            # '<br/>---------------------------------------------'
+            # '<br/>Institute for Theoretical Physics'
+            # '<br/>University of Amsterdam'
+            # '<br/>Science Park 904'
+            # '<br/>1098 XH Amsterdam<br/>The Netherlands'
+            # '<br/>---------------------------------------------'
+            # '<br/>tel.: +31 (0)20 5255775'
+            # '<br/>fax: +31 (0)20 5255778'
+            # '<br/>---------------------------------------------'
+            '<p>' + signature + '</p>'
+        )
         email_context['invitation_key'] = cls.invitation.invitation_key
 
         if cls.invitation.invitation_type == 'R':
@@ -379,9 +387,10 @@ class Utils(object):
             #emailmessage = EmailMessage(
             emailmessage = EmailMultiAlternatives(
                 'SciPost: invitation', email_text,
-                'J.-S. Caux <jscaux@scipost.org>',
+                #'J.-S. Caux <jscaux@scipost.org>',
+                'SciPost registration <registration@scipost.org>',
                 [cls.invitation.email],
-                ['registration@scipost.org'],
+                bcc=['registration@scipost.org'],
                 reply_to=['registration@scipost.org'])
             emailmessage.attach_alternative(html_version, 'text/html')
 
@@ -416,20 +425,21 @@ class Utils(object):
             #emailmessage = EmailMessage(
             emailmessage = EmailMultiAlternatives(
                 'SciPost: invitation', email_text,
-                'J.-S. Caux <jscaux@scipost.org>',
+                #'J.-S. Caux <jscaux@scipost.org>',
+                'SciPost registration <registration@scipost.org>'
                 [cls.invitation.email],
-                ['registration@scipost.org'],
+                bcc=['registration@scipost.org'],
                 reply_to=['registration@scipost.org'])
             emailmessage.attach_alternative(html_version, 'text/html')
 
         elif cls.invitation.invitation_type == 'C':
             email_text += ('I would hereby like to quickly introduce '
-                           'you to a scientific publishing initiative I recently launched, '
+                           'you to a scientific publishing initiative '
                            'called SciPost, and to invite you to become an active Contributor.')
             email_text += summary_text
             email_text_html += (
                 '<p>I would hereby like to quickly introduce '
-                'you to a scientific publishing initiative I recently launched, '
+                'you to a scientific publishing initiative '
                 'called SciPost, and to invite you to become an active Contributor.</p>')
             email_text_html += summary_text_html + '<br/>' + EMAIL_FOOTER
             html_template = Template(email_text_html)
@@ -437,9 +447,10 @@ class Utils(object):
             #emailmessage = EmailMessage(
             emailmessage = EmailMultiAlternatives(
                 'SciPost: invitation', email_text,
-                'J.-S. Caux <jscaux@scipost.org>',
+                #'J.-S. Caux <jscaux@scipost.org>',
+                'SciPost registration <registration@scipost.org>'
                 [cls.invitation.email],
-                ['registration@scipost.org'],
+                bcc=['registration@scipost.org'],
                 reply_to=['registration@scipost.org'])
             emailmessage.attach_alternative(html_version, 'text/html')
 
@@ -598,7 +609,7 @@ class Utils(object):
                 'SciPost registration invitation', email_text,
                 'J-S Caux <jscaux@scipost.org>',
                 [cls.invitation.email],
-                ['registration@scipost.org'],
+                bcc=['registration@scipost.org'],
                 reply_to=['registration@scipost.org'])
             emailmessage.attach_alternative(html_version, 'text/html')
             
diff --git a/scipost/views.py b/scipost/views.py
index de8dcf17f..173367596 100644
--- a/scipost/views.py
+++ b/scipost/views.py
@@ -415,7 +415,8 @@ def vet_registration_request_ack(request, contributor_id):
                 email_text += 'Thank you very much in advance, \nThe SciPost Team.'
                 emailmessage = EmailMessage('SciPost registration accepted', email_text,
                                             'SciPost registration <registration@scipost.org>', 
-                                            [contributor.user.email, 'registration@scipost.org'], 
+                                            [contributor.user.email], 
+                                            bcc=['registration@scipost.org'], 
                                             reply_to=['registration@scipost.org'])
                 emailmessage.send(fail_silently=False)
             else:
@@ -432,7 +433,8 @@ def vet_registration_request_ack(request, contributor_id):
                     email_text += '\n\nFurther explanations: ' + form.cleaned_data['email_response_field']
                 emailmessage = EmailMessage('SciPost registration: unsuccessful', 
                                             email_text, 'SciPost registration <registration@scipost.org>', 
-                                            [contributor.user.email, 'registration@scipost.org'], 
+                                            [contributor.user.email], 
+                                            bcc=['registration@scipost.org'], 
                                             reply_to=['registration@scipost.org'])
                 emailmessage.send(fail_silently=False)
                 contributor.status = form.cleaned_data['refusal_reason']
@@ -461,6 +463,13 @@ def registration_invitations(request):
                 errormessage = 'DUPLICATE ERROR: This email address has already been used for an invitation'
             elif Utils.email_already_taken():
                 errormessage = 'DUPLICATE ERROR: This email address is already associated to a Contributor'
+            elif (reg_inv_form.cleaned_data['invitation_type'] == 'F' 
+                  and not request.user.has_perm('scipost.can_invite_Fellows')):
+                errormessage = ('You do not have the authorization to send a Fellow-type '
+                                'invitation. Consider Contributor, or cited (sub/pub). ')
+            elif (reg_inv_form.cleaned_data['invitation_type'] == 'R'):
+                errormessage = ('Referee-type invitations must be made by the Editor-in-charge '
+                                'at the relevant Submission\'s Editorial Page. ')
             else:
                 Utils.create_invitation()
                 Utils.send_registration_invitation_email()
@@ -548,6 +557,17 @@ def renew_registration_invitation(request, invitation_id):
     Renew an invitation (called from registration_invitations).
     """
     invitation = get_object_or_404(RegistrationInvitation, pk=invitation_id)
+    errormessage = None
+    if (invitation.invitation_type == 'F' 
+        and not request.user.has_perm('scipost.can_invite_Fellows')):
+        errormessage = ('You do not have the authorization to send a Fellow-type '
+                        'invitation. Consider Contributor, or cited (sub/pub). ')
+    elif invitation.invitation_type == 'R':
+        errormessage = ('Referee-type invitations must be made by the Editor-in-charge '
+                        'at the relevant Submission\'s Editorial Page. ')
+    if errormessage is not None:
+        return render(request, 'scipost/error.html', context={'errormessage': errormessage})
+        
     Utils.load({'invitation': invitation})
     Utils.send_registration_invitation_email(True)
     return redirect(reverse('scipost:registration_invitations'))
-- 
GitLab