def test_find_asset(self): path1 = self.mkfile('static/some/dir/main.out.in') path2 = self.mkfile('static/another/dir/main.in2') asset_path1, filter1 = assets.find('some/dir/main.out') asset_path2, filter2 = assets.find('another/dir/main.out2') asset_path3, filter3 = assets.find('non/existent/file.out') assert_equal(path1, asset_path1) assert_is_instance(filter1, Filter1) assert_equal(path2, asset_path2) assert_is_instance(filter2, Filter2) assert_equal(None, asset_path3) assert_equal(None, filter3)
def serve(request, path, document_root=None, insecure=False, **kwargs): """ Serve static files below a given point in the directory structure or from locations inferred from the staticfiles finders. To use, put a URL pattern such as:: (r'^(?P<path>.*)$', 'assetfiles.views.serve') in your URLconf. It uses the django.views.static view to serve the found files. """ if not settings.DEBUG and not insecure: raise ImproperlyConfigured('The staticfiles view can only be used in ' 'debug mode or if the the --insecure ' "option of 'runserver' is used") normalized_path = posixpath.normpath(unquote(path)).lstrip('/') static_path = finders.find(normalized_path) if static_path: document_root, path = os.path.split(static_path) return static.serve(request, path, document_root=document_root, **kwargs) asset_path, filter = assets.find(normalized_path) if asset_path: content = filter.filter(asset_path) mimetype, encoding = mimetypes.guess_type(normalized_path) return HttpResponse(content, content_type=mimetype) if path.endswith('/') or path == '': raise Http404('Directory indexes are not allowed here.') raise Http404("'%s' could not be found" % path)