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