]> git.openstreetmap.org Git - osqa.git/blob - forum/settings/base.py
error in votes left message
[osqa.git] / forum / settings / base.py
1 import django.dispatch
2 from django.utils.encoding import force_unicode
3
4 class SettingSet(list):
5     def __init__(self, name, title, description, weight=1000):
6         self.name = name
7         self.title = title
8         self.description = description
9         self.weight = weight
10
11 class BaseSetting(object):
12     def __init__(self, name, default, field_context):
13         self.name = name
14         self.default = default
15         self.field_context = field_context
16
17     @property
18     def value(self):
19         from forum.models import KeyValue
20
21         try:
22             kv = KeyValue.objects.get(key=self.name)
23         except:
24             kv = KeyValue(key=self.name, value=self._parse(self.default))
25             kv.save()
26
27         return kv.value
28
29     def set_value(self, new_value):
30         new_value = self._parse(new_value)
31         from forum.models import KeyValue
32
33         try:
34             kv = KeyValue.objects.get(key=self.name)
35             old_value = kv.value
36         except:
37             kv = KeyValue(key=self.name)
38             old_value = self.default
39
40         kv.value = new_value
41         kv.save()
42
43         setting_update.send(sender=self, old_value=old_value, new_value=new_value)
44
45     def to_default(self):
46         self.set_value(self.default)
47
48     def _parse(self, value):
49         return value
50
51     def __str__(self):
52         return str(self.value)
53
54     def __unicode__(self):
55         return unicode(self.value)
56
57     def __nonzero__(self):
58         return bool(self.value)
59
60
61 class StringSetting(BaseSetting):
62     def _parse(self, value):
63         if isinstance(value, unicode):
64             return value.encode('utf8')
65         else:
66             return str(value)
67
68     def __unicode__(self):
69         return unicode(self.value.decode('utf8'))
70
71     def __add__(self, other):
72         return "%s%s" % (unicode(self), other)
73
74     def __cmp__(self, other):
75         return cmp(str(self), str(other))
76
77 class IntegerSetting(BaseSetting):
78     def _parse(self, value):
79         return int(value)
80
81     def __int__(self):
82         return int(self.value)
83
84     def __add__(self, other):
85         return int(self) + int(other)
86
87     def __sub__(self, other):
88         return int(self) - int(other)
89
90     def __cmp__(self, other):
91         return int(self) - int(other)
92
93 class FloatSetting(BaseSetting):
94     def _parse(self, value):
95         return float(value)
96
97     def __int__(self):
98         return int(self.value)
99
100     def __float__(self):
101         return float(self.value)
102
103     def __add__(self, other):
104         return float(self) + float(other)
105
106     def __sub__(self, other):
107         return float(self) - float(other)
108
109     def __cmp__(self, other):
110         return float(self) - float(other)
111
112 class BoolSetting(BaseSetting):
113     def _parse(self, value):
114         return bool(value)
115
116 class Setting(object):
117     sets = {}
118
119     def __new__(cls, name, default, set=None, field_context={}):
120         if isinstance(default, bool):
121             instance = BoolSetting(name, default, field_context)
122         elif isinstance(default, str):
123             instance = StringSetting(name, default, field_context)
124         elif isinstance(default, float):
125             instance = FloatSetting(name, default, field_context)
126         elif isinstance(default, int):
127             instance = IntegerSetting(name, default, field_context)
128         else:
129             instance = BaseSetting(name, default, field_context)
130
131         if set is not None:
132             if not set.name in cls.sets:
133                 cls.sets[set.name] = set
134
135             cls.sets[set.name].append(instance)
136
137         return instance
138
139 setting_update = django.dispatch.Signal(providing_args=['old_value', 'new_value'])