Changeset 1510 for trunk/pylucid/PyLucid/index.py
- Timestamp:
- 03/27/08 16:59:44 (8 months ago)
- Files:
-
- 1 modified
-
trunk/pylucid/PyLucid/index.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pylucid/PyLucid/index.py
r1507 r1510 18 18 """ 19 19 20 import datetime, md521 22 20 from django.http import HttpResponse, HttpResponsePermanentRedirect, \ 23 21 HttpResponseRedirect 22 from django.conf import settings 24 23 from django.template import RequestContext 25 from django. core.cache import cache24 from django.utils.safestring import mark_safe 26 25 from django.utils.translation import ugettext as _ 27 from django.utils.safestring import mark_safe 28 from django.conf import settings 29 30 from PyLucid import models 31 26 27 from PyLucid.models import Page 32 28 from PyLucid.system import plugin_manager 29 from PyLucid.system.URLs import URLs 30 from PyLucid.system.page_msg import PageMessages 33 31 from PyLucid.system.response import SimpleStringIO 34 32 from PyLucid.system.exceptions import AccessDenied 35 from PyLucid.system. page_msg import PageMessages33 from PyLucid.system.context_processors import add_dynamic_context, add_css_tag 36 34 from PyLucid.system.detect_page import get_current_page_obj, \ 37 35 get_default_page_id 38 from PyLucid.system.URLs import URLs 39 from PyLucid.system.context_processors import add_dynamic_context, add_css_tag 40 from PyLucid.system.utils import setup_debug 36 from PyLucid.tools.utils import escape, escape_django_tags 41 37 from PyLucid.tools.content_processors import apply_markup, \ 42 38 render_string_template, redirect_warnings 43 from PyLucid.tools.utils import escape, escape_django_tags44 39 from PyLucid.plugins_internal.page_style.page_style import replace_add_data 45 40 … … 135 130 136 131 137 def patch_response_headers(response, cache_timeout, ETag, last_modified):138 """139 Adds some useful headers to the given HttpResponse object:140 ETag, Last-Modified, Expires and Cache-Control141 142 Original version: django.utils.cache.patch_response_headers()143 """144 response['ETag'] = ETag145 response['Last-Modified'] = last_modified.strftime(146 '%a, %d %b %Y %H:%M:%S GMT'147 )148 now = datetime.datetime.utcnow()149 expires = now + datetime.timedelta(0, cache_timeout)150 response['Expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S GMT')151 152 153 def get_cached_data(url):154 """155 -Build the cache_key from the given url. Use the last page shortcut.156 -retuned the cache_key and the page data.157 """158 if url == "":159 # Request without a shortcut -> request the default page160 shortcut = "/"161 else:162 # Note: We use append_slash, but the url pattern striped the last163 # slash out.164 # e.g.: '/page1/page2/page3' -> ['/page1/page2', 'page3'] -> 'page3'165 shortcut = url.rsplit("/", 1)[-1]166 167 cache_key = settings.PAGE_CACHE_PREFIX + shortcut168 #print "Used cache key:", cache_key169 170 # Get the page data from the cache.171 response = cache.get(cache_key)172 173 return cache_key, response174 175 176 132 def index(request, url): 177 133 """ … … 181 137 the page shortcut from the url. 182 138 """ 183 # Cache only for anonymous users. Otherwise users how are log-in don't see184 # the dynamic integrate admin menu.185 use_cache = request.user.is_anonymous()186 187 if use_cache:188 # Try to get the cms page request from the cache189 cache_key, response = get_cached_data(url)190 if response:191 # This page has been cached in the past, use the cache data:192 return response193 194 setup_debug(request)195 196 139 try: 197 140 current_page_obj = get_current_page_obj(request, url) … … 211 154 212 155 context = _get_context(request, current_page_obj) 156 213 157 # Get the response for the requested cms page: 214 158 response = _render_cms_page(request, context) 215 159 216 if use_cache: 217 # It's a anonymous user -> Cache the cms page. 218 cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS 219 # Add some headers for the browser cache 220 patch_response_headers( 221 response, cache_timeout, 222 ETag = md5.new(cache_key).hexdigest(), 223 last_modified = current_page_obj.lastupdatetime, 224 ) 225 # Save the page into the cache 226 cache.set(cache_key, response, cache_timeout) 160 if getattr(request, "_use_cache", None) == None: 161 # Set _use_cache information for the PyLucid cache middleware, but only 162 # if it was set to true or false in the past 163 request._use_cache = True 227 164 228 165 return response 166 229 167 230 168 def _get_page(request, page_id): … … 233 171 TODO: Check int(page_id)! 234 172 """ 235 setup_debug(request)236 237 173 try: 238 current_page_obj = models.Page.objects.get(id=int(page_id))239 except models.Page.DoesNotExist:174 current_page_obj = Page.objects.get(id=int(page_id)) 175 except Page.DoesNotExist: 240 176 # The ID in the url is wrong -> goto the default page 241 177 default_page_id = get_default_page_id() 242 current_page_obj = models.Page.objects.get(id=default_page_id)178 current_page_obj = Page.objects.get(id=default_page_id) 243 179 244 180 user = request.user … … 256 192 257 193 return current_page_obj 194 258 195 259 196 def handle_command(request, page_id, module_name, method_name, url_args): … … 329 266 return HttpResponsePermanentRedirect(url) 330 267 268 331 269 def permalink(request, page_id): 332 270 """
