#15281: Staticfiles sever view should use generator ------------------------------------------------------+--------------------- Reporter: FunkyBob | Owner: Status: new | Milestone: 1.4 Component: django.contrib.staticfiles | Version: SVN Resolution: | Keywords: generator Triage Stage: Accepted | Has patch: 1 Needs documentation: 0 | Needs tests: 1 Patch needs improvement: 1 | ------------------------------------------------------+---------------------
Comment (by anonymous): Recent trunk changes have moved the work from contrib.staticfiles.views to views.static. Also, to address the test failures, I've adjusted them to consume the request.content only once. The new patch is smaller [doesn't clean up the stat.ST_* usage], and makes the tests pass for me. {{{ Index: django/views/static.py =================================================================== --- django/views/static.py (revision 15530) +++ django/views/static.py (working copy) @@ -59,10 +59,9 @@ if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'), statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]): return HttpResponseNotModified(mimetype=mimetype) - contents = open(fullpath, 'rb').read() - response = HttpResponse(contents, mimetype=mimetype) + response = HttpResponse(open(fullpath, 'rb'), mimetype=mimetype) response["Last-Modified"] = http_date(statobj[stat.ST_MTIME]) - response["Content-Length"] = len(contents) + response["Content-Length"] = statobj.st_size if encoding: response["Content-Encoding"] = encoding return response Index: tests/regressiontests/views/tests/static.py =================================================================== --- tests/regressiontests/views/tests/static.py (revision 15530) +++ tests/regressiontests/views/tests/static.py (working copy) @@ -22,8 +22,9 @@ for filename in media_files: response = self.client.get('/views/site_media/%s' % filename) file_path = path.join(media_dir, filename) - self.assertEquals(open(file_path).read(), response.content) - self.assertEquals(len(response.content), int(response ['Content-Length'])) + content = str(response.content) + self.assertEquals(open(file_path).read(), content) + self.assertEquals(len(content), int(response['Content- Length'])) self.assertEquals(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None)) def test_unknown_mime_type(self): @@ -65,9 +66,9 @@ response = self.client.get('/views/site_media/%s' % file_name, HTTP_IF_MODIFIED_SINCE=invalid_date) file = open(path.join(media_dir, file_name)) - self.assertEquals(file.read(), response.content) - self.assertEquals(len(response.content), - int(response['Content-Length'])) + content = str(response.content) + self.assertEquals(file.read(), content) + self.assertEquals(len(content), int(response['Content-Length'])) def test_invalid_if_modified_since2(self): """Handle even more bogus If-Modified-Since values gracefully @@ -80,6 +81,6 @@ response = self.client.get('/views/site_media/%s' % file_name, HTTP_IF_MODIFIED_SINCE=invalid_date) file = open(path.join(media_dir, file_name)) - self.assertEquals(file.read(), response.content) - self.assertEquals(len(response.content), - int(response['Content-Length'])) + content = str(response.content) + self.assertEquals(file.read(), content) + self.assertEquals(len(content), int(response['Content-Length'])) }}} -- Ticket URL: <http://code.djangoproject.com/ticket/15281#comment:5> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.