Login

SizeAndTimeMiddleware

Author:
amitu
Posted:
October 14, 2008
Language:
Python
Version:
1.0
Score:
4 (after 4 ratings)

Used for showing size of the page in human readable format and time taken to generate the page on the server. To use it, in your base template, somewhere put the line: <!-- ____SIZE_AND_DATE_PLACEHOLDER____ -->. May be used on production.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import time
from django.utils.encoding import smart_unicode
from django.template.defaultfilters import filesizeformat

class SizeAndTimeMiddleware(object):
    def process_request(self, request):
        request._request_start_time = time.time() 

    def process_response(self, request, response):
        if not hasattr(request, "_request_start_time"): return response
        if response['Content-Type'].split(';')[0] in (
            'text/html', 'application/xhtml+xml'
        ):
            response.content = smart_unicode(response.content).replace(
                "<!-- ____SIZE_AND_DATE_PLACEHOLDER____ -->", 
                "(%s, %0.3f seconds)" % (
                    filesizeformat(len(response.content)),
                    time.time() - request._request_start_time,
                )
            )
        return response

More like this

  1. csrf test by zukosdead 1 month, 2 weeks ago
  2. find even number by Rajeev529 9 months, 3 weeks ago
  3. Form field with fixed value by roam 10 months, 2 weeks ago
  4. New Snippet! by Antoliny0919 10 months, 3 weeks ago
  5. Add Toggle Switch Widget to Django Forms by OgliariNatan 1 year, 1 month ago

Comments

Please login first before commenting.