add remote handling templates for authentic and wcs

This commit is contained in:
Frédéric Péters 2015-05-28 15:40:39 +02:00
parent a6a83c7b03
commit e10debd018
4 changed files with 130 additions and 11 deletions

86
template.py Normal file
View File

@ -0,0 +1,86 @@
#! /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
CACHE_REFRESH_TIMEOUT = 300
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 gauche]
<div id="gauche">
[gauche]
</div>
[end]
[if-any bigdiv]<div id="[bigdiv]" [if-any breadcrumb]class="has-breadcrumb"[end]>[end]
[if-any title]<h2>[title]</h2>[end]
[body]
[if-any bigdiv]</div>[end]
"""
extra_head = """
[script]
<script type="text/javascript" src="/themes/base/static/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="[wcs_url]/myspace/">Mes démarches en cours</a></li>
<li><a href="[wcs_url]/login">Démarches en ligne</a></li>
<li><a href="[root_url]logout">Déconnexion</a></li>
[else]
<li><a href="[idp_url]/login">Connexion</a></li>
<li><a href="[idp_url]/accounts/register/">Créer un compte</a></li>
<li><a href="[wcs_url]">Démarches en ligne</a></li>
<li><a href="[portal_url]/aide">Aide</a></li>
[end]
"""
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 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)
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()

24
templates/base.html Normal file
View File

@ -0,0 +1,24 @@
{% extends theme_base %}
{% block extra-body-args %}
{% block bodyargs %}
{% endblock %}
{% endblock %}
{% block user-name %}
{% if user.is_authenticated %}{{user.get_full_name}}{% else %}Compte Citoyen{% endif %}
{% endblock %}
{% block left-menu-links %}
{% if request.user.is_authenticated %}
<li><a href="{{ idp_url }}/accounts/">Mon compte</a></li>
<li><a href="{{ wcs_url }}/myspace/">Mes démarches en cours</a></li>
<li><a href="{{ wcs_url }}/login">Démarches en ligne</a></li>
<li><a href="{% url 'auth_logout' %}">Déconnexion</a></li>
{% else %}
<li><a href="{{ idp_url }}/login">Connexion</a></li>
<li><a href="{{ idp_url }}/accounts/register/">Créer un compte</a></li>
<li><a href="{{ wcs_url }}">Démarches en ligne</a></li>
<li><a href="{{ portal_url }}/aide">Aide</a></li>
{% endif %}
{% endblock %}

View File

@ -39,6 +39,10 @@ class="page-{{ page.slug }}"
{% placeholder "footer" %}
{% endblock %}
{% block user-name %}
{% if user.is_authenticated %}{{user.get_full_name}}{% else %}Compte Citoyen{% endif %}
{% endblock %}
{% block user-info %}
{% if user.is_authenticated %}
<span class="logged-in">
@ -48,3 +52,17 @@ class="page-{{ page.slug }}"
{% endif %}
{% skeleton_extra_placeholder 'user-info' %}
{% endblock %}
{% block left-menu-links %}
{% if request.user.is_authenticated %}
<li><a href="{{ idp_url }}/accounts/">Mon compte</a></li>
<li><a href="{{ wcs_url }}/myspace/">Mes démarches en cours</a></li>
<li><a href="{{ wcs_url }}/login">Démarches en ligne</a></li>
<li><a href="{% url 'auth_logout' %}">Déconnexion</a></li>
{% else %}
<li><a href="{{ idp_url }}/login">Connexion</a></li>
<li><a href="{{ idp_url }}/accounts/register/">Créer un compte</a></li>
<li><a href="{{ wcs_url }}">Démarches en ligne</a></li>
<li><a href="{{ portal_url }}/aide">Aide</a></li>
{% endif %}
{% endblock %}

View File

@ -53,17 +53,8 @@
</div>
<div class="panel-body">
<ul>
{% if request.user.is_authenticated %}
<li><a href="{{ idp_url }}/accounts/">Mon compte</a></li>
<li><a href="{{ wcs_url }}/myspace/">Mes démarches en cours</a></li>
<li><a href="{{ wcs_url }}/login">Démarches en ligne</a></li>
<li><a href="{% url 'auth_logout' %}">Déconnexion</a></li>
{% else %}
<li><a href="{{ idp_url }}/login">Connexion</a></li>
<li><a href="{{ idp_url }}/accounts/register/">Créer un compte</a></li>
<li><a href="{{ wcs_url }}">Démarches en ligne</a></li>
<li><a href="{{ portal_url }}/aide">Aide</a></li>
{% endif %}
{% block left-menu-links %}
{% endblock %}
</ul>
</div>
</div>