handlers.py
author Tero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 01:11:05 +0200
changeset 51 07ca28f3a9f2
parent 50 f13cf27a360b
child 53 8103d18907a0
permissions -rw-r--r--
use improved URLConfig/URLType
"""
    Our URL action handlers
"""

import pytz

from qmsk.web import http, template

import urls, channels, helpers

# load templates from here
templates = template.TemplateLoader("templates",
    h               = helpers,
    urls            = urls,
    channel_list    = channels.channel_list,
)

def index (request) :
    """
        The topmost index page, display a list of available channels, perhaps some general stats
    """
    
    return templates.render_to_response("index",
        req             = request,
    )

def channel_select (request, channel) :
    """
        Redirect to the appropriate channel_view
    """
   
    return http.Redirect(urls.channel_view.build(request, channel=channel.id))

def channel_view (request, channel, count, formatter) :
    """
        The main channel view page, display the most important info, and all requisite links
    """
    
    # get latest events
    lines = channel.source.get_latest(count)

    # formatter
    formatter = formatter(pytz.utc, '%H:%M:%S')
    
    # lines
    lines = formatter.format_html(lines)

    return templates.render_to_response("channel_view",
        req             = request,
        channel         = channel,
        count           = count,
        formatter       = formatter,
        lines           = lines,
    )

def channel_last (request, channel, count, format) :
    """
        Display the last x lines of channel messages in various formats
    """

    if format == 'txt' :
        # XXX: formatting
#        return http.Response('\n'.join(str(channel.source.get_latest(count))), 'text/plain')
         pass

    else :
        raise http.ResponseError("Unknown filetype %r" % format)

def channel_calendar (request, channel) :
    """
        Display a list of avilable logs for some days
    """

    pass

def channel_date (request, channel, date, formatter) :
    """
        Display all log data for the given date
    """
    
    # XXX: fix date timezone
    date = date.replace(tzinfo=pytz.utc)

    # get latest events
    lines = channel.source.get_date(date)

    # formatter
    formatter = formatter(pytz.utc, '%H:%M:%S')
    
    # lines
    lines = formatter.format_html(lines)

    return templates.render_to_response("channel_date",
        req             = request,
        channel         = channel,
        formatter       = formatter,
        date            = date,
        lines           = lines,
    )

def channel_search (request, channel, q) :
    """
        Display the search form for the channel for GET, or do the search for POST
    """

    pass