From f4089abfbd8633d4cfc7aae175ccbf11c5c60e59 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sun, 4 Feb 2018 19:51:18 +0100
Subject: [PATCH] Basic email report/comment made citable

---
 .../journals/manage_comment_metadata.html     |  1 +
 .../journals/manage_report_metadata.html      |  1 +
 journals/urls/general.py                      |  3 +++
 journals/utils.py                             | 24 +++++++++++++++----
 journals/views.py                             | 24 +++++++++++++++++++
 .../email/email_comment_made_citable.html     | 18 ++++++++++++++
 .../email/email_comment_made_citable.txt      |  7 ++++++
 .../email/email_report_made_citable.html      | 22 +++++++++++++++++
 templates/email/email_report_made_citable.txt |  8 +++++++
 9 files changed, 103 insertions(+), 5 deletions(-)
 create mode 100644 templates/email/email_comment_made_citable.html
 create mode 100644 templates/email/email_comment_made_citable.txt
 create mode 100644 templates/email/email_report_made_citable.html
 create mode 100644 templates/email/email_report_made_citable.txt

diff --git a/journals/templates/journals/manage_comment_metadata.html b/journals/templates/journals/manage_comment_metadata.html
index efd231d30..d51e10289 100644
--- a/journals/templates/journals/manage_comment_metadata.html
+++ b/journals/templates/journals/manage_comment_metadata.html
@@ -48,6 +48,7 @@ event: "focusin"
         <ul>
 	  <li>Mark DOI as <a href="{% url 'journals:mark_comment_doi_needed' comment_id=comment.id needed=1 %}">needed</a> / <a href="{% url 'journals:mark_comment_doi_needed' comment_id=comment.id needed=0 %}">not needed</a></li>
           <li><a href="{% url 'journals:generic_metadata_xml_deposit' type_of_object='comment' object_id=comment.id %}">Create the metadata and deposit it to Crossref</a></li>
+	  <li><a href="{% url 'journals:email_object_made_citable' type_of_object='comment' object_id=comment.id %}">Email comment author: made citable</a>
 	</ul>
 
 	<h2 class="ml-3">Crossref Deposits</h2>
diff --git a/journals/templates/journals/manage_report_metadata.html b/journals/templates/journals/manage_report_metadata.html
index 9d491ccec..d35279925 100644
--- a/journals/templates/journals/manage_report_metadata.html
+++ b/journals/templates/journals/manage_report_metadata.html
@@ -63,6 +63,7 @@ event: "focusin"
         <ul>
 	  <li>Mark DOI as <a href="{% url 'journals:mark_report_doi_needed' report_id=report.id needed=1 %}">needed</a> / <a href="{% url 'journals:mark_report_doi_needed' report_id=report.id needed=0 %}">not needed</a></li>
           <li><a href="{% url 'journals:generic_metadata_xml_deposit' type_of_object='report' object_id=report.id %}">Create the metadata and deposit it to Crossref</a></li>
+	  <li><a href="{% url 'journals:email_object_made_citable' type_of_object='report' object_id=report.id %}">Email report author: made citable</a>
 	</ul>
 
 	<h2 class="ml-3">Crossref Deposits</h2>
diff --git a/journals/urls/general.py b/journals/urls/general.py
index fe28da657..dd9cfea9e 100644
--- a/journals/urls/general.py
+++ b/journals/urls/general.py
@@ -109,4 +109,7 @@ urlpatterns = [
     url(r'^mark_generic_deposit_success/(?P<deposit_id>[0-9]+)/(?P<success>[0-1])$',
         journals_views.mark_generic_deposit_success,
         name='mark_generic_deposit_success'),
+    url(r'^email_object_made_citable/(?P<type_of_object>[a-z]+)/(?P<object_id>[0-9]+)$',
+        journals_views.email_object_made_citable,
+        name='email_object_made_citable'),
 ]
diff --git a/journals/utils.py b/journals/utils.py
index e80f921bf..5b3d5e1da 100644
--- a/journals/utils.py
+++ b/journals/utils.py
@@ -1,12 +1,11 @@
 from django.core.mail import EmailMessage
 
+from scipost.utils import EMAIL_FOOTER
+from common.utils import BaseMailUtil
 
-class JournalUtils(object):
 
-    @classmethod
-    def load(cls, dict):
-        for var_name in dict:
-            setattr(cls, var_name, dict[var_name])
+class JournalUtils(BaseMailUtil):
+
 
     @classmethod
     def send_authors_paper_published_email(cls):
@@ -88,3 +87,18 @@ class JournalUtils(object):
             }
         }
         return md
