From 194faf3c4eb513ace58ba9c0dfeaa6e795deeaf0 Mon Sep 17 00:00:00 2001
From: Amir <amir@localhost.localdomain>
Date: Tue, 6 Oct 2015 14:46:00 +0300
Subject: [PATCH 2/2] Use django messages to give user feedback for email
 message request

---
 django/archives/mailarchives/templates/message.html |  7 +++++++
 django/archives/mailarchives/views.py               | 12 +++++++++---
 django/archives/settings.py                         |  9 +++++++--
 django/media/css/archives.css                       | 21 +++++++++++++++++++++
 4 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/django/archives/mailarchives/templates/message.html b/django/archives/mailarchives/templates/message.html
index ae2e6c5..0d6a1f5 100644
--- a/django/archives/mailarchives/templates/message.html
+++ b/django/archives/mailarchives/templates/message.html
@@ -32,6 +32,13 @@ $(function(){
 
 {%endblock%}
 {%block contents%}
+{% if messages %}
+   {% for message in messages %}
+    <div class="alert {% if message.tags %} {{ message.tags }} {% endif %}">
+    {{ message|safe }}
+    </div>
+    {% endfor %}
+{% endif %}
 <h2>{{msg.subject}}</h2>
 <table class="message">
  <tr>
diff --git a/django/archives/mailarchives/views.py b/django/archives/mailarchives/views.py
index 40d0e52..b07243e 100644
--- a/django/archives/mailarchives/views.py
+++ b/django/archives/mailarchives/views.py
@@ -2,6 +2,7 @@ from django.template import RequestContext
 from django.http import HttpResponse, HttpResponseForbidden, Http404
 from django.http import HttpResponsePermanentRedirect, HttpResponseNotModified, HttpResponseRedirect
 from django.shortcuts import render_to_response, get_object_or_404
+from django.contrib import messages
 from django.core.urlresolvers import reverse
 from django.utils.http import http_date, parse_http_date_safe
 from django.db import connection
@@ -425,12 +426,17 @@ def email_message(request):
 
     try:
         send_mail(settings.ARCHIVE_COPY_EMAIL_FROM, user_email,  rawtxt)
+        messages.add_message(request, messages.constants.SUCCESS,
+                                '<strong>Success!</strong> This message should be in your inbox in a few minutes.',
+                                extra_tags='alert-success')
     except:
         # log error
-        return Http404("An error has occured.")
+        messages.add_message(request, messages.constants.ERROR,
+                                "<strong>Error!</strong> Sorry, something went wrong.",
+                                extra_tags='alert-error alert-danger'
+                                )
 
-    response = render_to_response('email_message.html',
-                                  {'return_url' :  reverse(message,args=[msgid]) })
+    return HttpResponseRedirect(reverse(message,args=[msgid]))
 
     return response
 
diff --git a/django/archives/settings.py b/django/archives/settings.py
index a680c3a..ddd2896 100644
--- a/django/archives/settings.py
+++ b/django/archives/settings.py
@@ -97,9 +97,12 @@ MIDDLEWARE_CLASSES = (
 #    'django.contrib.sessions.middleware.SessionMiddleware',
 #    'django.middleware.csrf.CsrfViewMiddleware',
 #    'django.contrib.auth.middleware.AuthenticationMiddleware',
-#    'django.contrib.messages.middleware.MessageMiddleware',
+   'django.contrib.messages.middleware.MessageMiddleware',
 )
 
+TEMPLATE_CONTEXT_PROCESSORS = {
+    'django.contrib.messages.context_processors.messages',
+    }
 ROOT_URLCONF = 'archives.urls'
 
 TEMPLATE_DIRS = (
@@ -113,7 +116,7 @@ INSTALLED_APPS = (
 #    'django.contrib.contenttypes',
 #    'django.contrib.sessions',
 #    'django.contrib.sites',
-#    'django.contrib.messages',
+   'django.contrib.messages',
 #    'django.contrib.staticfiles',
     # Uncomment the next line to enable the admin:
     # 'django.contrib.admin',
@@ -123,6 +126,8 @@ INSTALLED_APPS = (
     'archives.mailqueue',
 )
 
+MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage'
+
 # A sample logging configuration. The only tangible logging
 # performed by this configuration is to send an email to
 # the site admins on every HTTP 500 error.
diff --git a/django/media/css/archives.css b/django/media/css/archives.css
index 1c0504b..17aec97 100644
--- a/django/media/css/archives.css
+++ b/django/media/css/archives.css
@@ -94,4 +94,25 @@ a.ui-selectmenu {
 
 img.attachedimage {
    max-width: 600px;
+}
+
+/* borrowed from bootstrap */
+/* http://getbootstrap.com/ */
+.alert {
+    padding: 15px;
+    margin-bottom: 20px;
+    border: 1px solid transparent;
+    border-radius: 4px;
+}
+
+.alert-success {
+    color: #3C763D;
+    background-color: #DFF0D8;
+    border-color: #D6E9C6;
+}
+
+.alert-danger {
+    color: #A94442;
+    background-color: #F2DEDE;
+    border-color: #EBCCD1;
 }
\ No newline at end of file
-- 
2.4.3

