preferences.py
author Tero Marttila <terom@fixme.fi>
Sun, 13 Sep 2009 00:49:29 +0300
changeset 138 7dbe0ee1a27b
parent 131 67f5d2fdca1d
permissions -rw-r--r--
fix use of mercurial API to be compatible with 1.3.1 - this breaks backwards-compability, though...
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
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
     9
import utils
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
class Preference (urltree.URLType) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
        A specific preference
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
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    # the name to use
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    name = None
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    19
    # the default value, as from parse()
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    default = None
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    22
    def is_default (self, value) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    23
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    24
            Returns True if the given post-value is the default value for this preference.
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    25
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    26
            Defaults to just compare against self.default
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    27
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    28
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    29
        return (value == self.default)
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    30
        
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    def process (self, preferences, value) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
        """
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    33
            Post-process this preference value. This can access the post-processed values of all other preferences that
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    34
            were defined before this one in the list given to Preferences. 
53
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
            Defaults to just return value.
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
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        return value
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
class RequestPreferences (object) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        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
    44
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    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
    47
        """
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    48
            Initialize with the given Preferences object, http Request, and { key: value } mapping of raw preference values.
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    49
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    50
            This will build a mapping of { name: pre-value } using Preference.parse/Preference.default, and then
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    51
            post-process them into the final { name: value } mapping using Preference.process, in strict pref_list
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    52
            order. Note that the process() method will only have access to those preferences processed before it was.
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        # store
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
        self.preferences = preferences
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
        self.request = request
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
        # initialize
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        self.values = {}
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        self.set_cookies = {}
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    63
        # initial value map
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    64
        pre_values = {}
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    65
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        # load preferences
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        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
    68
            # got a value for it?
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
            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
    70
                # get value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
                value = value_map[pref.name]
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
                # parse it
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
                value = pref.parse(value)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
            else :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
                # use default value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
                value = pref.default
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    79
                
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
            # add
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    81
            pre_values[pref.name] = value
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
        
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    83
        # then post-process using Preferences.process(), in strict pref_list order
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    84
        for pref in preferences.pref_list :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    85
            # store into self.values, so that pref.get(...) will be able to access the still-incomplete self.values
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    86
            # dict
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    87
            self.values[pref.name] = pref.process(self, pre_values[pref.name])
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
    88
    
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    89
    def _get_name (self, pref) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    90
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    91
            Look up a Preference's name, either by class, object or name.
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    92
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    93
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    94
        # Preference -> name
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    95
        if isinstance(pref, Preference) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    96
            pref = pref.name
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    97
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    98
        return pref
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    99
    
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   100
    def pref (self, name) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   101
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   102
            Look up a Preference by object, name
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   103
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   104
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   105
        # Preference
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   106
        if isinstance(name, Preference) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   107
            return name
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   108
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   109
        # Preference.name
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   110
        elif isinstance(name, basestring) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   111
            return self.preferences.pref_map[name]
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   112
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   113
        # XXX: class?
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   114
        else :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   115
            assert False
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   116
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
    def get (self, pref) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
        """
62
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
   119
            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
   120
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
        
62
e7ca94b94a4e add prefs to render context, and remove old timezone/formatter from it
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
   122
        # look up
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   123
        return self.values[self._get_name(pref)]
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   124
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   125
    # support dict-access
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   126
    __getitem__ = get
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   127
    
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   128
    def is_default (self, pref) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   129
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   130
            Returns True if the given preference is at its default value
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   131
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   132
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   133
        # determine using Preference.is_default
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   134
        return self.pref(pref).is_default(self.get(pref))
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   135
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   136
    def build (self, pref) :
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
        """
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   138
            Like 'get', but return the raw cookie value
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   139
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   140
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   141
        # the Preference
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   142
        pref = self.pref(pref)
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   143
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   144
        # build
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   145
        return pref.build(self.get(pref))
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   146
    
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   147
    def parse (self, pref, value=None) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   148
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   149
            Parse+process the raw value for some pref into a value object.
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   150
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   151
            Is the given raw value is None, this uses Preference.default
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   152
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   153
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   154
        # lookup pref
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   155
        pref = self.pref(pref)
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   156
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   157
        # build value
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   158
        if value is not None :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   159
            # parse
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   160
            value = pref.parse(value)
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   161
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   162
        else :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   163
            # default
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   164
            value = pref.default
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   165
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   166
        # post-process
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   167
        value = pref.process(self, value)
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   168
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   169
        # return
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   170
        return value
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   171
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   172
    def set (self, name, value_obj=None) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   173
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   174
            Set a new value for the given preference (by str name).
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   175
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   176
            If value_obj is None, then the preference cookie is unset
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   179
        # 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
   180
        assert name not in self.set_cookies
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   181
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   182
        # None?
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   183
        if value_obj is not None :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   184
            # encode using the Preference object
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   185
            value_str = self.preferences.pref_map[name].build(value_obj)
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   186
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   187
        else :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   188
            # unset as None
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   189
            value_str = None
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   191
        # update in our dict
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   192
        self.values[name] = value_obj
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   193
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   194
        # add to set_cookies
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
        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
   196
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
class Preferences (object) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   198
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   199
        Handle user preferences using cookies
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   200
    """
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
    def __init__ (self, pref_list) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
        """
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   204
            Use the given list of Preference objects.
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   205
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   206
            The ordering of the given pref_list is significant for the process() implementation, as the
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   207
            Preferences are process()'d in order.
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   208
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   209
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
        # store
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   211
        self.pref_list = pref_list
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
        # translate to mapping as well
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   214
        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
   215
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
    def load (self, request, ) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   218
            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
   219
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   220
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   221
        # the dict of values
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   222
        values = {}
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
        # load the cookies
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   225
        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
   226
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   227
        # got any?
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   228
        if cookie_data :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   229
            # parse into a SimpleCookie
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   230
            cookies = Cookie.SimpleCookie(cookie_data)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   231
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   232
            # update the the values
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   233
            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
   234
        
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   235
        else :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   236
            cookies = None
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
        # apply any query parameters
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   239
        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
   240
            # look for a query param
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   241
            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
   242
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   243
            if value :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   244
                # override
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
                values[pref.name] = value
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   246
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   247
        # build the RequestPreferences object
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   248
        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
   249
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   250
    def handler (self, *pref_list) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   251
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   252
            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
   253
            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
   254
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   255
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   256
        def _decorator (func) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   257
            @functools.wraps(func)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   258
            def _handler (request, **args) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   259
                # load preferences
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   260
                cookies, prefs = self.load(request)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   261
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   262
                # bind to request.prefs
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   263
                # XXX: better way to do this? :/
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   264
                request.prefs = prefs
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   265
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   266
                # update args with new ones
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   267
                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
   268
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   269
                # handle to get response
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   270
                response = func(request, **args)
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   271
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   272
                # set cookies?
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   273
                if prefs.set_cookies :
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   274
                    # default, empty, cookiejar
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   275
                    if not cookies :
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   276
                        cookies = Cookie.SimpleCookie('')
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   277
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   278
                    # update cookies
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   279
                    for key, value in prefs.set_cookies.iteritems() :
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   280
                        if value is None :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   281
                            assert False, "Not implemented yet..."
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   282
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   283
                        else :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   284
                            # set
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   285
                            cookies[key] = value
