sites/www.qmsk.net/page.py
changeset 46 185504387370
parent 45 e94ab812c0c8
child 47 3d59c9eeffaa
equal deleted inserted replaced
45:e94ab812c0c8 46:185504387370
     1 
       
     2 """
       
     3     Handling page requests
       
     4 """
       
     5 
       
     6 # for filesystem ops
       
     7 import os, os.path
       
     8 import time
       
     9 
       
    10 from lib import http, handler, template, config
       
    11 
       
    12 import menu
       
    13 
       
    14 class PageError (http.ResponseError) :
       
    15     """
       
    16         Error looking up/handling a page
       
    17     """
       
    18 
       
    19     pass
       
    20 
       
    21 # XXX: should inherit from PageInfo
       
    22 class Page (handler.RequestHandler) :
       
    23     """
       
    24         This object represents the information about our attempt to render some specific page
       
    25     """
       
    26 
       
    27     def __init__ (self, fs, url, path, basename, url_tail, charset='utf8') :
       
    28         """
       
    29             Initialize the page at the given location
       
    30             
       
    31             @param fs the FilesysteMapper
       
    32             @param url the URL leading to this page
       
    33             @param path the filesystem path to this page's file
       
    34             @param basename the filesystem name of this page's file, without the file extension
       
    35             @param url_trail trailing URL for this page
       
    36             @param charset file charset
       
    37         """
       
    38         
       
    39         # store
       
    40         self.fs = fs
       
    41         self.url = url
       
    42         self.path = path
       
    43         self.basename = basename
       
    44         self.url_tail = url_tail
       
    45         self.charset = charset
       
    46 
       
    47         # sub-init
       
    48         self._init()
       
    49 
       
    50     def _init (self) :
       
    51         """
       
    52             Do initial data loading, etc
       
    53         """
       
    54         
       
    55         pass
       
    56 
       
    57     @property
       
    58     def title (self) :
       
    59         """
       
    60             Return the page's title
       
    61 
       
    62             Defaults to the retreiving the page title from page_list, or basename in Titlecase.
       
    63         """
       
    64         
       
    65         # lookup in PageTree
       
    66         page_info = self.fs.tree.get_page(self.url)
       
    67         
       
    68         # fallback to titlecase
       
    69         if page_info :
       
    70             title = page_info.title
       
    71 
       
    72         else :
       
    73             title = self.basename.title()
       
    74 
       
    75         return title
       
    76     
       
    77     @property
       
    78     def content (self) :
       
    79         """
       
    80             Return the page content as a string
       
    81         """
       
    82 
       
    83         abstract
       
    84     
       
    85     @property
       
    86     def modified (self) :
       
    87         """
       
    88             Returns the page modification timestamp
       
    89         """
       
    90         
       
    91         # stat
       
    92         timestamp = os.stat(self.path).st_mtime
       
    93 
       
    94         return time.strftime(config.DATETIME_FMT, time.gmtime(timestamp))
       
    95     
       
    96     def handle_request (self, request) :
       
    97         """
       
    98             Renders the fs's layout template with this page + menu
       
    99         """
       
   100 
       
   101         # render the template
       
   102         response_data = template.TemplateLoader.render_template(self.fs.template,
       
   103             req             = request,
       
   104             page            = self,
       
   105             menu            = menu.Menu(self.fs, self),
       
   106         )
       
   107         
       
   108         # return the response
       
   109         return http.Response(response_data)
       
   110 
       
   111 class HTMLPage (Page) :
       
   112     """
       
   113         A simple .html page that's just passed through directly
       
   114     """
       
   115 
       
   116     @property
       
   117     def content (self) :
       
   118         """
       
   119             Opens the .html file, reads and returns contents
       
   120         """
       
   121 
       
   122         return open(self.path, 'rb').read().decode(self.charset)
       
   123 
       
   124 class TemplatePage (Page) :
       
   125     """
       
   126         A template that's rendered using our template library
       
   127     """
       
   128     
       
   129     @property
       
   130     def content (self) :
       
   131         """
       
   132             Loads the .tmpl file, and renders it
       
   133         """
       
   134 
       
   135         return template.TemplateLoader.render_file(self.path,
       
   136             page_tree   = self.fs.tree,
       
   137         )
       
   138