Hide keyboard shortcuts

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

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

# Copyright 2009-2011 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

"""A middleware for sending SQL statements to the Lino logger. 

Intended for use with the django development server. 

 

 

""" 

from __future__ import print_function 

from builtins import object 

 

from django.db import connection 

from django.template import Template, Context 

 

 

class SQLLogMiddleware(object): 

 

    """ 

    Log all SQL statements direct to the console. 

 

    Based on http://djangosnippets.org/snippets/1672/ 

    but writes to a file on the server instead of to the response. 

    """ 

 

    def process_response(self, request, response): 

        time = 0.0 

        for q in connection.queries: 

            time += float(q['time']) 

 

        t = Template('''<html><body> 

            <p><em>Total query count:</em> {{ count }}<br/> 

            <em>Total execution time:</em> {{ time }}</p> 

            <ul class="sqllog"> 

                {% for sql in sqllog %} 

                    <li>{{ sql.time }}: {{ sql.sql }}</li> 

                {% endfor %} 

            </ul> 

            </body></html> 

        ''') 

 

        #~ response.content = "%s%s" % ( response.content, t.render(Context({'sqllog':connection.queries,'count':len(connection.queries),'time':time}))) 

        html = t.render( 

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

        html = html.replace('FROM', '<b>FROM</b>') 

        file('sqllog.html', 'w').write(html.encode('utf-8')) 

        return response 

 

 

class SQLLogToConsoleMiddleware(object): 

 

    """ 

    Log all SQL statements to the console. 

    Intended for use with the django development server. 

 

    Based on http://djangosnippets.org/snippets/1672/ 

    but removed the test for settings.DEBUG. 

    """ 

 

    def process_response(self, request, response): 

        if 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 ShortSQLLogToConsoleMiddleware(object): 

 

    """ 

    Log a summary of the SQL statements made to the console. 

    Intended for use with the django development server. 

 

    """ 

 

    def process_response(self, request, response): 

        if connection.queries: 

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

            t = Template( 

                "{{count}} quer{{count|pluralize:\"y,ies\"}} in {{time}} seconds") 

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

        return response