Example #1
0
def response(context, req):
    """The user selected a wiki.  Display a list of reports that can be
       run on this wiki.
    """
    try:
        wiki = req.params['wiki']
        t = req.prepare_template(SelectReport)

        # The user can input either a domain or a dbname
        # in the wiki selection box; we accept either.
        try:
            t.wiki = repdb.find_wiki_domain(context, wiki)
        except ValueError:
            if wiki[-2:] != "_p":
                wiki += "_p"
            t.wiki = repdb.find_wiki(context, wiki)
            wiki = t.wiki['domain']

        cats = {}
        for k in context.reports.keys():
            r = context.reports[k]
            if not r.runs_on(t.wiki['domain']):
                continue
            catname = req.i18n.report_category(r)
            if not cats.has_key(catname):
                cats[catname] = []
            cats[catname].append(r)
        t.categories = cats
        cache = QueryCache(context)
        t.cache_status = cache.check_cache(t.wiki['dbname'], context.reports.values())
        output = str(t)

        req.start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8')])
        yield output
    except Exception, value:
        yield req.error(str(value))
Example #2
0
def response(context, req):
    try:
        key = req.params['report']
        report = context.reports[key]
    except ValueError:
        raise ValueError('no such report')
    
    dbname = req.params['wiki']
    try:
        format = req.params['format']
    except KeyError:
        format = 'html'
    
    Reporter = {'html': Report, 'wikitable': WikitextReport, 'wikilist': WikitextReport, 'json': JSONReport}[format]

    wiki = repdb.find_wiki(context, dbname)
    namespaces = repdb.get_namespaces(context, dbname)
    
    try:
        columns = [x.lower() for x in req.params['columns'].split("|")]
    except KeyError:
        columns = None

    # Extract all variables from the request.  Make sure all the
    # required variables were specified; if not, emit a form
    # where the user can specify them.
    variables = {}
    for k in req.params.keys():
        if k[0:4] != "var_":
            continue
        varname = k[4:]
        value = req.params[k]
        variables[varname] = value
    
    for v in report.variables:
        if not variables.has_key(v.name):
            # missing a variable
            t = req.prepare_template(QueryVariables)
            t.variables = report.variables
            t.wiki = wiki
            t.report = report

            output = str(t)
            req.start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8')])
            yield output
            return

    cache = QueryCache(context)
    cache_result = cache.execute(report, dbname, variables)
    status = cache_result['status']
    if status == 'unavailable' or status == 'first':
        t = req.prepare_template(CachedReportUnavailable)
        t.status = status
        if status == 'first':
            t.query_runtime = cache_result['query runtime']

        t.report = report
        output = str(t)
        req.start_response('200 OK', [('Content-Type', 'text/html; charset=UTF-8')])
        yield output
        return

    age = cache_result['age']
    result = cache_result['result']
    fields = report.fields

    t = req.prepare_template(Reporter)
    contenttype = {Report: 'text/html', WikitextReport: 'text/plain', JSONReport: 'application/json'}[Reporter] + '; charset=UTF-8'
    t.last_run_duration = cache_result.get('last_run_duration', None)
    t.age = age
    t.report = report
    t.wiki = wiki
    t.status = status
    t.format = format
    t.intro = "nointro" not in req.params

    if status == 'cold':
        t.query_runtime = cache_result['query runtime']

    # Generate a list of variables + values for the report page
    t.variables = {}
    for v in report.variables:
        t.variables[v.title] = variables[v.name]

    t.headers = []
    for f in fields:
        t.headers.append(f.title)
        
    t.rows = []
    for r in result:
        row = []
        for f in fields:
            if columns is None or f.title.lower() in columns:
                r['__namespaces__'] = namespaces
                r['__dbname__'] = dbname
                r['__domain__'] = wiki['domain']
                row.append(Field(f, r, req.i18n))
        t.rows.append(row)

    output = t.respond().encode('utf-8')
    req.start_response('200 OK', [('Content-Type', contenttype)])
    yield output