]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
views: Use messages framework in 'patch'
authorStephen Finucane <stephen.finucane@intel.com>
Mon, 21 Mar 2016 16:31:11 +0000 (16:31 +0000)
committerStephen Finucane <stephen.finucane@intel.com>
Fri, 25 Mar 2016 11:25:10 +0000 (11:25 +0000)
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@intel.com>
Tested-by: Andy Doan <andy.doan@linaro.org>
patchwork/views/patch.py
templates/base.html

index aa0e9cc28a56e5bdd420dc3d6ae4da3d496ee0ce..739ca9f75a2517c524fc1068080d1f2d99801db6 100644 (file)
 
 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):
index 0e9dd0f59a65b022c1a6d7624a01fae0e56809b0..8521edf81c6ced0857878e52d0a4a240f68dd961 100644 (file)
 {% 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>