From: Stephen Finucane Date: Mon, 21 Mar 2016 16:31:11 +0000 (+0000) Subject: views: Use messages framework in 'patch' X-Git-Tag: v2.0.0-rc1~396 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e8750a4741401a5574c2c980783cecc743f94c4a;p=thirdparty%2Fpatchwork.git views: Use messages framework in 'patch' 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 Tested-by: Andy Doan --- diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py index aa0e9cc2..739ca9f7 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 0e9dd0f5..8521edf8 100644 --- a/templates/base.html +++ b/templates/base.html @@ -101,6 +101,8 @@ {% if messages %}
{% for message in messages %} + {# TODO(stephenfin): Make use of message.tags when completely #} + {# converted to django.contrib.messages #}
{{ message }}
{% endfor %}