Changes between Initial Version and Version 1 of CookBookShortcutsPageDecorator


Ignore:
Timestamp:
01/11/2006 02:28:48 PM (9 years ago)
Author:
brantley (deadwisdom@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CookBookShortcutsPageDecorator

    v1 v1  
     1This decorator makes views even easier:
     2
     3'''Code:'''
     4{{{
     5def page(template=None, context=None, **decorator_args):
     6    def _wrapper(fn):
     7        def _innerWrapper(request, *args, **kw):
     8            context_dict = decorator_args.copy()
     9            g = fn(request, *args, **kw)
     10            if not hasattr(g, 'next'):  #Is this a generator?  Otherwise make her a tuple!
     11                g = (g,)
     12            for i in g:
     13                if isinstance(i, httpwrappers.HttpResponse):
     14                    return i
     15                if type(i) == type(()):
     16                    context_dict[i[0]] = i[1]
     17                else:
     18                    context_dict.update(i)
     19            template_name = context_dict.get("template",  template)
     20            context_instance = context_dict.get("context", context)
     21            return render_to_response(template_name, context_dict, context_instance)
     22           
     23        return _innerWrapper
     24    return _wrapper
     25}}}
     26
     27'''Usage:'''
     28
     29{{{
     30@page("polls/index")    #The decorator function takes the template to be used.
     31def index(request):
     32    latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5)
     33    yield {'latest_poll_list': latest_poll_list}
     34    yield {'title': 'Latest Poll Listing'}  # We can yield at multiple places in the function.
     35}}}
     36
     37Or one could yield the locals():
     38
     39{{{
     40@page("polls/index", title='Latest Poll Listing')  #Extra keyword arguments are sent in the context.
     41def index(request):
     42    latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5)
     43    yield locals()   #This yeilds all the local variables, which is 'latest_poll_list', and 'request'
     44}}}
     45
     46You can set a template mid-stream with:
     47{{{
     48@page() #We don't say what template is needed yet.
     49def index(request):
     50    latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5)
     51    if (latest_poll_list):
     52        template = 'polls/index'
     53    else:
     54        template = 'polls/nopolls'  # We could also send 'context' which changes what context we use.
     55    yield locals()
     56}}}
     57
     58You can yield a response to have it immediately returned:
     59{{{
     60@page('polls/index')
     61def index(request):
     62    latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5)
     63    if (not latest_poll_list):
     64        yield httpwrappers.HttpResponseRedirect("/home/")  #Since we don't have any latest polls, redirect to home.
     65        return #Don't yield anything else, we're done here.
     66    yield locals()
     67}}}
Back to Top