split out helpers._render_type, and add support for ?type= to various other handlers... still needs more work, though
authorTero Marttila <terom@fixme.fi>
Wed, 11 Feb 2009 21:50:03 +0200
changeset 108 d0aca7894fc5
parent 107 67f48e288102
child 109 ca82d0fee336
split out helpers._render_type, and add support for ?type= to various other handlers... still needs more work, though
handlers.py
log_formatter.py
urls.py
--- 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,
+        )
 
--- a/log_formatter.py	Wed Feb 11 21:49:35 2009 +0200
+++ b/log_formatter.py	Wed Feb 11 21:50:03 2009 +0200
@@ -65,7 +65,7 @@
         # full timestamps?
         if full_timestamp :
             # XXX: let the user define a 'datetime' format instead?
-            timestamp_fmt = self.date_fmt + self.timestamp_fmt
+            timestamp_fmt = self.date_fmt + ' ' + self.timestamp_fmt
 
         else :
             timestamp_fmt = self.timestamp_fmt
--- a/urls.py	Wed Feb 11 21:49:35 2009 +0200
+++ b/urls.py	Wed Feb 11 21:50:03 2009 +0200
@@ -36,10 +36,10 @@
 channel_select      = url('/channel_select/?channel:cid',                                   handlers.channel_select                     )
 channel             = url('/channels/{channel:cid}',                                        handlers.channel_last,      count=20        )
 channel_last        = url('/channels/{channel:cid}/last/{count:int=100}/{type=}',           handlers.channel_last                       )
-channel_link        = url('/channels/{channel:cid}/link/{timestamp:ts}',                    handlers.channel_link                       )
+channel_link        = url('/channels/{channel:cid}/link/{timestamp:ts}/?type=',             handlers.channel_link                       )
 channel_calendar    = url('/channels/{channel:cid}/calendar/{year:int=0}/{month:int=0}',    handlers.channel_calendar                   )
-channel_date        = url('/channels/{channel:cid}/date/{date:date}/?page:int=1',           handlers.channel_date                       )
-channel_search      = url('/channels/{channel:cid}/search/?q=&page:int=1&max:int=1',        handlers.channel_search                     )
+channel_date        = url('/channels/{channel:cid}/date/{date:date}/?page:int=1&type=',     handlers.channel_date                       )
+channel_search      = url('/channels/{channel:cid}/search/?q=&page:int=1&max:int=1&type=',  handlers.channel_search                     )
 
 # mapper
 mapper = urltree.URLTree(urls)