page.py
author Tero Marttila <terom@fixme.fi>
Sun, 14 Sep 2014 12:56:10 +0300
changeset 55 eabf155f5327
parent 47 2cceeb731950
permissions -rw-r--r--
pages proejcts/degal: typofix

"""
    Handling page requests
"""

# for filesystem ops
import os, os.path
import time

from qmsk.web import http, handler, config

import menu

class PageError (http.ResponseError) :
    """
        Error looking up/handling a page
    """

    pass

# XXX: should inherit from PageInfo
class Page (handler.RequestHandler) :
    """
        This object represents the information about our attempt to render some specific page
    """

    def __init__ (self, fs, url, path, basename, url_tail, charset='utf8') :
        """
            Initialize the page at the given location
            
            @param fs the FilesysteMapper
            @param url the URL leading to this page
            @param path the filesystem path to this page's file
            @param basename the filesystem name of this page's file, without the file extension
            @param url_trail trailing URL for this page
            @param charset file charset
        """
        
        # store
        self.fs = fs
        self.url = url
        self.path = path
        self.basename = basename
        self.url_tail = url_tail
        self.charset = charset

        # sub-init
        self._init()

    def _init (self) :
        """
            Do initial data loading, etc
        """
        
        pass

    @property
    def title (self) :
        """
            Return the page's title

            Defaults to the retreiving the page title from page_list, or basename in Titlecase.
        """
        
        # lookup in PageTree
        page_info = self.fs.tree.get_page(self.url)
        
        # fallback to titlecase
        if page_info :
            title = page_info.title

        else :
            title = self.basename.title()

        return title
    
    @property
    def content (self) :
        """
            Return the page content as a string
        """

        abstract
    
    @property
    def modified (self) :
        """
            Returns the page modification timestamp
        """
        
        # stat
        timestamp = os.stat(self.path).st_mtime

        return time.strftime(config.DATETIME_FMT, time.gmtime(timestamp))
    
    def handle_request (self, request) :
        """
            Renders the fs's layout template with this page + menu
        """

        # render the template
        return self.fs.templates.render_to_response("layout",
            req             = request,
            page            = self,
            menu            = menu.Menu(self.fs, self),
        )

class HTMLPage (Page) :
    """
        A simple .html page that's just passed through directly
    """

    @property
    def content (self) :
        """
            Opens the .html file, reads and returns contents
        """

        return open(self.path, 'rb').read().decode(self.charset)

class TemplatePage (Page) :
    """
        A template that's rendered using our template library
    """
    
    @property
    def content (self) :
        """
            Loads the .tmpl file, and renders it
        """

        return self.fs.templates.render_file(self.path,
            page_tree   = self.fs.tree,
        )