agdj/utils.py
author Adam Gomaa <code@adam.gomaa.us>
Sun Feb 01 01:35:02 2009 -0500
changeset 416 5a586a8c2f8f
parent 415 5e48e2725b18
child 451 88b49087ec1a
permissions -rw-r--r--
Switch to date-based URLs everywhere except the feed URLs of old posts, so I don't spam everyone's feedreader.
     1 from functools import wraps
     2 
     3 from django.core.cache import cache
     4 from django.shortcuts import render_to_response
     5 from django.template import RequestContext
     6 
     7 
     8 def use_template(template_name, request_context=True):
     9     """A view decorator that wraps render_to_response and uses RequestContext by default"""
    10     from functools import wraps
    11     from django.template import RequestContext
    12     from django.shortcuts import render_to_response
    13 
    14     def _decorator(func):
    15         @wraps(func)
    16         def _closure(request, *args, **kwargs):
    17             # Use a different variable to avoid 'referenced before
    18             # assignment'. 'template' is the kwarg used by
    19             # direct_to_template; copy its interface for consistency.
    20             actual_template = kwargs.pop("template", template_name)
    21             actual_rc = kwargs.pop("request_context", request_context)
    22             val = func(request, *args, **kwargs)
    23             if isinstance(val, dict) and template_name is not None:
    24                 if actual_rc:
    25                     context = RequestContext(request)
    26                     return render_to_response(actual_template, val,
    27                                               context_instance=context)
    28                 else:
    29                     return render_to_response(actual_template, val)
    30             return val
    31         return _closure
    32     return _decorator
    33 
    34 
    35 def iso8601(dt):
    36     "Format a datetime object as ISO8601"
    37 
    38     return dt.strftime("%Y-%m-%dT%H:%M:%SZ")
    39 
    40 
    41 def cached_property(func):
    42     @wraps(func)
    43     def _closure(self):
    44         try:
    45             cache_key = self.get_cache_key()
    46         except Exception:
    47             cache_key = "%s.%s.%s(%s)" % (self.__class__.__module__,
    48                                           self.__class__.__name__,
    49                                           func.__name__,
    50                                           self.pk)
    51         val = cache.get(cache_key)
    52 
    53         if val is None:
    54             val = func(self)
    55             cache.set(cache_key, val)
    56 
    57         return val
    58     return property(_closure)
    59 
    60 
    61 def dictproperty(method):
    62     """Turn dictionary or attribute access into a function call"""
    63 
    64     class _Object(object):
    65         def __init__(self, obj):
    66             self.obj = obj
    67         def __getattr__(self, attr):
    68             return method(self.obj, attr)
    69         __getitem__ = __getattr__
    70     _Object.__name__ = method.__name__
    71 
    72     return property(_Object)