summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2017-03-19 21:22:49 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2017-06-12 22:01:15 (GMT)
commit726e174d56510522f7c588a6f2a2743d3c6e641a (patch)
treed0b8f1310b0c8e4bad1e60ce84f089e7ea6fe729
parent6c7eb897dd62a45fbbe2efa3afe967fe171e93a6 (diff)
downloadvincennes-2014-726e174d56510522f7c588a6f2a2743d3c6e641a.zip
vincennes-2014-726e174d56510522f7c588a6f2a2743d3c6e641a.tar.gz
vincennes-2014-726e174d56510522f7c588a6f2a2743d3c6e641a.tar.bz2
sync with publik-base-theme (#15501)
merges: 8229427 wcs: only cache templates for successful requests (#10966) 9c06e15 template.py: don't access publisher object in thread (#9397) 48b7f3f wcs: use X-Combo-Page-Id when caching skeleton for wcs (#9097) b08c78a wcs: raise an error if skeleton gives a http error
-rw-r--r--template.py29
1 files changed, 23 insertions, 6 deletions
diff --git a/template.py b/template.py
index 8916e80..a992c08 100644
--- a/template.py
+++ b/template.py
@@ -9,15 +9,21 @@ class RemoteTemplate(object):
self.cache_key = simplify(urlparse.urlunparse(urlparse.urlparse(self.source)[:3] + ('', '', '')))
self.cache_dirpath = os.path.join(publisher.app_dir, 'skeleton-cache')
self.cache_filepath = os.path.join(self.cache_dirpath, self.cache_key)
+ self.theme_skeleton_url = publisher.get_site_option('theme_skeleton_url')
+ self.skip_cache = (request.response.status_code != 200)
def get_template_content(self):
import time, os, threading
+ if self.skip_cache:
+ return self.update_content()
from quixote import get_response, get_publisher
CACHE_REFRESH_TIMEOUT = 300
self.response = get_response()
self.static_path = get_publisher().get_application_static_files_root_url()
try:
- cache_filepath_mtime = os.stat(self.cache_filepath).st_mtime
+ cache_filepath_mtime = min((
+ os.stat(self.cache_filepath).st_mtime,
+ os.lstat(self.cache_filepath).st_mtime))
except OSError: # missing file
return self.update_content()
else:
@@ -28,9 +34,9 @@ class RemoteTemplate(object):
def update_content(self):
import requests
- theme_skeleton_url = publisher.get_site_option('theme_skeleton_url')
- r = requests.get(theme_skeleton_url, params={'source': self.source, 'format': 'ezt'})
+ r = requests.get(self.theme_skeleton_url, params={'source': self.source, 'format': 'ezt'})
+ r.raise_for_status()
body = """
[if-any bigdiv]<div id="[bigdiv]" [if-any breadcrumb]class="has-breadcrumb"[end]>[end]
@@ -106,14 +112,25 @@ class RemoteTemplate(object):
template_content = template_content.replace(
'<div id="main-content">',
'<div id="main-content" [if-any gauche][else]class="no-right-column"[end]>')
- self.cache(template_content)
+ if not self.skip_cache:
+ self.cache(template_content, page_id=r.headers.get('X-Combo-Page-Id'))
return template_content
- def cache(self, template_body):
+ def cache(self, template_body, page_id=None):
import os
from qommon.storage import atomic_write
if not os.path.exists(self.cache_dirpath):
os.mkdir(self.cache_dirpath)
- atomic_write(self.cache_filepath, template_body)
+ if page_id:
+ # if there's a page id we store the content under the page id
+ # filename and create a symlink from the cache key
+ cache_filepath = os.path.join(self.cache_dirpath, page_id)
+ atomic_write(cache_filepath, template_body)
+ # always recreate symlink to get an updated mtime
+ if os.path.exists(self.cache_filepath):
+ os.unlink(self.cache_filepath)
+ os.symlink(page_id, self.cache_filepath)
+ else:
+ atomic_write(self.cache_filepath, template_body)
template_content = RemoteTemplate(request.get_url()).get_template_content()