# Buggy code
def reload_from_db(self):
    """
    Read the timestamp from the database. If the timestamp is newer than
    the internal timestamp, then read the complete data
    :return:
    """
    if (
        not self.timestamp
        or self.timestamp
        + datetime.timedelta(
            seconds=current_app.config.get("PI_CHECK_RELOAD_CONFIG", 0)
        )
        < datetime.datetime.now()
    ):
        db_ts = Config.query.filter_by(Key=PRIVACYIDEA_TIMESTAMP).first()
        if reload_db(self.timestamp, db_ts):
            self.config = {}
            self.resolver = {}
            self.realm = {}
            self.default_realm = None
            for sysconf in Config.query.all():
                self.config[sysconf.Key] = {
                    "Value": sysconf.Value,
                    "Type": sysconf.Type,
                    "Description": sysconf.Description,
                }
            for resolver in Resolver.query.all():
                resolverdef = {"type": resolver.rtype, "resolvername": resolver.name}
                data = {}
                for rconf in resolver.config_list:
                    if rconf.Type == "password":
                        value = decryptPassword(rconf.Value)
                    else:
                        value = rconf.Value
                    data[rconf.Key] = value
                resolverdef["data"] = data
                self.resolver[resolver.name] = resolverdef

            for realm in Realm.query.all():
                if realm.default:
                    self.default_realm = realm.name
                realmdef = {
                    "option": realm.option,
                    "default": realm.default,
                    "resolver": [],
                }
                for x in realm.resolver_list:
                    realmdef["resolver"].append(
                        {
                            "priority": x.priority,
                            "name": x.resolver.name,
                            "type": x.resolver.rtype,
                        }
                    )
                self.realm[realm.name] = realmdef

        self.timestamp = datetime.datetime.now()


# Traceback (UnicodeDecodeError)
[2018-08-14 14:49:53,123] ERROR in app: Exception on /system/documentation [GET]
Traceback (most recent call last):
File "/home/fred/privacyidea/privacyidea/venv/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/fred/privacyidea/privacyidea/venv/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/fred/privacyidea/privacyidea/venv/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/fred/privacyidea/privacyidea/venv/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/fred/privacyidea/privacyidea/venv/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/fred/privacyidea/privacyidea/privacyidea/api/lib/prepolicy.py", line 120, in policy_wrapper
return wrapped_function(*args, **kwds)
File "/home/fred/privacyidea/privacyidea/privacyidea/api/auth.py", line 316, in decorated_function
return f(*args, **kwargs)
File "/home/fred/privacyidea/privacyidea/privacyidea/api/system.py", line 113, in get_config_documentation
context=context))
File "/home/fred/privacyidea/privacyidea/venv/lib/python2.7/site-packages/flask/templating.py", line 135, in render_template
context, ctx.app)
File "/home/fred/privacyidea/privacyidea/venv/lib/python2.7/site-packages/flask/templating.py", line 117, in _render
rv = template.render(context)
File "/home/fred/privacyidea/privacyidea/venv/lib/python2.7/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/home/fred/privacyidea/privacyidea/venv/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/fred/privacyidea/privacyidea/privacyidea/static/templates/documentation.rst", line 56, in top-level template code
{{k}}: **{{v}}**
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 8: ordinal not in range(128)

# Fixed code
def reload_from_db(self):
    """
    Read the timestamp from the database. If the timestamp is newer than
    the internal timestamp, then read the complete data
    :return:
    """
    if (
        not self.timestamp
        or self.timestamp
        + datetime.timedelta(
            seconds=current_app.config.get("PI_CHECK_RELOAD_CONFIG", 0)
        )
        < datetime.datetime.now()
    ):
        db_ts = Config.query.filter_by(Key=PRIVACYIDEA_TIMESTAMP).first()
        if reload_db(self.timestamp, db_ts):
            self.config = {}
            self.resolver = {}
            self.realm = {}
            self.default_realm = None
            for sysconf in Config.query.all():
                self.config[sysconf.Key] = {
                    "Value": sysconf.Value,
                    "Type": sysconf.Type,
                    "Description": sysconf.Description,
                }
            for resolver in Resolver.query.all():
                resolverdef = {"type": resolver.rtype, "resolvername": resolver.name}
                data = {}
                for rconf in resolver.config_list:
                    if rconf.Type == "password":
                        value = decryptPassword(rconf.Value, convert_unicode=True)
                    else:
                        value = rconf.Value
                    data[rconf.Key] = value
                resolverdef["data"] = data
                self.resolver[resolver.name] = resolverdef

            for realm in Realm.query.all():
                if realm.default:
                    self.default_realm = realm.name
                realmdef = {
                    "option": realm.option,
                    "default": realm.default,
                    "resolver": [],
                }
                for x in realm.resolver_list:
                    realmdef["resolver"].append(
                        {
                            "priority": x.priority,
                            "name": x.resolver.name,
                            "type": x.resolver.rtype,
                        }
                    )
                self.realm[realm.name] = realmdef

        self.timestamp = datetime.datetime.now()