2 from django.utils.encoding import force_unicode
4 class SettingSet(list):
5 def __init__(self, name, title, description, weight=1000, markdown=False):
8 self.description = description
10 self.markdown = markdown
12 class BaseSetting(object):
13 def __init__(self, name, default, field_context):
15 self.default = default
16 self.field_context = field_context
20 from forum.models import KeyValue
23 kv = KeyValue.objects.get(key=self.name)
25 kv = KeyValue(key=self.name, value=self._parse(self.default))
30 def set_value(self, new_value):
31 new_value = self._parse(new_value)
32 from forum.models import KeyValue
35 kv = KeyValue.objects.get(key=self.name)
38 kv = KeyValue(key=self.name)
39 old_value = self.default
44 setting_update.send(sender=self, old_value=old_value, new_value=new_value)
47 self.set_value(self.default)
49 def _parse(self, value):
53 return str(self.value)
55 def __unicode__(self):
56 return unicode(self.value)
58 def __nonzero__(self):
59 return bool(self.value)
62 class StringSetting(BaseSetting):
63 def _parse(self, value):
64 if isinstance(value, unicode):
65 return value.encode('utf8')
69 def __unicode__(self):
70 return unicode(self.value.decode('utf8'))
72 def __add__(self, other):
73 return "%s%s" % (unicode(self), other)
75 def __cmp__(self, other):
76 return cmp(str(self), str(other))
78 class IntegerSetting(BaseSetting):
79 def _parse(self, value):
83 return int(self.value)
85 def __add__(self, other):
86 return int(self) + int(other)
88 def __sub__(self, other):
89 return int(self) - int(other)
91 def __cmp__(self, other):
92 return int(self) - int(other)
94 class FloatSetting(BaseSetting):
95 def _parse(self, value):
99 return int(self.value)
102 return float(self.value)
104 def __add__(self, other):
105 return float(self) + float(other)
107 def __sub__(self, other):
108 return float(self) - float(other)
110 def __cmp__(self, other):
111 return float(self) - float(other)
113 class BoolSetting(BaseSetting):
114 def _parse(self, value):
117 class Setting(object):
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)
130 instance = BaseSetting(name, default, field_context)
133 if not set.name in cls.sets:
134 cls.sets[set.name] = set
136 cls.sets[set.name].append(instance)
140 setting_update = django.dispatch.Signal(providing_args=['old_value', 'new_value'])