handlers.py
changeset 108 d0aca7894fc5
parent 96 d30c88e89a7e
child 111 95c0c49d76aa
equal deleted inserted replaced
107:67f48e288102 108:d0aca7894fc5
    17     urls            = urls,
    17     urls            = urls,
    18     channel_list    = config.LOG_CHANNELS,
    18     channel_list    = config.LOG_CHANNELS,
    19     config          = config,
    19     config          = config,
    20 )
    20 )
    21 
    21 
       
    22 # return a http.Response for the given text in the given format
       
    23 def _render_type (request, channel, lines, type) :
       
    24     """
       
    25         Render the given LogLines as a http.Response in the given format, which is one of:
       
    26             html    - XXX: not supported
       
    27             txt     - Plaintext
       
    28             png     - PNG image
       
    29             rss     - RSS feed
       
    30     """
       
    31 
       
    32     # load related preferences
       
    33     formatter = request.prefs['formatter']
       
    34 
       
    35     # we can render in various modes...
       
    36     if type in ('html', None) :
       
    37         xxx
       
    38 
       
    39     elif type == 'txt' :
       
    40         # plaintext
       
    41         lines = formatter.format_txt(lines)
       
    42 
       
    43         # build data
       
    44         data = '\n'.join(data for line, data in lines)
       
    45 
       
    46         return http.Response(data, 'text/plain')
       
    47 
       
    48     elif type == 'png' :
       
    49         # PNG image
       
    50         png_data = formatter.format_png(lines)
       
    51 
       
    52         return http.Response(png_data, 'image/png', charset=None)
       
    53     
       
    54     elif type == 'rss' :
       
    55         # RSS feed
       
    56         rss_data = formatter.format_rss(lines)
       
    57         
       
    58         # XXX: fix to render as unicode?
       
    59         return http.Response(rss_data, 'application/rss+xml', charset=None)
       
    60 
       
    61     else :
       
    62         raise http.ResponseError("Unrecognized type: %r" % (type, ))
       
    63 
       
    64 
    22 def index (request) :
    65 def index (request) :
    23     """
    66     """
    24         The topmost index page, display a list of available channels, perhaps some general stats
    67         The topmost index page, display a list of available channels, perhaps some general stats
    25     """
    68     """
    26     
    69     
    69     """
   112     """
    70  
   113  
    71     # get latest events
   114     # get latest events
    72     lines = channel.source.get_latest(count)
   115     lines = channel.source.get_latest(count)
    73    
   116    
    74     # we can render in various modes...
   117     # type?
    75     if not type :
   118     if type :
    76         # normal HTML
   119         # other format
       
   120         return _render_type(request, channel, lines, type)
       
   121 
       
   122     else :
       
   123         # format HTML
    77         lines = formatter.format_html(lines)
   124         lines = formatter.format_html(lines)
    78 
   125 
       
   126         # render page
    79         return templates.render_to_response("channel_last",
   127         return templates.render_to_response("channel_last",
    80             req             = request,
   128             req             = request,
    81             prefs           = request.prefs,
   129             prefs           = request.prefs,
    82             channel         = channel,
   130             channel         = channel,
    83             count           = count,
   131             count           = count,
    84             lines           = lines,
   132             lines           = lines,
    85         )
   133         )
    86     
       
    87     elif type == 'txt' :
       
    88         # plaintext
       
    89         lines = formatter.format_txt(lines)
       
    90 
       
    91         # build data
       
    92         data = '\n'.join(data for line, data in lines)
       
    93 
       
    94         return http.Response(data, 'text/plain')
       
    95 
       
    96     elif type == 'png' :
       
    97         # PNG image
       
    98         png_data = formatter.format_png(lines)
       
    99 
       
   100         return http.Response(png_data, 'image/png', charset=None)
       
   101     
       
   102     elif type == 'rss' :
       
   103         # RSS feed
       
   104         rss_data = formatter.format_rss(lines)
       
   105 
       
   106         return http.Response(rss_data, 'application/rss+xml')
       
   107 
       
   108     else :
       
   109         raise http.ResponseError("Unrecognized type: %r" % (type, ))
       
   110 
   134 
   111 @preferences.handler(prefs.formatter, prefs.timezone, prefs.count)
   135 @preferences.handler(prefs.formatter, prefs.timezone, prefs.count)
   112 def channel_link (request, channel, timestamp, formatter, timezone, count) :
   136 def channel_link (request, channel, timestamp, formatter, timezone, count) :
   113     """
   137     """
   114         Display channel_date for specific UTC timestamp
   138         Display channel_date for specific UTC timestamp
   118     timestamp = timestamp.astimezone(timezone)
   142     timestamp = timestamp.astimezone(timezone)
   119 
   143 
   120     # get correct day's correct page of lines
   144     # get correct day's correct page of lines
   121     page, max, lines = channel.source.get_date_paged(timestamp, count)
   145     page, max, lines = channel.source.get_date_paged(timestamp, count)
   122 
   146 
   123     # lines
   147     # type?
   124     lines = formatter.format_html(lines)
   148     if type :
   125 
   149         # special type
   126     # render
   150         return _render_type(request, channel, lines, type)
   127     return templates.render_to_response("channel_date",
   151     
   128         req             = request,
   152     else :
   129         prefs           = request.prefs,
   153         # format HTML
   130         channel         = channel,
   154         lines = formatter.format_html(lines)
   131         date            = timestamp,
   155 
   132         page            = page,
   156         # render
   133         count           = count,
   157         return templates.render_to_response("channel_date",
   134         max             = max,
   158             req             = request,
   135         lines           = lines,
   159             prefs           = request.prefs,
   136     )
   160             channel         = channel,
       
   161             date            = timestamp,
       
   162             page            = page,
       
   163             count           = count,
       
   164             max             = max,
       
   165             lines           = lines,
       
   166         )
   137 
   167 
   138 @preferences.handler(prefs.timezone)
   168 @preferences.handler(prefs.timezone)
   139 def channel_calendar (request, channel, year, month, timezone) :
   169 def channel_calendar (request, channel, year, month, timezone) :
   140     """
   170     """
   141         Display a list of avilable logs for some month
   171         Display a list of avilable logs for some month
   163         month           = target.date(),
   193         month           = target.date(),
   164         days            = days,
   194         days            = days,
   165     )
   195     )
   166 
   196 
   167 @preferences.handler(prefs.formatter, prefs.timezone, prefs.count)
   197 @preferences.handler(prefs.formatter, prefs.timezone, prefs.count)
   168 def channel_date (request, channel, date, formatter, timezone, count, page=1) :
   198 def channel_date (request, channel, date, formatter, timezone, count, page=1, type=None) :
   169     """
   199     """
   170         Display all log data for the given date
   200         Display all log data for the given date
   171     """
   201     """
   172 
   202 
   173     # fix date timezone
   203     # fix date timezone
   179         
   209         
   180     else :
   210     else :
   181         lines = channel.source.get_date(date)
   211         lines = channel.source.get_date(date)
   182         max = None
   212         max = None
   183 
   213 
   184     # lines
   214     # type?
   185     lines = formatter.format_html(lines)
   215     if type :
   186 
   216         # special type
   187     # render
   217         return _render_type(request, channel, lines, type)
   188     return templates.render_to_response("channel_date",
   218     
   189         req             = request,
   219     else :
   190         prefs           = request.prefs,
   220         # lines
   191         channel         = channel,
   221         lines = formatter.format_html(lines)
   192         date            = date,
   222 
   193         page            = page,
   223         # render page
   194         count           = count,
   224         return templates.render_to_response("channel_date",
   195         max             = max,
   225             req             = request,
   196         lines           = lines,
   226             prefs           = request.prefs,
   197     )
   227             channel         = channel,
       
   228             date            = date,
       
   229             page            = page,
       
   230             count           = count,
       
   231             max             = max,
       
   232             lines           = lines,
       
   233         )
   198 
   234 
   199 @preferences.handler(prefs.formatter, prefs.count)
   235 @preferences.handler(prefs.formatter, prefs.count)
   200 def channel_search (request, channel, formatter, count, q=None, page=1, max=1) :
   236 def channel_search (request, channel, formatter, count, q=None, page=1, max=1, type=None) :
   201     """
   237     """
   202         Display the search form for the channel for GET, or do the search for POST.
   238         Display the search form for the channel for GET, or do the search for POST.
   203     """
   239     """
   204 
   240 
   205     # calculate skip offset from page/count
   241     # calculate skip offset from page/count
   214             # update max?
   250             # update max?
   215             if max and page > max :
   251             if max and page > max :
   216                 max = page
   252                 max = page
   217         
   253         
   218         except log_search.NoResultsFound :
   254         except log_search.NoResultsFound :
   219             # no lines
   255             # no results
   220             lines = None
   256             lines = None
   221 
   257 
   222         else :
   258     else :
       
   259         # just display the search form
       
   260         lines = None
       
   261  
       
   262     # type?
       
   263     if type and lines :
       
   264         # special type
       
   265         return _render_type(request, channel, lines, type)
       
   266     
       
   267     else :
       
   268         # format lines to HTML if any
       
   269         if lines :
   223             # format
   270             # format
   224             lines = formatter.format_html(lines, full_timestamps=True)
   271             lines = formatter.format_html(lines, full_timestamps=True)
   225 
   272 
   226     else :
   273         # render page
   227         lines = None
   274         return templates.render_to_response("channel_search",
   228     
   275             req             = request,
   229     # render
   276             prefs           = request.prefs,
   230     return templates.render_to_response("channel_search",
   277             channel         = channel,
   231         req             = request,
   278             search_query    = q,
   232         prefs           = request.prefs,
   279             count           = count,
   233         channel         = channel,
   280             page            = page,
   234         search_query    = q,
   281             skip            = skip,
   235         count           = count,
   282             max             = max,
   236         page            = page,
   283             lines           = lines,
   237         skip            = skip,
   284         )
   238         max             = max,
   285 
   239         lines           = lines,
       
   240     )
       
   241