#! /usr/bin/env python # -*- coding: utf-8 -*- class RemoteTemplate(object): def __init__(self, source): self.source = source from wcs.qommon.misc import simplify import os, urlparse 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) def get_template_content(self): import time, os, threading 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 except OSError: # missing file return self.update_content() else: template_content = open(self.cache_filepath).read() if time.time() > (cache_filepath_mtime + CACHE_REFRESH_TIMEOUT): threading.Thread(target=self.update_content).start() return template_content 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'}) body = """ [if-any bigdiv]
[end] [if-any title]

[title]

[end] [body] [if-any bigdiv]
[end] """ extra_head = """ [script] """ extra_top_head = """ """ user_name = """[if-any user][session_user_display_name][else]Compte Citoyen[end]""" left_menu_links = """ [if-any user]
  • Mon compte
  • Mes démarches en cours
  • Démarches en ligne
  • Déconnexion
  • [else]
  • Connexion
  • Créer un compte
  • Démarches en ligne
  • Aide
  • [end] """ right_column = """ [if-any gauche]
    [gauche]
    [end]""" custom_breadcrumb = '' from quixote.html import htmlescape response = self.response if hasattr(response, 'breadcrumb') and response.breadcrumb: s = [] path = self.static_path for i, (component, label) in enumerate(response.breadcrumb[1:]): if component.startswith('http:') or component.startswith('https:'): s.append('
  • %s
  • ' % (component, label)) continue if type(label) is str: label = htmlescape(label) if i == len(response.breadcrumb)-2: # last s.append('
  • %s
  • ' % label) else: s.append('
  • %s
  • ' % (path, component, label)) path += component custom_breadcrumb = ' '.join(s) template_content = r.text.encode('utf-8') template_content = template_content.replace('[if-any content][content][end]', body) template_content = template_content.replace( '[if-any breadcrumb-elements][breadcrumb-elements][end]', custom_breadcrumb) template_content = template_content.replace('[if-any extra-top-head][extra-top-head][end]', extra_top_head) template_content = template_content.replace('[if-any extra-head][extra-head][end]', extra_head) template_content = template_content.replace('[if-any left-menu-links][left-menu-links][end]', left_menu_links) template_content = template_content.replace('[if-any user-name][user-name][end]', user_name) template_content = template_content.replace( '[if-any right-column][right-column][end]', right_column) template_content = template_content.replace( '
    ', '
    ') self.cache(template_content) return template_content def cache(self, template_body): 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) template_content = RemoteTemplate(request.get_url()).get_template_content()