80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2011 Entr'ouvert
|
|
#
|
|
# This program 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 2 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 General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from quixote.html import htmltext, TemplateIO
|
|
|
|
class Substitutions(object):
|
|
substitutions_dict = {}
|
|
dynamic_sources = []
|
|
|
|
sources = None
|
|
|
|
def __init__(self):
|
|
self.sources = []
|
|
|
|
@classmethod
|
|
def register(cls, varname, category=None, comment=None):
|
|
if cls.substitutions_dict.has_key(varname):
|
|
return
|
|
|
|
cls.substitutions_dict[varname] = {
|
|
'category': category,
|
|
'comment': comment
|
|
}
|
|
|
|
@classmethod
|
|
def register_dynamic_source(cls, klass):
|
|
if not cls.dynamic_sources:
|
|
cls.dynamic_sources = []
|
|
cls.dynamic_sources.append(klass)
|
|
|
|
def reset(self):
|
|
self.sources = []
|
|
|
|
def feed(self, source):
|
|
if source is None:
|
|
# silently ignore a None source, this is for example useful when
|
|
# adding the current user, as it may be None if he is not logged
|
|
# in.
|
|
return
|
|
self.sources.append(source)
|
|
|
|
def get_context_variables(self):
|
|
d = {}
|
|
for source in self.sources:
|
|
d.update(source.get_substitution_variables())
|
|
return d
|
|
|
|
@classmethod
|
|
def get_substitution_html_table(cls):
|
|
from qommon import _
|
|
r = TemplateIO(html=True)
|
|
r += htmltext('<table id="substvars">')
|
|
r += htmltext('<thead><tr><th>%s</th><th>%s</th><th>%s</th></tr></thead>' % (
|
|
_('Category'), _('Variable'), _('Comment')))
|
|
r += htmltext('<tbody>')
|
|
vars = [(_(y.get('category')), x, _(y.get('comment')))
|
|
for x, y in cls.substitutions_dict.items()]
|
|
for dynamic_source in cls.dynamic_sources:
|
|
vars.extend(dynamic_source.get_substitution_variables_list())
|
|
vars.sort()
|
|
for category, variable, comment in vars:
|
|
r += htmltext('<tr><td>%s</td><td>%s</td><td>%s</td>' % (
|
|
category, '[%s]' % variable, comment))
|
|
r += htmltext('</tbody>')
|
|
r += htmltext('</table>')
|
|
return r.getvalue()
|