terom@29: """ terom@29: Our URL action handlers terom@29: """ terom@29: terom@54: import datetime, calendar, pytz terom@51: terom@46: from qmsk.web import http, template terom@40: terom@50: import urls, channels, helpers terom@53: import preferences as prefs terom@53: from preferences import preferences terom@96: import config, log_search terom@42: terom@41: # load templates from here terom@47: templates = template.TemplateLoader("templates", terom@54: _helper_class = helpers.Helpers, terom@42: urls = urls, terom@73: channel_list = config.LOG_CHANNELS, terom@73: config = config, terom@42: ) terom@40: terom@29: def index (request) : terom@29: """ terom@29: The topmost index page, display a list of available channels, perhaps some general stats terom@29: """ terom@40: terom@42: return templates.render_to_response("index", terom@42: req = request, terom@42: ) terom@42: terom@59: # XXX: fix this namespace crap terom@59: @preferences.handler() terom@59: def preferences_ (request) : terom@59: """ terom@59: Preferences editor terom@59: """ terom@59: terom@59: # POST? terom@59: if request.is_post() : terom@59: # update any modified preferences terom@59: for pref in preferences.pref_list : terom@59: # get+parse new POST'd value terom@59: # XXX: this doesn't postprocess terom@59: new_value = pref.parse(request.get_post(pref.name)) terom@59: terom@59: # update if changed terom@59: if new_value != request.prefs[pref] : terom@59: request.prefs.set(pref.name, new_value) terom@59: terom@59: # render terom@59: return templates.render_to_response("preferences", terom@59: req = request, terom@59: prefs = request.prefs, terom@59: preferences = prefs, terom@59: timezones = pytz.common_timezones, terom@59: ) terom@59: terom@42: def channel_select (request, channel) : terom@42: """ terom@42: Redirect to the appropriate channel_view terom@42: """ terom@42: terom@70: return http.Redirect(urls.channel.build(request, channel=channel)) terom@29: terom@59: @preferences.handler(prefs.formatter) terom@79: def channel_last (request, channel, count, formatter, type=None) : terom@29: """ terom@70: The main channel view page, displaying the most recent lines terom@29: """ terom@79: terom@50: # get latest events terom@50: lines = channel.source.get_latest(count) terom@79: terom@79: # we can render in various modes... terom@79: if not type : terom@79: # normal HTML terom@79: lines = formatter.format_html(lines) terom@43: terom@79: return templates.render_to_response("channel_last", terom@79: req = request, terom@79: prefs = request.prefs, terom@79: channel = channel, terom@79: count = count, terom@79: lines = lines, terom@79: ) terom@79: terom@79: elif type == 'txt' : terom@79: # plaintext terom@79: lines = formatter.format_txt(lines) terom@79: terom@79: # build data terom@79: data = '\n'.join(data for line, data in lines) terom@79: terom@79: return http.Response(data, 'text/plain') terom@79: terom@79: elif type == 'png' : terom@79: # PNG image terom@79: png_data = formatter.format_png(lines) terom@79: terom@79: return http.Response(png_data, 'image/png', charset=None) terom@80: terom@80: elif type == 'rss' : terom@80: # RSS feed terom@80: rss_data = formatter.format_rss(lines) terom@80: terom@80: return http.Response(rss_data, 'application/rss+xml') terom@79: terom@79: else : terom@79: raise http.ResponseError("Unrecognized type: %r" % (type, )) terom@40: terom@78: @preferences.handler(prefs.formatter, prefs.timezone, prefs.count) terom@78: def channel_link (request, channel, timestamp, formatter, timezone, count) : terom@72: """ terom@72: Display channel_date for specific UTC timestamp terom@72: """ terom@72: terom@72: # convert timestamp to user's timezone terom@72: timestamp = timestamp.astimezone(timezone) terom@72: terom@78: # get correct day's correct page of lines terom@78: page, max, lines = channel.source.get_date_paged(timestamp, count) terom@72: terom@72: # lines terom@72: lines = formatter.format_html(lines) terom@72: terom@72: # render terom@72: return templates.render_to_response("channel_date", terom@72: req = request, terom@72: prefs = request.prefs, terom@72: channel = channel, terom@72: date = timestamp, terom@78: page = page, terom@78: count = count, terom@78: max = max, terom@72: lines = lines, terom@72: ) terom@72: terom@59: @preferences.handler(prefs.timezone) terom@54: def channel_calendar (request, channel, year, month, timezone) : terom@51: """ terom@54: Display a list of avilable logs for some month terom@51: """ terom@51: terom@54: # current date as default terom@54: now = timezone.localize(datetime.datetime.now()) terom@54: terom@54: # target year/month terom@54: target = timezone.localize(datetime.datetime( terom@54: year = year if year else now.year, terom@54: month = month if month else now.month, terom@54: day = 1 terom@54: )) terom@54: terom@54: # get set of days available terom@83: days = set(channel.source.get_month_days(target)) terom@54: terom@54: # display calendar terom@54: return templates.render_to_response("channel_calendar", terom@54: req = request, terom@62: prefs = request.prefs, terom@54: channel = channel, terom@54: calendar = calendar.Calendar(), terom@54: month = target.date(), terom@54: days = days, terom@54: ) terom@51: terom@76: @preferences.handler(prefs.formatter, prefs.timezone, prefs.count) terom@77: def channel_date (request, channel, date, formatter, timezone, count, page=1) : terom@50: """ terom@50: Display all log data for the given date terom@50: """ terom@76: terom@53: # fix date timezone terom@53: date = date.replace(tzinfo=timezone) terom@50: terom@78: # get that day's events, either paged or not terom@76: if page : terom@76: page, max, lines = channel.source.get_date_paged(date, count, page) terom@76: terom@76: else : terom@76: lines = channel.source.get_date(date) terom@76: max = None terom@50: terom@51: # lines terom@50: lines = formatter.format_html(lines) terom@50: terom@63: # render terom@50: return templates.render_to_response("channel_date", terom@50: req = request, terom@62: prefs = request.prefs, terom@50: channel = channel, terom@50: date = date, terom@76: page = page, terom@76: count = count, terom@76: max = max, terom@50: lines = lines, terom@50: ) terom@50: terom@73: @preferences.handler(prefs.formatter, prefs.count) terom@75: def channel_search (request, channel, formatter, count, q=None, page=1, max=1) : terom@36: """ terom@75: Display the search form for the channel for GET, or do the search for POST. terom@36: """ terom@36: terom@75: # calculate skip offset from page/count terom@76: skip = (page - 1) * count terom@75: terom@63: # got a search query? terom@63: if q : terom@74: try : terom@74: # do search terom@96: lines = log_search.get_index().search_simple(channel, q, count, skip) terom@75: terom@75: # update max? terom@75: if max and page > max : terom@75: max = page terom@74: terom@74: except log_search.NoResultsFound : terom@74: # no lines terom@74: lines = None terom@36: terom@74: else : terom@74: # format terom@74: lines = formatter.format_html(lines, full_timestamps=True) terom@63: terom@63: else : terom@63: lines = None terom@63: terom@63: # render terom@63: return templates.render_to_response("channel_search", terom@63: req = request, terom@63: prefs = request.prefs, terom@63: channel = channel, terom@65: search_query = q, terom@73: count = count, terom@75: page = page, terom@73: skip = skip, terom@73: max = max, terom@63: lines = lines, terom@63: ) terom@63: