Login

View decorator providing confirmation dialog

Author:
rudyryk
Posted:
February 12, 2010
Language:
Python
Version:
1.1
Tags:
views decorators confirmation
Score:
1 (after 1 ratings)

Decorator for views that need confirmation page. For example, delete object view. Decorated view renders confirmation page defined by template 'template_name'. If request.POST contains confirmation key, defined by 'key' parameter, then original view is executed.

Context for confirmation page is created by function 'context_creator', which accepts same arguments as decorated view.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-

try:
    from functools import wraps
except ImportError:
    from django.utils.functional import wraps
    
from django.shortcuts import render_to_response
from django.template import RequestContext
    
def confirm_required(template_name, context_creator, key='__confirm__'):
    """
    Decorator for views that need confirmation page. For example, delete
    object view. Decorated view renders confirmation page defined by template
    'template_name'. If request.POST contains confirmation key, defined
    by 'key' parameter, then original view is executed.
    
    Context for confirmation page is created by function 'context_creator',
    which accepts same arguments as decorated view.
    
    Example
    -------
    
        def remove_file_context(request, id):
            file = get_object_or_404(Attachment, id=id)
            return RequestContext(request, {'file': file})
    
        @confirm_required('remove_file_confirm.html', remove_file_context)
        def remove_file_view(request, id):
            file = get_object_or_404(Attachment, id=id)
            file.delete()
            next_url = request.GET.get('next', '/')
            return HttpResponseRedirect(next_url)
            
    Example of HTML template
    ------------------------
    
        <h1>Remove file {{ file }}?</h1>

        <form method="POST" action="">
            <input type="hidden" name="__confirm__" value="1" />
            <input type="submit" value="delete"/> <a href="{{ file.get_absolute_url }}">cancel</a>
        </form>
    
    """
    def decorator(func):
        def inner(request, *args, **kwargs):
            if request.POST.has_key(key):
                return func(request, *args, **kwargs)
            else:
                context = context_creator and context_creator(request, *args, **kwargs) \
                    or RequestContext(request)
                return render_to_response(template_name, context)
        return wraps(func)(inner)
    return decorator

More like this

  1. API_magic API view decorator by jobs@flowgram.com 6 years, 8 months ago
  2. View Redirect Decorators by bryanpieper 4 years, 7 months ago
  3. is_staff decorator by munhitsu 6 years, 6 months ago
  4. New view decorator to only cache pages for anonymous users by vaughnkoch 4 years, 4 months ago
  5. Render Decorator by dahool 5 years, 5 months ago

Comments

Please login first before commenting.