view MoinMoin/util/ @ 102:a54cebaab73e

move make / parse query string functions to wikiutil, make unicode-safe imported from: moin--main--1.5--patch-104
author Thomas Waldmann <>
date Sat, 15 Oct 2005 18:03:12 +0000
parents 2202f548cbb0
children 1fe19d424346
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
    MoinMoin - Helper functions for WWW stuff

    @copyright: 2002 by Jürgen Hermann <>
    @license: GNU GPL, see COPYING for details.

import re
from MoinMoin import config

def isSpiderAgent(request):
    """ Return True if user agent appears to be a spider.
    if not request.cfg.ua_spiders:
        return 0

    ua = request.getUserAgent()
    if not ua:
        return 0

    return, ua, re.I) is not None

def getIntegerInput(request, fieldname, default=None, minval=None, maxval=None):
    """ Get an integer value from a request parameter. If the value
        is out of bounds, it's made to fit into those bounds.

        Returns `default` in case of errors (not a valid integer, or field
        is missing).
        result = int(request.form[fieldname][0])
    except (KeyError, ValueError):
        return default
        if minval is not None:
            result = max(result, minval)
        if maxval is not None:
            result = min(result, maxval)
        return result

def getLinkIcon(request, formatter, scheme):
    """ Get icon for fancy links, or '' if user doesn't want them.
    if scheme in ["mailto", "news", "telnet", "ftp", "file"]:
        icon = scheme
        icon = "www"

    return request.theme.make_icon(icon)

def makeSelection(name, values, selectedval=None):
    """ Make a HTML <select> element named `name` from a value list.
        The list can either be a list of strings, or a list of
        (value, label) tuples.

        `selectedval` is the value that should be pre-selected.
    from MoinMoin.widget import html
    result = html.SELECT(name=name)
    for val in values:
        if not isinstance(val, type(())):
            val = (val, val)
            value=val[0], selected=(val[0] == selectedval))

    return result