add base template for wcs/django

This commit is contained in:
Frédéric Péters 2017-08-16 15:45:42 +02:00
parent 7b16e7ff8c
commit e4a2d1dd98
2 changed files with 55 additions and 136 deletions

View File

@ -1,136 +0,0 @@
#! /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)
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 = min((
os.stat(self.cache_filepath).st_mtime,
os.lstat(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
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]
<!-- [breadcrumb] -->
[if-any title]<h2>[title]</h2>[end]
[body]
[if-any bigdiv]</div>[end]
"""
extra_head = """
[script]
<script type="text/javascript" src="/themes/vincennes-2014/dataview.js"></script>
<link rel="stylesheet" type="text/css" href="[css]">
"""
extra_top_head = """
"""
user_name = """[if-any user][session_user_display_name][else]Compte Citoyen[end]"""
left_menu_links = """
[if-any user]
<li><a href="[idp_url]accounts/">Mon compte</a></li>
<li><a href="[portal_url]demarches-en-cours/">Mes démarches en cours</a></li>
<li><a href="[portal_url]">Démarches en ligne</a></li>
<li><a href="[root_url]logout">Déconnexion</a></li>
[else]
<li><a href="[portal_url]login/">Connexion</a></li>
<li><a href="[idp_url]accounts/register/">Créer un compte</a></li>
<li><a href="[portal_url]">Démarches en ligne</a></li>
<li><a href="[portal_url]aide/">Aide</a></li>
[end]
"""
right_column = """ [if-any gauche]
<div class="steplist"><div class="panel panel-defaults">
<div class="panel-body">
[gauche]
</div>
</div></div>
[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('<li><a href="%s">%s</a></li>' % (component, label))
continue
if type(label) is str:
label = htmlescape(label)
if i == len(response.breadcrumb)-2: # last
s.append('<li class="active">%s</li>' % label)
else:
s.append('<li><a href="%s%s">%s</a></li>' % (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(
'<div id="main-content">',
'<div id="main-content" [if-any gauche][else]class="no-right-column"[end]>')
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, page_id=None):
import os
from qommon.storage import atomic_write
if not os.path.exists(self.cache_dirpath):
os.mkdir(self.cache_dirpath)
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()

55
templates/wcs/base.html Normal file
View File

@ -0,0 +1,55 @@
{% extends theme_base %}
{% load static %}
{% block extra-head %}
{{script|safe}}
<script src="/themes/vincennes-2014/dataview.js"></script>
<link rel="stylesheet" type="text/css" href="/themes/vincennes-2014/wcs.css">
{% endblock %}
{% block user-name %}
{% if session_user %}
{{session_user_display_name}}
{% else %}
Compte Citoyen
{% endif %}
{% endblock %}
{% block left-menu-links %}
{% if session_user %}
<li><a href="{{idp_url}}accounts/">Mon compte</a></li>
<li><a href="{{portal_url}}demarches-en-cours/">Mes démarches en cours</a></li>
<li><a href="{{portal_url}}">Démarches en ligne</a></li>
<li><a href="{{root_url}}logout">Déconnexion</a></li>
{% else %}
<li><a href="{{portal_url}}login/">Connexion</a></li>
<li><a href="{{idp_url}}accounts/register/">Créer un compte</a></li>
<li><a href="{{portal_url}}">Démarches en ligne</a></li>
<li><a href="{{portal_url}}aide/">Aide</a></li>
{% endif %}
{% endblock %}
{% block right-column %}
{% if gauche %}
<div class="steplist"><div class="panel panel-defaults">
<div class="panel-body">
{{gauche|safe}}
</div>
</div>
{% endif %}
{% endblock %}
{% block breadcrumb-elements %}
<li><a href="{{portal_url}}">Contact et démarches en ligne</a></li>
{% if category_slug %}<li><a href="/{{category_slug}}/">{{category_name}}</a></li>{% endif %}
{% if form_name %}<li class="active">{{form_name}}</li>{% endif %}
{% endblock %}
{% block placeholder-content %}
{% block content %}
{% if bigdiv %}<div id="{{bigdiv}}" {% if breadcrumb %}class="has-breadcrumb"{% endif %}>{% endif %}
{% if title %}<h2>{{title}}</h2>{% endif %}
{{ body|safe }}
{% if bigdiv %}</div>{% endif %}
{% endblock %}
{% endblock %}