Serotonin Storm

source>flatblocks>views.py
 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponseForbidden, HttpResponse
from django.utils.translation import ugettext as _

from flatblocks.models import FlatBlock
from flatblocks.forms import FlatBlockForm


def edit(request, pk, modelform_class=FlatBlockForm, permission_check=None,
        template_name='flatblocks/edit.html', success_url=None):
    """
    This view provides a simple editor implementation for flatblocks.

    There are two customization hooks. First of all you can specify your own
    ModelForm class by passing it to the view using the ``modelform_class``
    keyword-argument.

    The other entry point helps you check permissions: Pass a simple function
    via the ``permission_check`` keyword-argument in order to check 
    permissions on the flatblock-level::
        
        def my_perm_check(request, flatblock):
            return request.user.is_staff

        # ...

        urlpatterns('flatblocks.views',
            url('flatblocks/(?P<pk>\d+)/edit/$', 'edit',
                kwargs={'permission_check': my_perm_check}),
        )

    The contract here is pretty simple: If the function returns False, the
    view will return HttpResponseForbidden. Otherwise it will pass.  So if you
    want to do some fancy redirects if the permissions are wrong, return your
    own HttpResponse-object/-subclass. 

    If everything is alright with the permissions, simply return True.
    """
    flatblock = get_object_or_404(FlatBlock, pk=pk)
    if permission_check is not None:
        permcheck_result = permission_check(request, flatblock)
        if permcheck_result is False:
            return HttpResponseForbidden(_('You are not allowed to edit this flatblock'))
        if isinstance(permcheck_result, HttpResponse):
            return permcheck_result

    session_key = 'flatblock.origin.%d' % (int(pk), )
    if request.method == 'POST':
        origin = request.session.get(session_key, 
                request.META.get('HTTP_REFERER', '/'))
        form = modelform_class(request.POST, instance=flatblock)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.slug = flatblock.slug
            instance.save()
            del request.session[session_key]
            redirect_to = success_url and success_url or origin
            return HttpResponseRedirect(redirect_to)
    else:
        origin = request.META.get('HTTP_REFERER', '/')
        # Don't set origin to this view's url no matter what
        origin = origin == request.get_full_path() and request.session.get(session_key, '/') or origin
        form = modelform_class(instance=flatblock)
        request.session[session_key] = origin
    return render_to_response(template_name, {
        'form': form,
        'origin': origin,
        'flatblock': flatblock,
        }, context_instance=RequestContext(request))