handlers.py
changeset 108 d0aca7894fc5
parent 96 d30c88e89a7e
child 111 95c0c49d76aa
--- a/handlers.py	Wed Feb 11 21:49:35 2009 +0200
+++ b/handlers.py	Wed Feb 11 21:50:03 2009 +0200
@@ -19,6 +19,49 @@
     config          = config,
 )
 
+# return a http.Response for the given text in the given format
+def _render_type (request, channel, lines, type) :
+    """
+        Render the given LogLines as a http.Response in the given format, which is one of:
+            html    - XXX: not supported
+            txt     - Plaintext
+            png     - PNG image
+            rss     - RSS feed
+    """
+
+    # load related preferences
+    formatter = request.prefs['formatter']
+
+    # we can render in various modes...
+    if type in ('html', None) :
+        xxx
+
+    elif type == 'txt' :
+        # plaintext
+        lines = formatter.format_txt(lines)
+
+        # build data
+        data = '\n'.join(data for line, data in lines)
+
+        return http.Response(data, 'text/plain')
+
+    elif type == 'png' :
+        # PNG image
+        png_data = formatter.format_png(lines)
+
+        return http.Response(png_data, 'image/png', charset=None)
+    
+    elif type == 'rss' :
+        # RSS feed
+        rss_data = formatter.format_rss(lines)
+        
+        # XXX: fix to render as unicode?
+        return http.Response(rss_data, 'application/rss+xml', charset=None)
+
+    else :
+        raise http.ResponseError("Unrecognized type: %r" % (type, ))
+
+
 def index (request) :
     """
         The topmost index page, display a list of available channels, perhaps some general stats
@@ -71,11 +114,16 @@
     # get latest events
     lines = channel.source.get_latest(count)
    
-    # we can render in various modes...
-    if not type :
-        # normal HTML
+    # type?
+    if type :
+        # other format
+        return _render_type(request, channel, lines, type)
+
+    else :
+        # format HTML
         lines = formatter.format_html(lines)
 
+        # render page
         return templates.render_to_response("channel_last",
             req             = request,
             prefs           = request.prefs,
@@ -83,30 +131,6 @@
             count           = count,
             lines           = lines,
         )
-    
-    elif type == 'txt' :
-        # plaintext
-        lines = formatter.format_txt(lines)
-
-        # build data
-        data = '\n'.join(data for line, data in lines)
-
-        return http.Response(data, 'text/plain')
-
-    elif type == 'png' :
-        # PNG image
-        png_data = formatter.format_png(lines)
-
-        return http.Response(png_data, 'image/png', charset=None)
-    
-    elif type == 'rss' :
-        # RSS feed
-        rss_data = formatter.format_rss(lines)
-
-        return http.Response(rss_data, 'application/rss+xml')
-
-    else :
-        raise http.ResponseError("Unrecognized type: %r" % (type, ))
 
 @preferences.handler(prefs.formatter, prefs.timezone, prefs.count)
 def channel_link (request, channel, timestamp, formatter, timezone, count) :
@@ -120,20 +144,26 @@
     # get correct day's correct page of lines
     page, max, lines = channel.source.get_date_paged(timestamp, count)
 
-    # lines
-    lines = formatter.format_html(lines)
+    # type?
+    if type :
+        # special type
+        return _render_type(request, channel, lines, type)
+    
+    else :
+        # format HTML
+        lines = formatter.format_html(lines)
 
-    # render
-    return templates.render_to_response("channel_date",
-        req             = request,
-        prefs           = request.prefs,
-        channel         = channel,
-        date            = timestamp,
-        page            = page,
-        count           = count,
-        max             = max,
-        lines           = lines,
-    )
+        # render
+        return templates.render_to_response("channel_date",
+            req             = request,
+            prefs           = request.prefs,
+            channel         = channel,
+            date            = timestamp,
+            page            = page,
+            count           = count,
+            max             = max,
+            lines           = lines,
+        )
 
 @preferences.handler(prefs.timezone)
 def channel_calendar (request, channel, year, month, timezone) :
@@ -165,7 +195,7 @@
     )
 
 @preferences.handler(prefs.formatter, prefs.timezone, prefs.count)
-def channel_date (request, channel, date, formatter, timezone, count, page=1) :
+def channel_date (request, channel, date, formatter, timezone, count, page=1, type=None) :
     """
         Display all log data for the given date
     """
@@ -181,23 +211,29 @@
         lines = channel.source.get_date(date)
         max = None
 
-    # lines
-    lines = formatter.format_html(lines)
+    # type?
+    if type :
+        # special type
+        return _render_type(request, channel, lines, type)
+    
+    else :
+        # lines
+        lines = formatter.format_html(lines)
 
-    # render
-    return templates.render_to_response("channel_date",
-        req             = request,
-        prefs           = request.prefs,
-        channel         = channel,
-        date            = date,
-        page            = page,
-        count           = count,
-        max             = max,
-        lines           = lines,
-    )
+        # render page
+        return templates.render_to_response("channel_date",
+            req             = request,
+            prefs           = request.prefs,
+            channel         = channel,
+            date            = date,
+            page            = page,
+            count           = count,
+            max             = max,
+            lines           = lines,
+        )
 
 @preferences.handler(prefs.formatter, prefs.count)
-def channel_search (request, channel, formatter, count, q=None, page=1, max=1) :
+def channel_search (request, channel, formatter, count, q=None, page=1, max=1, type=None) :
     """
         Display the search form for the channel for GET, or do the search for POST.
     """
@@ -216,26 +252,34 @@
                 max = page
         
         except log_search.NoResultsFound :
-            # no lines
+            # no results
             lines = None
 
-        else :
+    else :
+        # just display the search form
+        lines = None
+ 
+    # type?
+    if type and lines :
+        # special type
+        return _render_type(request, channel, lines, type)
+    
+    else :
+        # format lines to HTML if any
+        if lines :
             # format
             lines = formatter.format_html(lines, full_timestamps=True)
 
-    else :
-        lines = None
-    
-    # render
-    return templates.render_to_response("channel_search",
-        req             = request,
-        prefs           = request.prefs,
-        channel         = channel,
-        search_query    = q,
-        count           = count,
-        page            = page,
-        skip            = skip,
-        max             = max,
-        lines           = lines,
-    )
+        # render page
+        return templates.render_to_response("channel_search",
+            req             = request,
+            prefs           = request.prefs,
+            channel         = channel,
+            search_query    = q,
+            count           = count,
+            page            = page,
+            skip            = skip,
+            max             = max,
+            lines           = lines,
+        )