SizeAndTimeMiddleware

 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. Modelaware json serializer by fivethreeo 7 years, 1 month ago
  2. Paginator Tag by insin 7 years, 1 month ago
  3. Filter to adjust forloop.counter across pages in a paginated view by egmanoj 5 years, 1 month ago
  4. Disable fields in oldforms admin using jQuery by schraal 5 years, 10 months ago
  5. Pagination/Filtering Alphabetically by zain 5 years, 1 month ago

Comments

(Forgotten your password?)