From 72ad07f36b0841c07d9929ad7ad982640aa94768 Mon Sep 17 00:00:00 2001
From: Geert Kapteijns <ghkapteijns@gmail.com>
Date: Mon, 19 Dec 2016 21:09:08 +0100
Subject: [PATCH] Use django.contrib.messages to handle acknowledgement

The acknowledgement message is no longer hardcoded in
theses/views.py, but resides in the top-level strings module.
---
 SciPost_v1/settings.py                  | 15 ++++++++++++---
 scipost/templates/scipost/base.html     |  1 +
 scipost/templates/scipost/messages.html |  8 ++++++++
 strings/__init__.py                     |  1 +
 theses/test_views.py                    |  4 ----
 theses/views.py                         | 14 ++++++--------
 6 files changed, 28 insertions(+), 15 deletions(-)
 create mode 100644 scipost/templates/scipost/messages.html
 create mode 100644 strings/__init__.py

diff --git a/SciPost_v1/settings.py b/SciPost_v1/settings.py
index e6c50a4a8..ad70a7650 100644
--- a/SciPost_v1/settings.py
+++ b/SciPost_v1/settings.py
@@ -12,12 +12,14 @@ https://docs.djangoproject.com/en/1.8/ref/settings/
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 import os
-
 import json
 
+from django.utils.translation import ugettext_lazy as _
+
+
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
-host_settings_path = os.path.join(os.path.dirname(BASE_DIR),"scipost-host-settings.json")
+host_settings_path = os.path.join(os.path.dirname(BASE_DIR), "scipost-host-settings.json")
 host_settings = json.load(open(host_settings_path))
 
 # Quick-start development settings - unsuitable for production
@@ -94,6 +96,7 @@ MATHJAX_CONFIG_DATA = {
 
 MIDDLEWARE_CLASSES = (
     'django.contrib.sessions.middleware.SessionMiddleware',
+    'django.middleware.locale.LocaleMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
@@ -114,6 +117,7 @@ TEMPLATES = [
             'context_processors': [
                 'django.template.context_processors.debug',
                 'django.template.context_processors.request',
+                'django.template.context_processors.i18n',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
                 'scipost.context_processors.searchform',
@@ -144,7 +148,12 @@ DATABASES = {
 # https://docs.djangoproject.com/en/1.8/topics/i18n/
 
 LANGUAGE_CODE = 'en-us'
-
+LANGUAGES = (
+    ('en', _('English')),
+)
+LOCALE_PATHS = (
+    os.path.join(BASE_DIR, 'locale'),
+)
 TIME_ZONE = 'CET'
 
 USE_I18N = True
diff --git a/scipost/templates/scipost/base.html b/scipost/templates/scipost/base.html
index b8c940011..8c7f4c2b5 100644
--- a/scipost/templates/scipost/base.html
+++ b/scipost/templates/scipost/base.html
@@ -26,6 +26,7 @@
   <body>
     {% include 'scipost/header.html' %}
     {% include 'scipost/navbar.html' %}
+    {% include 'scipost/messages.html' %}
 
     {% block bodysup %}
     {% endblock bodysup %}
diff --git a/scipost/templates/scipost/messages.html b/scipost/templates/scipost/messages.html
new file mode 100644
index 000000000..ac8c6dff8
--- /dev/null
+++ b/scipost/templates/scipost/messages.html
@@ -0,0 +1,8 @@
+{% for message in messages %}
+  <div class="alert {{ message.tags }} alert-dismissible" role="alert">
+    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
+      <span aria-hidden="true">&times;</span>
+    </button>
+    {{ message }}
+  </div>
+{% endfor %}
diff --git a/strings/__init__.py b/strings/__init__.py
new file mode 100644
index 000000000..82d957c9d
--- /dev/null
+++ b/strings/__init__.py
@@ -0,0 +1 @@
+acknowledge_request_thesis_link = "Thank you for your request for a Thesis Link. Your request will soon be handled by an editor"
diff --git a/theses/test_views.py b/theses/test_views.py
index 7e70a6dd4..a3283c731 100644
--- a/theses/test_views.py
+++ b/theses/test_views.py
@@ -36,7 +36,3 @@ class TestRequestThesisLink(TestCase):
         request.user = UserFactory()
         response = RequestThesisLink.as_view()(request)
         self.assertEqual(response.status_code, 200)
-
-    def test_redirects_to_acknowledgement_page(self):
-        response = self.client.post(reverse('theses:request_thesislink'), {}, follow=True)
-        self.assertRedirects(response, reverse('scipost:acknowledgement'))
diff --git a/theses/views.py b/theses/views.py
index 7e738e39d..7173078c5 100644
--- a/theses/views.py
+++ b/theses/views.py
@@ -5,8 +5,9 @@ from django.shortcuts import get_object_or_404, render
 from django.contrib.auth import authenticate, login, logout
 from django.contrib.auth.decorators import login_required, permission_required
 from django.contrib.auth.models import User
+from django.contrib import messages
 from django.core.mail import EmailMessage
-from django.core.urlresolvers import reverse
+from django.core.urlresolvers import reverse, reverse_lazy
 from django.http import HttpResponse, HttpResponseRedirect
 from django.views.decorators.csrf import csrf_protect
 from django.db.models import Avg
@@ -33,15 +34,12 @@ title_dict = dict(TITLE_CHOICES)  # Convert titles for use in emails
 class RequestThesisLink(CreateView):
     form_class = RequestThesisLinkForm
     template_name = 'theses/request_thesislink.html'
-    success_url = ''
+    success_url = reverse_lazy('scipost:personal_page')
 
     def form_valid(self, form):
-        context = {'ack_header': 'Thank you for your request for a Thesis Link',
-                   'ack_message': 'Your request will soon be handled by an Editor. ',
-                   'followup_message': 'Return to your ',
-                   'followup_link': reverse('scipost:personal_page'),
-                   'followup_link_label': 'personal page'}
-        return render(self.request, 'scipost/acknowledgement.html', context)
+        messages.add_message(self.request, messages.SUCCESS,
+                             strings.acknowledge_request_thesis_link)
+        return super(RequestThesisLink, self).form_valid(form)
 
 
 @permission_required('scipost.can_vet_thesislink_requests', raise_exception=True)
-- 
GitLab