+
+
+    @classmethod
+    def email_report_made_citable(cls):
+        """ Requires loading 'report' attribute. """
+        cls._send_mail(cls, 'email_report_made_citable',
+                       [cls._context['report'].author.user.email],
+                       'Report made citable')
+
+    @classmethod
+    def email_comment_made_citable(cls):
+        """ Requires loading 'comment' attribute. """
+        cls._send_mail(cls, 'email_comment_made_citable',
+                       [cls._context['comment'].author.user.email],
+                       'Comment made citable')
diff --git a/journals/views.py b/journals/views.py
index 06edc2905..fd11f313c 100644
--- a/journals/views.py
+++ b/journals/views.py
@@ -1263,6 +1263,30 @@ def mark_generic_deposit_success(request, deposit_id, success):
         return redirect(reverse('journals:manage_comment_metadata'))
 
 
+@permission_required('scipost.can_publish_accepted_submission', return_403=True)
+def email_object_made_citable(request, **kwargs):
+    """
+    This method sends an email to the author of a Report or a Comment,
+    to notify that the object has been made citable (doi registered).
+    """
+    type_of_object = kwargs['type_of_object']
+    object_id = int(kwargs['object_id'])
+
+    if type_of_object == 'report':
+        _object = get_object_or_404(Report, id=object_id)
+    elif type_of_object == 'comment':
+        _object = get_object_or_404(Comment, id=object_id)
+
+    if type_of_object == 'report':
+        JournalUtils.load({'report': _object, })
+        JournalUtils.email_report_made_citable()
+        return redirect(reverse('journals:manage_report_metadata'))
+    else:
+        JournalUtils.load({'comment': _object, })
+        JournalUtils.email_comment_made_citable()
+        return redirect(reverse('journals:manage_comment_metadata'))
+
+
 ###########
 # Viewing #
 ###########
diff --git a/templates/email/email_comment_made_citable.html b/templates/email/email_comment_made_citable.html
new file mode 100644
index 000000000..794bb252e
--- /dev/null
+++ b/templates/email/email_comment_made_citable.html
@@ -0,0 +1,18 @@
+<p>Dear {{ comment.author.get_title_display }} {{ comment.author.user.last_name }},</p>
+
+<p>
+    The Comment you have submitted, concerning publication with title
+
+    {{comment.core_content_object.title}} by {% if comment.core_content_object.author_list %}{{comment.core_content_object.author_list}}{% elif comment.core_content_object.author %}{{comment.core_content_object.author}}{% endif %} (<a href="https://scipost.org{{comment.get_absolute_url}}">see on SciPost.org</a>)
+    has been ascribed DOI {{ comment.doi_label }}, and is thus now citable in the form:
+</p>
+<p>
+  {{ comment.citation }}.
+</p>
+<p>
+    Thank you again very much for your contribution.<br><br>
+    The SciPost Team
+</p>
+
+
+{% include 'email/_footer.html' %}
diff --git a/templates/email/email_comment_made_citable.txt b/templates/email/email_comment_made_citable.txt
new file mode 100644
index 000000000..bd47711e3
--- /dev/null
+++ b/templates/email/email_comment_made_citable.txt
@@ -0,0 +1,7 @@
+Dear {{ comment.author.get_title_display }} {{ comment.author.user.last_name }},\n\n
+The Comment you have submitted, concerning publication with title\n
+{{comment.core_content_object.title}} by {% if comment.core_content_object.author_list %}{{comment.core_content_object.author_list}}{% elif comment.core_content_object.author %}{{comment.core_content_object.author}}{% endif %} (https://scipost.org{{comment.get_absolute_url}})\n
+has been ascribed DOI {{ comment.doi_label }}, and is thus now citable in the form:\n\n
+{{ comment.citation }}.\n\n
+Thank you again very much for your contribution.\n
+The SciPost Team
diff --git a/templates/email/email_report_made_citable.html b/templates/email/email_report_made_citable.html
new file mode 100644
index 000000000..384edf7d5
--- /dev/null
+++ b/templates/email/email_report_made_citable.html
@@ -0,0 +1,22 @@
+<p>Dear {{ report.author.get_title_display }} {{ report.author.user.last_name }},</p>
+
+<p>
+  Your Report on Submission:
+</p>
+<p>
+  {{ report.submission.title }}
+  <br/>
+  by {{ report.submission.author_list }}
+</p>
+<p>
+  has been ascribed DOI {{ report.doi_label }}, and is thus now citable in the form:
+</p>
+<p>
+  {{ report.citation }}.
+</p>
+<p>
+  We thank you again very much for your contribution.<br/>
+  The SciPost Team
+</p>
+
+{% include 'email/_footer.html' %}
diff --git a/templates/email/email_report_made_citable.txt b/templates/email/email_report_made_citable.txt
new file mode 100644
index 000000000..e252b6c4d
--- /dev/null
+++ b/templates/email/email_report_made_citable.txt
@@ -0,0 +1,8 @@
+Dear {{ report.author.get_title_display }} {{ report.author.user.last_name }},\n\n
+Your Report on Submission:\n\n
+{{ report.submission.title }}\n
+by {{ report.submission.author_list }}\n\n
+has been ascribed DOI {{ report.doi_label }}, and is thus now citable in the form:\n\n
+{{ report.citation }}.\n\n
+We thank you again very much for your contribution.\n
+The SciPost Team
-- 
GitLab