Ticket #15281: 15281.patch

File 15281.patch, 3.9 KB (added by Aymeric Augustin, 14 years ago)
  • django/views/static.py

     
    77import os
    88import posixpath
    99import re
    10 import stat
    1110import urllib
    1211from email.Utils import parsedate_tz, mktime_tz
    1312
     
    5756    mimetype, encoding = mimetypes.guess_type(fullpath)
    5857    mimetype = mimetype or 'application/octet-stream'
    5958    if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
    60                               statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
     59                              statobj.st_mtime, statobj.st_size):
    6160        return HttpResponseNotModified(mimetype=mimetype)
    62     contents = open(fullpath, 'rb').read()
     61    contents = open(fullpath, 'rb')
    6362    response = HttpResponse(contents, mimetype=mimetype)
    64     response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
    65     response["Content-Length"] = len(contents)
     63    response["Last-Modified"] = http_date(statobj.st_mtime)
     64    response["Content-Length"] = statobj.st_size
    6665    if encoding:
    6766        response["Content-Encoding"] = encoding
    6867    return response
  • tests/regressiontests/views/tests/static.py

     
    2626        for filename in media_files:
    2727            response = self.client.get('/views/%s/%s' % (self.prefix, filename))
    2828            file_path = path.join(media_dir, filename)
    29             self.assertEquals(open(file_path).read(), response.content)
    30             self.assertEquals(len(response.content), int(response['Content-Length']))
     29            content = str(response.content)
     30            self.assertEquals(open(file_path).read(), content)
     31            self.assertEquals(len(content), int(response['Content-Length']))
    3132            self.assertEquals(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None))
    3233
     34    def test_serve_does_not_buffer_files(self):
     35        "The static view uses an iterator - see #15281"
     36        response = self.client.get('/views/%s/file.txt' % self.prefix)
     37        # response objects can't be used as file-like objects when they are
     38        # initialized with an iterator
     39        self.assertRaises(Exception, response.write, 'more text')
     40
    3341    def test_unknown_mime_type(self):
    3442        response = self.client.get('/views/%s/file.unknown' % self.prefix)
    3543        self.assertEquals('application/octet-stream', response['Content-Type'])
     
    6876        response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
    6977                                   HTTP_IF_MODIFIED_SINCE=invalid_date)
    7078        file = open(path.join(media_dir, file_name))
    71         self.assertEquals(file.read(), response.content)
    72         self.assertEquals(len(response.content),
    73                           int(response['Content-Length']))
     79        content = str(response.content)
     80        self.assertEquals(file.read(), content)
     81        self.assertEquals(len(content), int(response['Content-Length']))
    7482
    7583    def test_invalid_if_modified_since2(self):
    7684        """Handle even more bogus If-Modified-Since values gracefully
     
    8290        invalid_date = ': 1291108438, Wed, 20 Oct 2010 14:05:00 GMT'
    8391        response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
    8492                                   HTTP_IF_MODIFIED_SINCE=invalid_date)
     93        content = str(response.content)
    8594        file = open(path.join(media_dir, file_name))
    86         self.assertEquals(file.read(), response.content)
    87         self.assertEquals(len(response.content),
    88                           int(response['Content-Length']))
     95        self.assertEquals(file.read(), content)
     96        self.assertEquals(len(content), int(response['Content-Length']))
    8997
    9098
    9199class StaticHelperTest(StaticTests):
Back to Top