Skip to content

Instantly share code, notes, and snippets.

@stephrdev
Created September 16, 2010 10:04
Show Gist options
  • Save stephrdev/582199 to your computer and use it in GitHub Desktop.
Save stephrdev/582199 to your computer and use it in GitHub Desktop.
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse
from django.utils import simplejson
from random import choice
import string
def render_tuple(f):
def wrap(request, *args, **kwargs):
context = f(request, *args, **kwargs)
if not isinstance(context, tuple):
return context
if context[0] == 'json':
return HttpResponse(simplejson.dumps(context[1]),
mimetype='application/json')
elif context[0] == 'jsonp':
return HttpResponse('%s(%s)' % (context[1], simplejson.dumps(context[2])),
mimetype='application/json')
else:
return render_to_response(
context[0],
context[1],
context_instance=RequestContext(request),
)
return wrap
def pwgen(length):
return ''.join([choice(string.letters + string.digits) for i in range(length)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment