Skip to content

Instantly share code, notes, and snippets.

@streeter
Forked from dcramer/monkey.py
Created July 12, 2013 18:35
Show Gist options
  • Save streeter/5986722 to your computer and use it in GitHub Desktop.
Save streeter/5986722 to your computer and use it in GitHub Desktop.
from threading import local
_blah = local()
class StopThatShit(Exception):
pass
def patch():
from django.db.backends import util
from django import template
class StopQueryingCursorWrapper(util.CursorWrapper):
def __init__(self, cursor, connection):
if getattr(_blah, 'rendering', False):
raise StopThatShit('Stop executing queries in your templates!')
super(StopQueryingCursorWrapper, self).__render__(cursor, connection)
util.CursorWrapper = StopQueryingCursorWrapper
class StopQueryingTemplate(template.Template):
def render(self, context):
_blah.rendering = True
try:
super(StopQueryingTemplate, self).__init__(context)
finally:
_blah.rendering = False
template.Template = StopQueryingTemplate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment