2011-10-28 10:29:29 -07:00
|
|
|
from django.http import Http404
|
2011-12-23 18:28:16 -08:00
|
|
|
from django.template import RequestContext
|
|
|
|
from django.shortcuts import render_to_response
|
2011-10-28 10:29:29 -07:00
|
|
|
|
|
|
|
def get_argument_or_404(request, param, method='REQUEST'):
|
|
|
|
try:
|
|
|
|
return getattr(request, method)[param]
|
|
|
|
except KeyError:
|
2011-12-23 18:28:16 -08:00
|
|
|
raise Http404
|
|
|
|
|
|
|
|
def render_to(template):
|
|
|
|
"""
|
|
|
|
Decorator for Django views that sends returned dict to render_to_response function
|
|
|
|
with given template and RequestContext as context instance.
|
|
|
|
|
|
|
|
If view doesn't return dict then decorator simply returns output.
|
|
|
|
Additionally view can return two-tuple, which must contain dict as first
|
|
|
|
element and string with template name as second. This string will
|
|
|
|
override template name, given as parameter
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
- template: template name to use
|
|
|
|
"""
|
|
|
|
def renderer(func):
|
|
|
|
def wrapper(request, *args, **kw):
|
|
|
|
output = func(request, *args, **kw)
|
|
|
|
if isinstance(output, (list, tuple)):
|
|
|
|
return render_to_response(output[1], output[0], RequestContext(request))
|
|
|
|
elif isinstance(output, dict):
|
|
|
|
return render_to_response(template, output, RequestContext(request))
|
|
|
|
return output
|
|
|
|
return wrapper
|
2012-03-19 15:46:59 -07:00
|
|
|
return renderer
|
|
|
|
|
|
|
|
def is_true(value):
|
2012-07-30 06:32:34 -07:00
|
|
|
if value == 1:
|
|
|
|
return True
|
|
|
|
return bool(value) and isinstance(value, basestring) and value.lower() not in ('false', '0')
|