public
Last active

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
def merge(url):
full_url=[]
for i, el in enumerate(url):
if i%2==0:
full_url.append(el)
full_url = ''.join(full_url)
return full_url
def combineflatten(seq):
items= tuple(item for item in seq if not isinstance(item, tuple))
yield items
for item in seq:
if isinstance(item, tuple):
for yielded in combineflatten(item):
yield items+yielded
def generate_flat_urls(tree_urls):
"""
>>> tree_urls = ('', 'list',
... ('edit/', 'edit', ('auto/', 'edit_auto')),
... ('^add/', 'add'),
... ('delete/', 'delete', ('delete/', 'delete_hard'))
... )
 
>>> generate_flat_urls(tree_urls)
[('^$', 'list'), ('^edit/$', 'edit'), ('^edit/auto/$', 'edit_auto'), ('^^add/$', 'add'), ('^delete/$', 'delete'), ('^delete/delete/$', 'delete_hard')]
"""
return [('^%s$'%merge(el), el[-1]) for el in tuple(combineflatten(tree_urls))]
from django.shortcuts import render_to_response
 
def render_with_auth_check(request, payload, request_context, *args, **kwrags):
for el in payload.itervalues():
try:
has_auth = el.check_auth(request.user)
if not has_auth:
raise AuthFailException
except ValueError:
pass #Not all objects have check_auth
return render_to_response(request, payload, request_context, *args, **kwrags)
class AuthFailHandlerMiddleware:
def process_exception(self, request, exception):
if type(exception) == AuthFailException:
return render_to_response('accounts/login/', {}, RequestContext(request))
def render_jinja_to_response(template_name, payload):
#This should probably go in settings.py
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
template = env.get_template(template_name)
response = template.render(**payload)
return HttpResponse(response)
 
 
 

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.