utils.py
author Tero Marttila <terom@fixme.fi>
Mon, 09 Feb 2009 03:05:43 +0200
changeset 53 8103d18907a0
parent 51 07ca28f3a9f2
child 72 5ade0288f2ec
permissions -rw-r--r--
add some user-preferences support (e.g. timezone, time formats)
"""
    Miscellaneous things
"""

import datetime

from qmsk.web.urltree import URLType

class URLChannelName (URLType) :
    """
        Handle LogChannel names in URLs. Deals with instances of LogChannel
    """

    def __init__ (self, channels) :
        """
            Use the given { name -> LogChannel } dict
        """

        self.channels = channels
    
    def parse (self, chan_name) :
        """
            chan_name -> LogChannel
        """

        return self.channels[chan_name]

    def build (self, chan) :
        """
            LogChannel -> chan_name
        """

        return chan.id

class URLDateType (URLType) :
    """
        Handle dates in URLs as naive datetime objects (with indeterminate time info)
    """

    def __init__ (self, date_fmt="%Y-%m-%d") :
        """
            Format/parse dates using the given format
        """

        self.date_fmt = date_fmt
    
    def parse (self, date_str) :
        """
            date_str -> naive datetime.datetime
        """
        
        return datetime.datetime.strptime(date_str, self.date_fmt)
    
    def build (self, date) :
        """
            datetime.date -> date_str
        """

        return date.strftime(self.date_fmt)