preferences.py
author Tero Marttila <terom@fixme.fi>
Fri, 13 Feb 2009 00:32:06 +0200
changeset 122 2d75e13397a8
parent 79 43ac75054d5c
child 129 67a30d680f60
permissions -rw-r--r--
merge quickfixes
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Handling user preferences
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
import functools
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
import Cookie
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
from qmsk.web import urltree
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
class Preference (urltree.URLType) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
        A specific preference
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    # the name to use
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    name = None
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    # the default value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    default = None
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    def process (self, preferences, value) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
            Post-process this preference value. This can access the values of other preferences or the request via
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
            the given RequestPreferences. Note that the other items will not be post-processed here.
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
            Defaults to just return value.
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        return value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
class RequestPreferences (object) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        Represents the specific preferences for some request
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    def __init__ (self, preferences, request, value_map=None) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
            Initialize with the given Preferences object, http Request, and list of mapping of raw preference values
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        # store
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        self.preferences = preferences
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        self.request = request
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        # initialize
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        self.values = {}
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        self.set_cookies = {}
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        # load preferences
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        for pref in preferences.pref_list :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
            # got a value for it?
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
            if value_map and pref.name in value_map :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
                # get value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
                value = value_map[pref.name]
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
                # parse it
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
                value = pref.parse(value)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
            else :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
                # use default value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
                value = pref.default
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
            
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
            # add
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
            self.values[pref.name] = value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
        
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        # then post-process using the Preferences
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        self.values = dict(
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
            (name, self.preferences.pref_map[name].process(self, value)) for name, value in self.values.iteritems()
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        )
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    70
    
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
    def get (self, pref) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        """
62
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
    73
            Return the value for the given Preference, or preference name
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
        
62
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
    76
        # Preference -> name
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
    77
        if isinstance(pref, Preference) :
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
    78
            pref = pref.name
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
    79
        
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
    80
        # look up
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
    81
        return self.values[pref]
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    82
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    83
    # support dict-access
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    84
    __getitem__ = get
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    85
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
    def set (self, name, value_obj) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
            Set a new value for the given preference
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
        # sanity-check to make sure we're not setting it twice...
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
        assert name not in self.set_cookies
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
        # encode using the Preference object
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
        value_str = self.preferences.pref_map[name].build(value_obj)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        # update in our dict
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
        self.values[name] = value_obj
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
        # add to set_cookies
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
        self.set_cookies[name] = value_str
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
class Preferences (object) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
        Handle user preferences using cookies
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    def __init__ (self, pref_list) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
            Use the given list of Preference objects
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
        # store
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
        self.pref_list = pref_list
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
        # translate to mapping as well
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
        self.pref_map = dict((pref.name, pref) for pref in pref_list)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
    def load (self, request, ) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
            Load the set of preferences for the given request, and return as a { name -> value } dict
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
        # the dict of values
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
        values = {}
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
        # load the cookies
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
        cookie_data = request.env.get('HTTP_COOKIE')
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
        # got any?
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
        if cookie_data :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
            # parse into a SimpleCookie
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
            cookies = Cookie.SimpleCookie(cookie_data)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
            # update the the values
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
            values.update((morsel.key, morsel.value) for morsel in cookies.itervalues())
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
        
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
        else :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
            cookies = None
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
        # apply any query parameters
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
        for pref in self.pref_list :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
            # look for a query param
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
            value = request.get_arg(pref.name)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
            if value :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
                # override
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
                values[pref.name] = value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
        # build the RequestPreferences object
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
        return cookies, RequestPreferences(self, request, values)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
    def handler (self, *pref_list) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
            Intended to be used as a decorator for a request handler, this will load the give Preferences and pass
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   156
            them to the wrapped handler as keyword arguments, in addition to any others given.
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
        def _decorator (func) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   160
            @functools.wraps(func)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   161
            def _handler (request, **args) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
                # load preferences
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   163
                cookies, prefs = self.load(request)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   164
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   165
                # bind to request.prefs
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   166
                # XXX: better way to do this? :/
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   167
                request.prefs = prefs
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   168
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
                # update args with new ones
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
                args.update(((pref.name, prefs.get(pref)) for pref in pref_list))
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   171
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
                # handle to get response
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
                response = func(request, **args)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
                # set cookies?
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
                if prefs.set_cookies :
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   177
                    if not cookies :
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   178
                        cookies = Cookie.SimpleCookie('')
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   179
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   180
                    # update cookies
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
                    for key, value in prefs.set_cookies.iteritems() :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
                        cookies[key] = value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   184
                    # add headers
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   185
                    for morsel in cookies.itervalues() :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
                        response.add_header('Set-cookie', morsel.OutputString())
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   187
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   188
                return response
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
            
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
            # return wrapped handler
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   191
            return _handler
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   192
        
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   193
        # return decorator...
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   194
        return _decorator
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   196
# now for our defined preferences....
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
import pytz
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   198
import config
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   199
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   200
class TimeFormat (urltree.URLStringType, Preference) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   201
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   202
        Time format
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   204
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   205
    # set name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   206
    name = 'time_format'
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   207
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   208
    # default value
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   209
    default = config.PREF_TIME_FMT_DEFAULT
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
class DateFormat (urltree.URLStringType, Preference) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   213
        Date format
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   214
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   215
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
    # set name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
    name = 'date_format'
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   218
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   219
    # default value
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   220
    default = config.PREF_DATE_FMT_DEFAULT
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   221
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   222
class Timezone (Preference) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   223
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   224
        Timezone
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   225
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   226
    
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   227
    # set name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   228
    name = 'timezone'
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   229
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   230
    # default value is UTC...
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   231
    default = config.PREF_TIMEZONE_DEFAULT
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   232
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   233
    def parse (self, name) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   234
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   235
            tz_name -> pytz.timezone
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   236
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   237
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   238
        return pytz.timezone(name)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   239
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   240
    def build (self, tz) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   241
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   242
            pytz.timezone -> tz_name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   243
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   244
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
        return tz.zone
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   246
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   247
class ImageFont (Preference) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   248
    """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   249
        Font for ImageFormatter
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   250
    """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   251
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   252
    # set name
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   253
    name = 'image_font'
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   254
    
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   255
    def __init__ (self, font_dict, default_name) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   256
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   257
            Use the given { name: (path, title) } dict and default the given name
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   258
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   259
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   260
        self.font_dict = font_dict
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   261
        self.default = self.parse(default_name)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   262
    
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   263
    def parse (self, name) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   264
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   265
            name -> (name, path, title)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   266
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   267
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   268
        path, title = self.font_dict[name]
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   269
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   270
        return name, path, title
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   271
    
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   272
    def build (self, font_info) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   273
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   274
            (name, path, title) -> name
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   275
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   276
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   277
        name, path, title = font_info
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   278
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   279
        return name
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   280
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   281
class ImageFontSize (urltree.URLIntegerType, Preference) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   282
    # set name, default
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   283
    name = 'image_font_size'
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   284
    default = config.PREF_IMAGE_FONT_SIZE_DEFAULT
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   285
    
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   286
    # XXX: params
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   287
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   288
class Formatter (Preference) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   289
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   290
        LogFormatter to use
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   291
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   292
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   293
    # set name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   294
    name = 'formatter'
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   295
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   296
    def __init__ (self, formatters, default) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   297
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   298
            Use the given { name -> class LogFormatter } dict and default (a LogFormatter class)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   299
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   300
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   301
        self.formatters = formatters
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   302
        self.default = default
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   303
    
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   304
    def parse (self, fmt_name) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   305
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   306
            fmt_name -> class LogFormatter
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   307
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   308
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   309
        return self.formatters[fmt_name]
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   310
    
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   311
    def build (self, fmt_cls) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   312
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   313
            class LogFormatter -> fmt_name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   314
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   315
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   316
        return fmt_cls.name
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   317
    
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   318
    def process (self, prefs, fmt_cls) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   319
        """
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   320
            class LogFormatter -> LogFormatter(tz, time_fmt, image_font.path)
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   321
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   322
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   323
        font_name, font_path, font_title = prefs[image_font]
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   324
        font_size = prefs[image_font_size]
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   325
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   326
        return fmt_cls(prefs[timezone], prefs[time_format], font_path, font_size)
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   327
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   328
class Count (urltree.URLIntegerType, Preference) :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   329
    """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   330
        Number of lines of log data to display per page
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   331
    """
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   332
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   333
    # set name
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   334
    name = "count"
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   335
    
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   336
    # default
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   337
    default = config.PREF_COUNT_DEFAULT
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   338
    
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   339
    def __init__ (self) :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   340
        super(Count, self).__init__(allow_negative=False, allow_zero=False, max=config.PREF_COUNT_MAX)
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   341
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   342
# and then build the Preferences object
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   343
time_format     = TimeFormat()
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   344
date_format     = DateFormat()
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   345
timezone        = Timezone()
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   346
image_font      = ImageFont(config.FORMATTER_IMAGE_FONTS, config.PREF_IMAGE_FONT_DEFAULT)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   347
image_font_size = ImageFontSize()
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   348
formatter       = Formatter(config.LOG_FORMATTERS, config.PREF_FORMATTER_DEFAULT)
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   349
count           = Count()
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   350
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   351
preferences = Preferences([
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   352
    time_format,
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   353
    date_format,
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   354
    timezone,
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   355
    image_font,
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   356
    image_font_size,
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   357
    formatter,
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   358
    count,
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   359
])
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   360