combo/combo/public/templatetags/combo.py

79 lines
3.0 KiB
Python

# combo - content management system
# Copyright (C) 2014 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import
import datetime
from django import template
from django.template import RequestContext
from combo.public.menu import get_menu_context
from combo.utils import NothingInCacheException
register = template.Library()
def skeleton_text(context, placeholder_name):
if context['request'].GET.get('format') == 'ezt':
return '[if-any %s][%s][end]' % (placeholder_name, placeholder_name)
return '{%% block %s %%}{%% endblock %%}' % placeholder_name
@register.inclusion_tag('combo/placeholder.html', takes_context=True)
def placeholder(context, placeholder_name):
context['cells'] = [x for x in context['page_cells'] if
x.placeholder == placeholder_name and
(context.get('render_skeleton') or x.is_relevant(context))]
if context.get('render_skeleton') and not context['cells']:
context['skeleton'] = skeleton_text(context, placeholder_name)
return context
@register.simple_tag(takes_context=True)
def render_cell(context, cell):
if context.get('render_skeleton') and cell.is_user_dependant(context):
return template.loader.get_template('combo/deferred-cell.html').render(context)
try:
return cell.render(context)
except NothingInCacheException:
return template.loader.get_template('combo/deferred-cell.html').render(context)
@register.simple_tag(takes_context=True)
def skeleton_extra_placeholder(context, placeholder_name):
if context.get('render_skeleton'):
return skeleton_text(context, placeholder_name)
return ''
@register.inclusion_tag('combo/menu.html', takes_context=True)
def show_menu(context, level=0, current_page=None, depth=1, reduce_depth=False):
if reduce_depth:
depth -= 1
new_context = RequestContext(context['request'], {
'page': context['page'],
'render_skeleton': context.get('render_skeleton'),
'request': context['request']})
return get_menu_context(new_context, level=level, current_page=current_page,
depth=depth)
@register.simple_tag(takes_context=True)
def page_absolute_url(context, page):
return context['request'].build_absolute_uri(page.get_online_url())
@register.filter(name='strptime')
def strptime(date_string, date_format):
try:
return datetime.datetime.strptime(date_string, date_format)
except ValueError:
return None