Ejemplo n.º 1
0
def route_create():

    # create all the plugin default keys
    settings = _get_settings()
    for plugin in ploader.get_all():
        for s in plugin.settings():
            if s.key not in settings:
                db.session.add(Setting(key=s.key, value=s.default))
    db.session.commit()
    return redirect(url_for('settings.route_view'))
Ejemplo n.º 2
0
def utility_processor():
    def format_timestamp(tmp):
        if not tmp:
            return 'n/a'
        return datetime.datetime.fromtimestamp(tmp).strftime(
            '%Y-%m-%d %H:%M:%S')

    def format_humanize_naturalday(tmp):
        if not tmp:
            return 'n/a'
        return humanize.naturalday(tmp)

    def format_humanize_naturaltime(tmp):
        if not tmp:
            return 'n/a'
        return humanize.naturaltime(tmp.replace(tzinfo=None))

    def format_humanize_intchar(tmp):
        if tmp > 1000000:
            return '%.0fM' % (float(tmp) / 1000000)
        if tmp > 1000:
            return '%.0fK' % (float(tmp) / 1000)
        return tmp

    def format_timedelta_approx(tmp):
        return humanize.naturaltime(tmp).replace(' from now', '')

    def format_size(num, suffix='B'):
        if not isinstance(num, int) and not isinstance(num, int):
            return "???%s???" % num
        for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
            if abs(num) < 1024.0:
                return "%3.1f%s%s" % (num, unit, suffix)
            num /= 1024.0
        return "%.1f%s%s" % (num, 'Yi', suffix)

    def format_iso3166(tmp):
        return iso3166.countries.get(tmp, ['Unknown!'])[0]

    def format_plugin_id(tmp):
        return ploader.get_by_id(tmp)

    def format_html_from_markdown(tmp):
        if not tmp:
            return '<p>None</p>'
        root = _xml_from_markdown(tmp)
        txt = ''
        for n in root:
            if n.tag == 'p':
                txt += '<p>' + html.escape(n.text) + '</p>'
            elif n.tag == 'ul' or n.tag == 'ol':
                txt += '<ul>'
                for c in n:
                    if c.tag == 'li':
                        txt += '<li>' + html.escape(c.text) + '</li>'
                txt += '</ul>'
        return txt

    return dict(format_size=format_size,
                format_humanize_naturalday=format_humanize_naturalday,
                format_humanize_naturaltime=format_humanize_naturaltime,
                format_humanize_intchar=format_humanize_intchar,
                format_timedelta_approx=format_timedelta_approx,
                format_html_from_markdown=format_html_from_markdown,
                format_timestamp=format_timestamp,
                format_iso3166=format_iso3166,
                format_plugin_id=format_plugin_id,
                loader_plugins=sorted(ploader.get_all(),
                                      key=lambda x: x.name()))