Permalink
Browse files

handle path in cached filenames

  • Loading branch information...
1 parent 975c583 commit a4e89cb31fd35681d86cf876e0aad8cc1598cbe3 @mrj0 committed Mar 5, 2011
Showing with 23 additions and 9 deletions.
  1. +1 −1 setup.py
  2. +17 −5 staticgenerator/__init__.py
  3. +5 −3 staticgenerator/middleware.py
View
@@ -1,6 +1,6 @@
from distutils.core import setup
-version = '1.4.1'
+version = '1.4.1.1-mrj0'
setup(name='staticgenerator',
version=version,
@@ -3,10 +3,12 @@
"""Static file generator for Django."""
import stat
+import urlparse
import shutil
from django.utils.functional import Promise
+from django.http import QueryDict
from filesystem import FileSystem
from handlers import DummyHandler
@@ -155,7 +157,10 @@ def get_content_from_path(self, path):
"""
request = self.http_request()
- request.path_info = path
+ # We must parse the path to grab query string
+ parsed = urlparse.urlparse(path)
+ request.path_info = parsed.path
+ request.GET = QueryDict(parsed.query)
request.META.setdefault('SERVER_PORT', 80)
request.META.setdefault('SERVER_NAME', self.server_name)
@@ -170,23 +175,30 @@ def get_content_from_path(self, path):
return response.content
- def get_filename_from_path(self, path):
+ def get_filename_from_path(self, path, query_string):
"""
- Returns (filename, directory)
+ Returns (filename, directory). None if unable to cache this request.
Creates index.html for path if necessary
"""
if path.endswith('/'):
path = '%sindex.html' % path
+ # will not work on windows... meh
+ if query_string:
+ path += "?" + query_string
filename = self.fs.join(self.web_root, path.lstrip('/')).encode('utf-8')
+ if len(filename) > 255:
+ return None, None
return filename, self.fs.dirname(filename)
- def publish_from_path(self, path, content=None):
+ def publish_from_path(self, path, query_string, content=None):
"""
Gets filename and content for a path, attempts to create directory if
necessary, writes to file.
"""
- filename, directory = self.get_filename_from_path(path)
+ filename, directory = self.get_filename_from_path(path, query_string)
+ if not filename:
+ return # cannot cache
if not content:
content = self.get_content_from_path(path)
@@ -19,20 +19,22 @@ class StaticGeneratorMiddleware(object):
gen = StaticGenerator()
def process_response(self, request, response):
+ path = request.path_info
+ query_string = request.META.get('QUERY_STRING', '')
if response.status_code == 200:
if getattr(settings, 'STATIC_GENERATOR_ANONYMOUS_ONLY', False) and not request.user.is_anonymous():
return response
excluded = False
for url in self.excluded_urls:
- if url.match(request.path_info):
+ if url.match(path):
excluded = True
break
if not excluded:
for url in self.urls:
- if url.match(request.path_info):
- self.gen.publish_from_path(request.path_info, response.content)
+ if url.match(path):
+ self.gen.publish_from_path(path, query_string, response.content)
break
return response

0 comments on commit a4e89cb

Please sign in to comment.