# authentic2 - versatile identity manager
# Copyright (C) 2010-2020 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 django.template import TemplateSyntaxError, VariableDoesNotExist, engines
from django.utils.encoding import force_str
from django.utils.translation import ugettext_lazy as _


class TemplateError(Exception):
    pass


class Template:
    def __init__(self, value, raises=False):
        self.value = value
        self.raises = raises

        try:
            self.template = engines['django'].from_string(value)
        except TemplateSyntaxError as e:
            if self.raises:
                raise TemplateError(_('template syntax error: %s') % e)

    def render(self, context={}):
        if not hasattr(self, 'template'):
            # oops, silent error during initialization, let's get outta here
            return force_str(self.value)
        try:
            rendered = self.template.render(context)
        except TemplateSyntaxError as e:
            if self.raises:
                raise TemplateError(_('template syntax error: %s') % e)
            return force_str(self.value)
        except VariableDoesNotExist as e:
            if self.raises:
                raise TemplateError(_('missing template variable: %s') % e)
            return force_str(self.value)
        return force_str(rendered)

    def null_render(self, context={}):
        return str(self.value)
