1
"""Decorators for zinnia.views"""
2
from django.template import RequestContext
3
from django.contrib.auth.views import login
4
from django.shortcuts import redirect
5
from django.shortcuts import get_object_or_404
6
from django.shortcuts import render_to_response
7
from django.views.decorators.csrf import csrf_protect
8
from django.views.decorators.cache import never_cache
10
from zinnia.models import Entry
13
def update_queryset(view, queryset,
14
queryset_parameter='queryset'):
15
"""Decorator around views based on a queryset
16
passed in parameter, who will force the update
17
of the queryset before executing the view.
18
Related to issue http://code.djangoproject.com/ticket/8378"""
20
def wrap(*args, **kwargs):
21
"""Regenerate the queryset before passing it to the view."""
22
kwargs[queryset_parameter] = queryset()
23
return view(*args, **kwargs)
30
def password(request, entry):
31
"""Displays the password form and handle validation
32
by setting the valid password in a cookie."""
34
if request.method == 'POST':
35
if request.POST.get('password') == entry.password:
37
'zinnia_entry_%s_password' % entry.pk] = entry.password
38
return redirect(entry)
40
return render_to_response('zinnia/password.html', {'error': error},
41
context_instance=RequestContext(request))
44
def protect_entry(view):
45
"""Decorator performing a security check if needed
46
around the generic.date_based.entry_detail view
47
and specify the template used to render the entry"""
50
"""Do security check and retrieve the template"""
52
entry = get_object_or_404(Entry, slug=kw['slug'],
53
creation_date__year=kw['year'],
54
creation_date__month=kw['month'],
55
creation_date__day=kw['day'])
57
if entry.login_required and not request.user.is_authenticated():
58
return login(request, 'zinnia/login.html')
59
if entry.password and entry.password != \
60
request.session.get('zinnia_entry_%s_password' % entry.pk):
61
return password(request, entry)
62
kw['template_name'] = entry.template
63
return view(*ka, **kw)