[PATCH 11/15] views: Use messages framework in 'patch'

Stephen Finucane stephen.finucane at intel.com
Fri Mar 25 04:52:56 AEDT 2016


Django provides the messages framework as part of the core library.
This framework allows for "toast"-style notifications to the user,
for things like post-form processing notifcations. At the moment
patchwork provides this functionality using custom code. However, this
code is not well tested and, like any code, incurs some degree of
maintenance overhead. It would be easier to use the "batteries" that
Django provides so begin doing just that.

This change only covers one of two places in which this custom
messages framework is currently used. By extension, it also covers one
of the two places in which 'render_to_response' is still used. This
"other place" will be addressed in a follow-up commit.

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/views/patch.py | 28 +++++++++++++++++-----------
 templates/base.html      |  2 ++
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py
index aa0e9cc..739ca9f 100644
--- a/patchwork/views/patch.py
+++ b/patchwork/views/patch.py
@@ -19,22 +19,24 @@
 
 from __future__ import absolute_import
 
+from django.contrib import messages
 from django.http import HttpResponse, HttpResponseForbidden
-from django.shortcuts import render_to_response, get_object_or_404
+from django.shortcuts import render, render_to_response, get_object_or_404
 from django.utils import six
 
 from patchwork.forms import PatchForm, CreateBundleForm
 from patchwork.models import Patch, Project, Bundle
-from patchwork.requestcontext import PatchworkRequestContext
 from patchwork.views import generic_list, patch_to_mbox
 
 
 def patch(request, patch_id):
-    context = PatchworkRequestContext(request)
     patch = get_object_or_404(Patch, id=patch_id)
-    context.project = patch.project
     editable = patch.is_editable(request.user)
 
+    context = {
+        'project': patch.project
+    }
+
     form = None
     createbundleform = None
 
@@ -57,18 +59,19 @@ def patch(request, patch_id):
                 bundle.append_patch(patch)
                 bundle.save()
                 createbundleform = CreateBundleForm()
-                context.add_message('Bundle %s created' % bundle.name)
-
+                messages.success(request, 'Bundle %s created' % bundle.name)
         elif action == 'addtobundle':
             bundle = get_object_or_404(
                 Bundle, id=request.POST.get('bundle_id'))
             try:
                 bundle.append_patch(patch)
                 bundle.save()
-                context.add_message('Patch added to bundle "%s"' % bundle.name)
+                messages.success(request,
+                                 'Patch added to bundle "%s"' % bundle.name)
             except Exception as ex:
-                context.add_message("Couldn't add patch '%s' to bundle %s: %s"
-                                    % (patch.name, bundle.name, ex.message))
+                messages.error(request,
+                               "Couldn't add patch '%s' to bundle %s: %s"
+                               % (patch.name, bundle.name, ex.message))
 
         # all other actions require edit privs
         elif not editable:
@@ -78,14 +81,17 @@ def patch(request, patch_id):
             form = PatchForm(data=request.POST, instance=patch)
             if form.is_valid():
                 form.save()
-                context.add_message('Patch updated')
+                messages.success(request, 'Patch updated')
+
+    if request.user.is_authenticated():
+        context['bundles'] = Bundle.objects.filter(owner=request.user)
 
     context['patch'] = patch
     context['patchform'] = form
     context['createbundleform'] = createbundleform
     context['project'] = patch.project
 
-    return render_to_response('patchwork/patch.html', context)
+    return render(request, 'patchwork/patch.html', context)
 
 
 def content(request, patch_id):
diff --git a/templates/base.html b/templates/base.html
index 0e9dd0f..8521edf 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -101,6 +101,8 @@
 {% if messages %}
   <div id="messages">
   {% for message in messages %}
+  {# TODO(stephenfin): Make use of message.tags when completely #}
+  {# converted to django.contrib.messages #}
    <div class="message">{{ message }}</div>
   {% endfor %}
   </div>
-- 
2.0.0



More information about the Patchwork mailing list