131
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   286
                            cookies[key]["path"] = config.PREF_COOKIE_PATH
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   287
                            cookies[key]["expires"] = config.PREF_COOKIE_EXPIRE_SECONDS
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   288
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   289
                    # add headers
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   290
                    for morsel in cookies.itervalues() :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   291
                        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
   292
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   293
                return response
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   294
            
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   295
            # return wrapped handler
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   296
            return _handler
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
        # return decorator...
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   299
        return _decorator
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
# now for our defined preferences....
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   302
import pytz
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   303
import config
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   304
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   305
class TimeFormat (urltree.URLStringType, Preference) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   306
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   307
        Time format
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
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   310
    # set name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   311
    name = 'time_format'
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
    # default value
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   314
    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
   315
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   316
class DateFormat (urltree.URLStringType, Preference) :
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
        Date format
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   319
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   320
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   321
    # set name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   322
    name = 'date_format'
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   323
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   324
    # default value
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   325
    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
   326
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   327
class TimezoneOffset (Preference) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   328
    """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   329
        If the DST-aware 'timezone' is missing, we can fallback to a fixed-offset timezone as detected by
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   330
        Javascript.
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   331
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   332
        This is read-only, and None by default
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   333
    """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   334
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   335
    name = 'timezone_offset'
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   336
    default = None
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   337
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   338
    def parse (self, offset) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   339
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   340
            Offset in minutes -> said minutes
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   341
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   342
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   343
        return int(offset)
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   344
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   345
class Timezone (Preference) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   346
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   347
        Timezone
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   348
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   349
    
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   350
    # set name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   351
    name = 'timezone'
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   352
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   353
    # default is handled via process()
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   354
    default = 'auto'
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   355
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   356
    # the list of available (value, name) options for use with helpers.select_options
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   357
    OPTIONS = [('auto', "Autodetect")] + [(None, tz_name) for tz_name in pytz.common_timezones]
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   358
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   359
    def parse (self, name) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   360
        """
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   361
            default -> default
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   362
            tz_name -> pytz.timezone
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   363
        """
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   364
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   365
        # special-case for 'auto'
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   366
        if name == self.default :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   367
            return self.default
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   368
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   369
        else :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   370
            return pytz.timezone(name)
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   371
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   372
    def is_default (self, tz) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   373
        """
131
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   374
            True if it's a FixedOffsetTimezone or PREF_TIMEZONE_FALLBACK
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   375
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   376
131
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   377
        return (isinstance(tz, utils.FixedOffsetTimezone) or tz == config.PREF_TIMEZONE_FALLBACK)
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   378
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   379
    def build (self, tz) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   380
        """
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   381
            FixedOffsetTimezone -> None
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   382
            pytz.timezone -> tz_name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   383
        """
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   384
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   385
        # special-case for auto/no explicit timezone
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   386
        if self.is_default(tz) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   387
            return self.default
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   388
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   389
        else :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   390
            # pytz.timezone zone name
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   391
            return tz.zone
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   392
    
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   393
    def process (self, prefs, tz) :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   394
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   395
            If this timezone is given, simply build that. Otherwise, try and use TimezoneOffset, and if that fails,
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   396
            just return the default.
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   397
131
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   398
            None -> FixedOffsetTimezone/PREF_TIMEZONE_FALLBACK
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   399
            pytz.timezone -> pytz.timezone
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   400
        """
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   401
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   402
        # specific timezone set?
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   403
        if tz != self.default :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   404
            return tz
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   405
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   406
        # fixed offset?
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   407
        elif prefs[timezone_offset] is not None :
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   408
            return utils.FixedOffsetTimezone(prefs[timezone_offset])
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   409
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   410
        # default
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   411
        else :
