scribeengine/scribeengine/helpers.py

124 lines
4.6 KiB
Python

# -*- coding: utf-8 -*-
###############################################################################
# ScribeEngine - Open Source Content Management System #
# --------------------------------------------------------------------------- #
# Copyright (c) 2010-2021 Raoul Snyman #
# --------------------------------------------------------------------------- #
# This file is part of ScribeEngine. #
# #
# ScribeEngine is free software: you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# ScribeEngine 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 General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with ScribeEngine. If not, see <https://www.gnu.org/licenses/>. #
###############################################################################
"""
The :mod:`~scribeengine.helpers` module contains some theme helper methods
"""
from flask import current_app
from flask_themes2 import get_theme, render_theme_template, template_exists as ft2_template_exists
from jinja2 import TemplateNotFound, contextfunction
from scribeengine.models import Menu, Site, Variable
@contextfunction
def template_exists(context, template_name):
"""
Add the template_exists() method into the template context
"""
return ft2_template_exists(template_name)
def get_variable(name, default_value=None):
def _type_cast(type_, value, default_value):
try:
return type_(var.value)
except (TypeError, ValueError):
return default_value
var = Variable.get(name)
if var:
if var.type.lower().startswith('int'):
return _type_cast(int, var.value, default_value)
elif var.type.lower().startswith('float'):
return _type_cast(float, var.value, default_value)
elif var.type.lower().startswith('bool'):
return var.value.lower().strip() in ['yes', 'y', '1', 'true', 't']
return var.value
else:
return default_value
def get_site_details():
"""
Returns an object with the details of the site
"""
return Site(get_variable('site-name', 'Example Site'),
get_variable('site-slogan', 'From the Firehose'),
get_variable('site-about', ''))
def get_navigation(block='primary'):
"""
Get the navigation
"""
return Menu.query.filter(Menu.block == block).first()
def get_current_theme():
"""
Determine the current theme.
"""
ident = current_app.config.get('THEME_DEFAULT', 'quill')
return get_theme(ident)
def render(template, **context):
"""
Render a template, after selecting a theme
"""
context.update({'site': get_site_details(), 'navigation': get_navigation()})
return render_theme_template(get_current_theme(), template, **context)
def render_node(node):
"""
Render a template, after selecting a theme
"""
context = {'node': node.complete}
template_names = ['/node-{}.html'.format(node.type), '/node.html']
for template in template_names:
if ft2_template_exists(template):
return render(template, **context)
raise TemplateNotFound('Could not find a node template')
def render_node_list(nodes):
"""
Render a list of nodes
"""
context = {'nodes': [node.complete for node in nodes]}
template_names = ['/node-list.html']
if nodes:
template_names.insert(0, '/node-{}-list.html'.format(nodes[0].type))
for template in template_names:
if ft2_template_exists(template):
return render(template, **context)
raise TemplateNotFound('Could not find a node-list template')
def render_admin(template, **context):
"""
Render a template, after selecting a theme
"""
context.update({'site': get_site_details()})
return render_theme_template('admin', template, **context)