Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from django.db import connection 

from django.template import Template, Context 

from django.conf import settings 

 

# 

# Log all SQL statements direct to the console (when running in DEBUG) 

# Intended for use with the django development server. 

# 

 

class SQLLogToConsoleMiddleware: 

    """ 

    Add to settings_local like so to log sql commands: 

 

    from madrona.common.default_settings import MIDDLEWARE_CLASSES 

 

    MIDDLEWARE_CLASSES += ( 

        'madrona.common.middleware.SQLLogToConsoleMiddleware', 

    ) 

    """ 

    def process_response(self, request, response): 

        if settings.DEBUG and connection.queries: 

            time = sum([float(q['time']) for q in connection.queries]) 

            t = Template("{{count}} quer{{count|pluralize:\"y,ies\"}} in {{time}} seconds:\n\n{% for sql in sqllog %}[{{forloop.counter}}] {{sql.time}}s: {{sql.sql|safe}}{% if not forloop.last %}\n\n{% endif %}{% endfor %}") 

            print t.render(Context({'sqllog':connection.queries,'count':len(connection.queries),'time':time})) 

        return response 

 

class IgnoreCsrfMiddleware(object): 

    """ 

    http://djangosnippets.org/snippets/2069/ 

    CSRF protection requires updating all forms 

    Meanwhile the crsf middleware is required for admin to work. 

    This middleware class will just ignore csrf, allowing the current views and admin to work 

    """ 

    def process_request(self, request): 

        request.csrf_processing_done = True