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@42: terom@41: # load templates from here terom@47: templates = template.TemplateLoader("templates", terom@54: _helper_class = helpers.Helpers, terom@42: urls = urls, terom@42: channel_list = channels.channel_list, 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@55: return http.Redirect(urls.channel_view.build(request, channel=channel)) terom@29: terom@59: @preferences.handler(prefs.formatter) terom@59: def channel_view (request, channel, count, formatter) : terom@29: """ terom@29: The main channel view page, display the most important info, and all requisite links terom@29: """ terom@50: terom@50: # get latest events terom@50: lines = channel.source.get_latest(count) terom@43: terom@51: # lines terom@50: lines = formatter.format_html(lines) terom@43: terom@50: return templates.render_to_response("channel_view", terom@42: req = request, terom@62: prefs = request.prefs, terom@41: channel = channel, terom@43: count = count, terom@50: lines = lines, terom@40: ) terom@40: terom@41: def channel_last (request, channel, count, format) : terom@29: """ terom@29: Display the last x lines of channel messages in various formats terom@29: """ terom@29: terom@41: if format == 'txt' : terom@50: # XXX: formatting terom@50: # return http.Response('\n'.join(str(channel.source.get_latest(count))), 'text/plain') terom@50: pass terom@50: terom@56: elif format == 'html' : terom@56: pass terom@56: terom@41: else : terom@41: raise http.ResponseError("Unknown filetype %r" % format) terom@29: 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@54: days = 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@59: @preferences.handler(prefs.formatter, prefs.timezone) terom@53: def channel_date (request, channel, date, formatter, timezone) : terom@50: """ terom@50: Display all log data for the given date terom@50: """ terom@50: terom@53: # fix date timezone terom@53: date = date.replace(tzinfo=timezone) terom@50: terom@50: # get latest events terom@50: lines = channel.source.get_date(date) 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@50: lines = lines, terom@50: ) terom@50: terom@63: @preferences.handler(prefs.formatter) terom@63: def channel_search (request, channel, formatter, q=None) : terom@36: """ terom@36: Display the search form for the channel for GET, or do the search for POST terom@36: """ terom@36: terom@63: # got a search query? terom@63: if q : terom@63: # do search terom@63: lines = channel.source.get_search(q) terom@36: terom@63: # format terom@63: lines = formatter.format_html(lines) 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@63: query = q, terom@63: lines = lines, terom@63: ) terom@63: terom@63: terom@63: