From fe1f031026c592703e837053ddadb9632b2020d4 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Wed, 11 Oct 2017 10:35:08 +0200
Subject: [PATCH] Implement HTML editor for mail func

---
 mails/forms.py                                | 21 ++++--
 ...p_mail.txt => partners_followup_mail.html} |  2 +
 .../mail_templates/partners_initial_mail.html | 51 +++++++++++++
 .../mail_templates/partners_initial_mail.txt  | 31 --------
 .../production_send_proofs.html               | 20 ++++++
 .../mail_templates/production_send_proofs.txt | 11 ---
 .../submissions_referee_invite.html           | 29 ++++++++
 .../submissions_referee_invite.txt            | 15 ----
 mails/templates/mails/mail_form.html          |  8 ++-
 mails/widgets.py                              | 72 +++++++++++++++++++
 requirements.txt                              |  1 -
 11 files changed, 195 insertions(+), 66 deletions(-)
 rename mails/templates/mail_templates/{partners_followup_mail.txt => partners_followup_mail.html} (97%)
 create mode 100644 mails/templates/mail_templates/partners_initial_mail.html
 delete mode 100644 mails/templates/mail_templates/partners_initial_mail.txt
 create mode 100644 mails/templates/mail_templates/production_send_proofs.html
 delete mode 100644 mails/templates/mail_templates/production_send_proofs.txt
 create mode 100644 mails/templates/mail_templates/submissions_referee_invite.html
 delete mode 100644 mails/templates/mail_templates/submissions_referee_invite.txt
 create mode 100644 mails/widgets.py

diff --git a/mails/forms.py b/mails/forms.py
index a034d1a75..c277051d2 100644
--- a/mails/forms.py
+++ b/mails/forms.py
@@ -1,5 +1,6 @@
 import json
 import inspect
+from html2text import HTML2Text
 
 from django import forms
 from django.core.mail import EmailMultiAlternatives
@@ -9,10 +10,12 @@ from django.template import loader
 
 from scipost.models import Contributor
 
+from .widgets import SummernoteEditor
+
 
 class EmailTemplateForm(forms.Form):
     subject = forms.CharField(max_length=250, label="Subject*")
-    text = forms.CharField(widget=forms.Textarea(attrs={'rows': 25}), label="Text*")
+    text = forms.CharField(widget=SummernoteEditor, label="Text*")
     extra_recipient = forms.EmailField(label="Optional: bcc this email to", required=False)
 
     def __init__(self, *args, **kwargs):
@@ -21,8 +24,12 @@ class EmailTemplateForm(forms.Form):
         super().__init__(*args)
 
         # Gather data
-        mail_template = loader.get_template('mail_templates/%s.txt' % self.mail_code)
-        self.mail_template = mail_template.render(kwargs)
+        mail_template = loader.get_template('mail_templates/%s.html' % self.mail_code)
+        mail_template = mail_template.render(kwargs)
+        # self.doc = html.fromstring(mail_template)
+        # self.doc2 = self.doc.text_content()
+        # print(self.doc2)
+
         json_location = '%s/mails/templates/mail_templates/%s.json' % (settings.BASE_DIR,
                                                                        self.mail_code)
         self.mail_data = json.loads(open(json_location).read())
@@ -43,14 +50,14 @@ class EmailTemplateForm(forms.Form):
             self.fields['extra_recipient'].required = True
 
         # Set the data as initials
-        self.fields['text'].initial = self.mail_template
+        self.fields['text'].initial = mail_template
         self.fields['subject'].initial = self.mail_data['subject']
 
     def save_data(self):
         # Get text and html
-        message = self.cleaned_data['text']
-        html_template = loader.get_template('email/general.html')
-        html_message = html_template.render({'text': message})
+        html_message = self.cleaned_data['text']
+        handler = HTML2Text()
+        message = handler.handle(html_message)
 
         # Get recipients list. Try to send through BCC to prevent privacy issues!
         bcc_list = []
diff --git a/mails/templates/mail_templates/partners_followup_mail.txt b/mails/templates/mail_templates/partners_followup_mail.html
similarity index 97%
rename from mails/templates/mail_templates/partners_followup_mail.txt
rename to mails/templates/mail_templates/partners_followup_mail.html
index 136e2d335..9ad27399d 100644
--- a/mails/templates/mail_templates/partners_followup_mail.txt
+++ b/mails/templates/mail_templates/partners_followup_mail.html
@@ -15,3 +15,5 @@ If you not the right person in your organization to contact about this topic, pl
 On behalf of the SciPost Foundation,
 Prof. dr Jean-Sébastien Caux
 J.S.Caux@uva.nl
+
+{% include 'email/_footer.html' %}
diff --git a/mails/templates/mail_templates/partners_initial_mail.html b/mails/templates/mail_templates/partners_initial_mail.html
new file mode 100644
index 000000000..5cbd3b3cc
--- /dev/null
+++ b/mails/templates/mail_templates/partners_initial_mail.html
@@ -0,0 +1,51 @@
+<p>
+    Dear {{ contact.get_title_display }} {{ contact.last_name }},
+</p>
+
+<p>
+    You might by now have heard of SciPost, a recently-launched initiative aiming to bring disruptive change to current academic publishing practices.
+</p>
+
+<p>
+    In summary, SciPost is a publication portal managed by professional scientists, offering (among others) high-quality Open Access journals with innovative forms of refereeing, and a means of commenting on all existing literature. SciPost is established as a not-for-profit foundation devoted to serving the interests of the international scientific community.
+    The site is anchored at <a href="https://scipost.org">SciPost.org</a>. Many further details about SciPost, its principles, ideals and implementation can be found at <a href="https://scipost.org/about">the about page</a> and <a href="https://scipost.org/FAQ">our FAQ</a>.
+</p>
+
+<p>
+    Crucially, as explained on our <a href="https://scipost.org/partners">Partners page</a>, SciPost follows a completely different funding model than traditional publishers, and provides a cost-slashing alternative to existing platforms. SciPost charges neither subscription fees, nor article processing charges; its activities are instead to be collectively financed through a Supporting Partners Board, formed by a worldwide consortium of institutions and organizations which directly or indirectly benefit from SciPost’s activities.
+</p>
+
+<p>
+    Support takes the form of a small financial commitment, collectively pooled to enable SciPost to perform all its publication-related activities, maintain its online portal and implement its long-term development plan.
+</p>
+
+<p>
+    In <a href="https://scipost.org/static/scipost/SPB/SciPost_Supporting_Partner_Agreement.pdf">the agreement template</a>, you will find many more specific details about our operations, requirements and funding strategy. I would greatly appreciate if you took a few minutes to read through this document.
+</p>
+
+<p>
+    It would be a privilege to welcome you as members of our Supporting Partners Board. I am hereby contacting you to enquire whether your institution would consider joining. Your support at this time is crucially required to make our initiative sustainable, and to help make it possible for the community to reap all the benefits deriving form its viable implementation.
+</p>
+
+<p>
+    I will be happy to provide any required further details. If you are interested, you can simply get in touch via this address (partners@scipost.org). I sincerely hope that SciPost will be able to count on your support.
+</p>
+
+<p>
+    On behalf of the SciPost Foundation,<br><br>
+    Prof. dr Jean-Sébastien Caux<br>
+    J.S.Caux@uva.nl<br>
+    http://jscaux.org<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>
+
+{% include 'email/_footer.html' %}
diff --git a/mails/templates/mail_templates/partners_initial_mail.txt b/mails/templates/mail_templates/partners_initial_mail.txt
deleted file mode 100644
index 739744c47..000000000
--- a/mails/templates/mail_templates/partners_initial_mail.txt
+++ /dev/null
@@ -1,31 +0,0 @@
-Dear {{ contact.get_title_display }} {{ contact.last_name }},
-
-You might by now have heard of SciPost, a recently-launched initiative aiming to bring disruptive change to current academic publishing practices.
-
-In summary, SciPost is a publication portal managed by professional scientists, offering (among others) high-quality Open Access journals with innovative forms of refereeing, and a means of commenting on all existing literature. SciPost is established as a not-for-profit foundation devoted to serving the interests of the international scientific community.
-The site is anchored at https://scipost.org. Many further details about SciPost, its principles, ideals and implementation can be found at https://scipost.org/about and https://scipost.org/FAQ.
-
-Crucially, as explained on our Partners page at https://scipost.org/partners, SciPost follows a completely different funding model than traditional publishers, and provides a cost-slashing alternative to existing platforms. SciPost charges neither subscription fees, nor article processing charges; its activities are instead to be collectively financed through a Supporting Partners Board, formed by a worldwide consortium of institutions and organizations which directly or indirectly benefit from SciPost’s activities.
-
-Support takes the form of a small financial commitment, collectively pooled to enable SciPost to perform all its publication-related activities, maintain its online portal and implement its long-term development plan.
-
-In the agreement template, which you can find online at https://scipost.org/static/scipost/SPB/SciPost_Supporting_Partner_Agreement.pdf, you will find many more specific details about our operations, requirements and funding strategy. I would greatly appreciate if you took a few minutes to read through this document.
-
-It would be a privilege to welcome you as members of our Supporting Partners Board. I am hereby contacting you to enquire whether your institution would consider joining. Your support at this time is crucially required to make our initiative sustainable, and to help make it possible for the community to reap all the benefits deriving form its viable implementation.
-
-I will be happy to provide any required further details. If you are interested, you can simply get in touch via this address (partners@scipost.org). I sincerely hope that SciPost will be able to count on your support.
-
-On behalf of the SciPost Foundation,
-Prof. dr Jean-Sébastien Caux
-J.S.Caux@uva.nl
-http://jscaux.org
----------------------------------------------
-Institute for Theoretical Physics
-University of Amsterdam
-Science Park 904
-1098 XH  Amsterdam
-The Netherlands
----------------------------------------------
-tel.: +31 (0)20 5255775
-fax: +31 (0)20 5255778
----------------------------------------------
diff --git a/mails/templates/mail_templates/production_send_proofs.html b/mails/templates/mail_templates/production_send_proofs.html
new file mode 100644
index 000000000..bfd87c4e2
--- /dev/null
+++ b/mails/templates/mail_templates/production_send_proofs.html
@@ -0,0 +1,20 @@
+<p>
+    Dear {% for author in proofs.stream.submission.authors.all %}{{ author.get_title_display }} {{ author.user.last_name }}{% if not forloop.last %}, {% elif proofs.stream.submission.authors.count > 1 %} and {% endif %}{% endfor %},
+</p>
+
+<p>
+    The SciPost production team has finished the proofs of your manuscript (version {{ proofs.version }}). You can find the proofs on your <a href="https://scipost.org{{ proofs.stream.submission.get_absolute_url }}">submission's page</a>.
+</p>
+
+<p>
+    Please review the proofs and let us know whether you accept the proofs for publication using the form on the submission page.
+</p>
+
+<p>
+    Sincerely,<br>
+
+    {{ proofs.stream.supervisor.user.first_name }} {{ proofs.stream.supervisor.user.last_name }},<br>
+    SciPost Production
+</p>
+
+{% include 'email/_footer.html' %}
diff --git a/mails/templates/mail_templates/production_send_proofs.txt b/mails/templates/mail_templates/production_send_proofs.txt
deleted file mode 100644
index d6cbda22c..000000000
--- a/mails/templates/mail_templates/production_send_proofs.txt
+++ /dev/null
@@ -1,11 +0,0 @@
-Dear {% for author in proofs.stream.submission.authors.all %}{{ author.get_title_display }} {{ author.user.last_name }}{% if not forloop.last %}, {% elif proofs.stream.submission.authors.count > 1 %} and {% endif %}{% endfor %},
-
-The SciPost production team has finished the proofs of your manuscript (version {{ proofs.version }}). You can find the proofs on your submission's page (see https://scipost.org{{ proofs.stream.submission.get_absolute_url }}).
-
-Please review the proofs and let us know whether you accept the proofs for publication using the form on the submission page.
-
-
-Sincerely,
-
-{{ proofs.stream.supervisor.user.first_name }} {{ proofs.stream.supervisor.user.last_name }}
-SciPost Production
diff --git a/mails/templates/mail_templates/submissions_referee_invite.html b/mails/templates/mail_templates/submissions_referee_invite.html
new file mode 100644
index 000000000..2c2bd8059
--- /dev/null
+++ b/mails/templates/mail_templates/submissions_referee_invite.html
@@ -0,0 +1,29 @@
+<p>
+    Dear {{invitation.referee.get_title_display}} {{invitation.referee.user.last_name}},
+</p>
+
+<p>
+    We have received a Submission to SciPost which, in view of your expertise and on behalf of the Editor-in-charge {{invitation.submission.editor_in_charge.get_title_display}} {{invitation.submission.editor_in_charge.user.last_name}}, we would like to invite you to referee:
+    <br>
+    <a href="https://scipost.org{{invitation.submission.get_absolute_url}}">{{invitation.submission.title}} by {{invitation.submission.author_list}}</a>.
+</p>
+
+<p>
+    Please <a href="https://scipost.org/submissions/accept_or_decline_ref_invitations">accept or decline</a> (login required) this invitation as soon as possible (ideally within the next 2 days).
+</p>
+
+<p>
+    If you accept, your report can be submitted by simply clicking on the "Contribute a Report" link on <a href="https://scipost.org{{invitation.submission.get_absolute_url}}">the Submission Page</a> before the reporting deadline (currently set at {{invitation.submission.reporting_deadline|date:'d-m-Y'}}; your report will be automatically recognized as an invited report).
+</p>
+
+<p>
+    You might want to make sure you are familiar with our refereeing <a href="https://scipost.org/journals/journals_terms_and_conditions">code of conduct</a> and with <a href="https://scipost.org/submissions/sub_and_ref_procedure">the refereeing procedure</a>.
+</p>
+
+<p>
+    We would be extremely grateful for your contribution, and thank you in advance for your consideration.
+    <br>
+    The SciPost Team.
+</p>
+
+{% include 'email/_footer.html' %}
diff --git a/mails/templates/mail_templates/submissions_referee_invite.txt b/mails/templates/mail_templates/submissions_referee_invite.txt
deleted file mode 100644
index c831966f4..000000000
--- a/mails/templates/mail_templates/submissions_referee_invite.txt
+++ /dev/null
@@ -1,15 +0,0 @@
-Dear {{invitation.referee.get_title_display}} {{invitation.referee.user.last_name}},
-
-We have received a Submission to SciPost which, in view of your expertise and on behalf of the Editor-in-charge {{invitation.submission.editor_in_charge.get_title_display}} {{invitation.submission.editor_in_charge.user.last_name}}, we would like to invite you to referee:
-
-{{invitation.submission.title}} by {{invitation.submission.author_list}} (see https://scipost.org{{invitation.submission.get_absolute_url}}).
-
-Please visit https://scipost.org/submissions/accept_or_decline_ref_invitations (login required) as soon as possible (ideally within the next 2 days) in order to accept or decline this invitation.
-
-If you accept, your report can be submitted by simply clicking on the "Contribute a Report" link at https://scipost.org{{invitation.submission.get_absolute_url}} before the reporting deadline (currently set at {{invitation.submission.reporting_deadline|date:'d-m-Y'}}; your report will be automatically recognized as an invited report).
-
-You might want to make sure you are familiar with our refereeing code of conduct https://scipost.org/journals/journals_terms_and_conditions and with the refereeing procedure https://scipost.org/submissions/sub_and_ref_procedure.
-
-We would be extremely grateful for your contribution, and thank you in advance for your consideration.
-
-The SciPost Team.
diff --git a/mails/templates/mails/mail_form.html b/mails/templates/mails/mail_form.html
index b2837b64a..fbb170d4d 100644
--- a/mails/templates/mails/mail_form.html
+++ b/mails/templates/mails/mail_form.html
@@ -11,7 +11,7 @@
 
     <form enctype="multipart/form-data" method="post">
         {% csrf_token %}
-        {{form|bootstrap}}
+        {{ form|bootstrap }}
         <div class="form-group row">
             <div class="offset-md-2 col-md-10">
                 <input class="btn btn-secondary mr-2" type="reset" value="Reset to default">
@@ -20,3 +20,9 @@
         </div>
     </form>
 {% endblock content %}
+
+
+{% block footer_script %}
+    {{ block.super }}
+    {{ form.media }}
+{% endblock footer_script %}
diff --git a/mails/widgets.py b/mails/widgets.py
new file mode 100644
index 000000000..d5f1aff30
--- /dev/null
+++ b/mails/widgets.py
@@ -0,0 +1,72 @@
+import json
+
+from django.core.urlresolvers import reverse, NoReverseMatch
+from django.forms import widgets, Media
+from django.utils.safestring import mark_safe
+# from django.conf import settings
+
+# from . import PLUGINS, PLUGINS_WITH_CSS
+
+
+class SummernoteEditor(widgets.Textarea):
+    def __init__(self, *args, **kwargs):
+        self.options = kwargs.pop('options', {})
+        self.include_jquery = False
+        super().__init__(*args, **kwargs)
+
+    def get_options(self):
+
+        default_options = {
+            'inlineMode': False,
+            'toolbar': [
+                ['style', ['bold', 'italic', 'underline', 'clear']],
+                ['font', ['strikethrough', 'superscript', 'subscript']],
+                ['fontsize', ['fontsize']],
+                ['para', ['ul', 'ol', 'paragraph']],
+            ],
+        }
+
+        try:
+            file_upload_url = reverse('froala_editor_file_upload')
+            default_options['fileUploadURL'] = file_upload_url
+            default_options.update([
+                ('fileUploadParams', {'csrfmiddlewaretoken': 'csrftokenplaceholder'})])
+        except NoReverseMatch:
+            default_options['fileUpload'] = False
+
+        # settings_options = getattr(settings, 'FROALA_EDITOR_OPTIONS', {})
+        # options = dict(default_options.items() + settings_options.items() + self.options.items())
+        options = dict(default_options.items()).copy()
+        # options.update(settings_options.items())
+        options.update(self.options.items())
+
+        json_options = json.dumps(options)
+        json_options = json_options.replace('"csrftokenplaceholder"', 'getCookie("csrftoken")')
+        return json_options
+
+    def render(self, name, value, attrs=None):
+        html = super().render(name, value, attrs)
+        el_id = self.build_attrs(attrs).get('id')
+        html += self.trigger_summernote(el_id, self.get_options())
+        return mark_safe(html)
+
+    def trigger_summernote(self, el_id, options):
+        str = """
+        <script>
+            $(function(){
+                $('#%s').summernote(%s)
+            });
+        </script>""" % (el_id, options)
+        return str
+
+    @property
+    def media(self):
+        css = {
+            'all': ('//cdnjs.cloudflare.com/ajax/libs/summernote/0.8.8/summernote-bs4.css',)
+        }
+        js = ('//cdnjs.cloudflare.com/ajax/libs/summernote/0.8.8/summernote-bs4.js',)
+
+        if self.include_jquery:
+            js = ('//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js',) + js
+
+        return Media(css=css, js=js)
diff --git a/requirements.txt b/requirements.txt
index c86df0eaf..0cb1a60d9 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -48,7 +48,6 @@ mailchimp3==2.0.15
 python-dateutil==2.6.0  # Doesn't Django have this functionality built-in?  -- JdW
 Pillow==3.4.2  # Latest version is v4.2.1; need to know about usage before upgrade. -- JdW
 
-
 # Possibly dead (most probably not used anymore and possibly not up-to-date packages)  -- JdW (August 15th, 2017)
 imagesize==0.7.1
 Jinja2==2.8
-- 
GitLab