2 from django.utils.encoding import force_unicode
4 class SettingSet(list):
5 def __init__(self, name, title, description, weight=1000):
8 self.description = description
11 class BaseSetting(object):
12 def __init__(self, name, default, field_context):
14 self.default = default
15 self.field_context = field_context
19 from forum.models import KeyValue
22 kv = KeyValue.objects.get(key=self.name)
24 kv = KeyValue(key=self.name, value=self._parse(self.default))
29 def set_value(self, new_value):
30 new_value = self._parse(new_value)
31 from forum.models import KeyValue
34 kv = KeyValue.objects.get(key=self.name)
37 kv = KeyValue(key=self.name)
38 old_value = self.default
43 setting_update.send(sender=self, old_value=old_value, new_value=new_value)
46 self.set_value(self.default)
48 def _parse(self, value):
52 return str(self.value)
54 def __unicode__(self):
55 return unicode(self.value)
57 def __nonzero__(self):
58 return bool(self.value)
61 class StringSetting(BaseSetting):
62 def _parse(self, value):
63 if isinstance(value, unicode):
64 return value.encode('utf8')
68 def __unicode__(self):
69 return unicode(self.value.decode('utf8'))
71 def __add__(self, other):
72 return "%s%s" % (unicode(self), other)
74 def __cmp__(self, other):
75 return cmp(str(self), str(other))
77 class IntegerSetting(BaseSetting):
78 def _parse(self, value):
82 return int(self.value)
84 def __add__(self, other):
85 return int(self) + int(other)
87 def __sub__(self, other):
88 return int(self) - int(other)
90 def __cmp__(self, other):
91 return int(self) - int(other)
93 class FloatSetting(BaseSetting):
94 def _parse(self, value):
98 return int(self.value)
101 return float(self.value)
103 def __add__(self, other):
104 return float(self) + float(other)
106 def __sub__(self, other):
107 return float(self) - float(other)
109 def __cmp__(self, other):
110 return float(self) - float(other)
112 class BoolSetting(BaseSetting):
113 def _parse(self, value):
116 class Setting(object):
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)
129 instance = BaseSetting(name, default, field_context)
132 if not set.name in cls.sets:
133 cls.sets[set.name] = set
135 cls.sets[set.name].append(instance)
139 setting_update = django.dispatch.Signal(providing_args=['old_value', 'new_value'])