combo/combo/public/templatetags/combo.py

58 lines
2.1 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()
@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 x.is_relevant(context)]
return context
@register.simple_tag(takes_context=True)
def render_cell(context, cell):
try:
return cell.render(context)
except NothingInCacheException:
return template.loader.get_template('combo/deferred-cell.html').render(context)
@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'],
'request': context['request']})
return get_menu_context(new_context, level=level, current_page=current_page,
depth=depth)
@register.filter(name='strptime')
def strptime(date_string, date_format):
try:
return datetime.datetime.strptime(date_string, date_format)
except ValueError:
return None