"""
Our URL action handlers
"""
import datetime, calendar, pytz
from qmsk.web import http, template
import urls, channels, helpers
import preferences as prefs
from preferences import preferences
# load templates from here
templates = template.TemplateLoader("templates",
_helper_class = helpers.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,
)
# XXX: fix this namespace crap
@preferences.handler()
def preferences_ (request) :
"""
Preferences editor
"""
# POST?
if request.is_post() :
# update any modified preferences
for pref in preferences.pref_list :
# get+parse new POST'd value
# XXX: this doesn't postprocess
new_value = pref.parse(request.get_post(pref.name))
# update if changed
if new_value != request.prefs[pref] :
request.prefs.set(pref.name, new_value)
# render
return templates.render_to_response("preferences",
req = request,
prefs = request.prefs,
preferences = prefs,
timezones = pytz.common_timezones,
)
def channel_select (request, channel) :
"""
Redirect to the appropriate channel_view
"""
return http.Redirect(urls.channel_view.build(request, channel=channel))
@preferences.handler(prefs.formatter)
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)
# lines
lines = formatter.format_html(lines)
return templates.render_to_response("channel_view",
req = request,
prefs = request.prefs,
channel = channel,
count = count,
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
elif format == 'html' :
pass
else :
raise http.ResponseError("Unknown filetype %r" % format)
@preferences.handler(prefs.timezone)
def channel_calendar (request, channel, year, month, timezone) :
"""
Display a list of avilable logs for some month
"""
# current date as default
now = timezone.localize(datetime.datetime.now())
# target year/month
target = timezone.localize(datetime.datetime(
year = year if year else now.year,
month = month if month else now.month,
day = 1
))
# get set of days available
days = channel.source.get_month_days(target)
# display calendar
return templates.render_to_response("channel_calendar",
req = request,
prefs = request.prefs,
channel = channel,
calendar = calendar.Calendar(),
month = target.date(),
days = days,
)
@preferences.handler(prefs.formatter, prefs.timezone)
def channel_date (request, channel, date, formatter, timezone) :
"""
Display all log data for the given date
"""
# fix date timezone
date = date.replace(tzinfo=timezone)
# get latest events
lines = channel.source.get_date(date)
# lines
lines = formatter.format_html(lines)
# render
return templates.render_to_response("channel_date",
req = request,
prefs = request.prefs,
channel = channel,
date = date,
lines = lines,
)
@preferences.handler(prefs.formatter)
def channel_search (request, channel, formatter, q=None) :
"""
Display the search form for the channel for GET, or do the search for POST
"""
# got a search query?
if q :
# do search
lines = channel.source.get_search(q)
# format
lines = formatter.format_html(lines)
else :
lines = None
# render
return templates.render_to_response("channel_search",
req = request,
prefs = request.prefs,
channel = channel,
query = q,
lines = lines,
)