131
67f5d2fdca1d rename config.PREF_TIMEZONE_FALLBACK, add Cookie expire/path info, and move the hg version stuff into the template/helpers
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   412
            return config.PREF_TIMEZONE_FALLBACK
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   413
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   414
class ImageFont (Preference) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   415
    """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   416
        Font for ImageFormatter
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   417
    """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   418
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   419
    # set name
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   420
    name = 'image_font'
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   421
    
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   422
    def __init__ (self, font_dict, default_name) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   423
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   424
            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
   425
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   426
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   427
        self.font_dict = font_dict
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   428
        self.default = self.parse(default_name)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   429
    
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   430
    def parse (self, name) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   431
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   432
            name -> (name, path, title)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   433
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   434
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   435
        path, title = self.font_dict[name]
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   436
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   437
        return name, path, title
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   438
    
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   439
    def build (self, font_info) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   440
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   441
            (name, path, title) -> name
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   442
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   443
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   444
        name, path, title = font_info
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   445
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   446
        return name
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   447
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   448
class ImageFontSize (urltree.URLIntegerType, Preference) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   449
    # set name, default
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   450
    name = 'image_font_size'
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   451
    default = config.PREF_IMAGE_FONT_SIZE_DEFAULT
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   452
    
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   453
    # XXX: constraints for valid values
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   454
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   455
class Formatter (Preference) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   456
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   457
        LogFormatter to use
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   458
    """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   459
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   460
    # set name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   461
    name = 'formatter'
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   462
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   463
    def __init__ (self, formatters, default) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   464
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   465
            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
   466
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   467
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   468
        self.formatters = formatters
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   469
        self.default = default
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   470
    
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   471
    def parse (self, fmt_name) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   472
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   473
            fmt_name -> class LogFormatter
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   474
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   475
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   476
        return self.formatters[fmt_name]
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   477
    
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   478
    def build (self, fmt_cls) :
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   479
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   480
            class LogFormatter -> fmt_name
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   481
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   482
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   483
        return fmt_cls.name
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   484
    
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   485
    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
   486
        """
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   487
            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
   488
        """
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   489
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   490
        # time stuff
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   491
        tz = prefs[timezone]
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   492
        time_fmt = prefs[time_format]
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   493
        
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   494
        # font stuff
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   495
        font_name, font_path, font_title = prefs[image_font]
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   496
        font_size = prefs[image_font_size]
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   497
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   498
        return fmt_cls(tz, time_fmt, font_path, font_size)
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   499
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   500
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
   501
    """
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   502
        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
   503
    """
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   504
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   505
    # set name
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   506
    name = "count"
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   507
    
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   508
    # default
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   509
    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
   510
    
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   511
    def __init__ (self) :
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   512
        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
   513
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   514
# 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
   515
time_format     = TimeFormat()
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   516
date_format     = DateFormat()
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   517
timezone_offset = TimezoneOffset()
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   518
timezone        = Timezone()
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   519
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
   520
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
   521
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
   522
count           = Count()
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   523
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   524
preferences = Preferences([
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   525
    time_format,
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   526
    date_format,
129
67a30d680f60 Autodetect user timezone using Javascript, this makes the Preferences code a bit more complicated in terms of interaction between default/parse/is_default/build/process/etc, but it should work as intended now
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
   527
    timezone_offset,
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   528
    timezone,
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   529
    image_font,
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   530
    image_font_size,
59
8ec729c5d998 ugly, but working, preferences editor
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   531
    formatter,
73
5a7188bf2894 split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents: 62
diff changeset
   532
    count,
53
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   533
])
8103d18907a0 add some user-preferences support (e.g. timezone, time formats)
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   534