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
13 class BaseSetting(object):
15 def add_to_class(cls, name):
16 def wrapper(self, *args, **kwargs):
17 return self.value.__getattribute__(name)(*args, **kwargs)
19 setattr(cls, name, wrapper)
21 def __init__(self, name, default, set=None, field_context=None):
23 self.default = default
24 self.field_context = field_context or {}
27 if not set.name in Setting.sets:
28 Setting.sets[set.name] = set
30 Setting.sets[set.name].append(self)
32 def __unicode__(self):
33 return unicode(self.value)
37 from forum.models import KeyValue
40 kv = KeyValue.objects.get(key=self.name)
42 kv = KeyValue(key=self.name, value=self._parse(self.default))
47 def set_value(self, new_value):
48 new_value = self._parse(new_value)
51 def save(self, value):
52 from forum.models import KeyValue
55 kv = KeyValue.objects.get(key=self.name)
57 kv = KeyValue(key=self.name)
63 self.set_value(self.default)
65 def _parse(self, value):
69 class Setting(object):
73 def __new__(cls, name, default, set=None, field_context=None):
74 deftype = type(default)
76 if deftype in Setting.emulators:
77 emul = Setting.emulators[deftype]
79 emul = type(deftype.__name__ + cls.__name__, (BaseSetting,), {})
80 fns = [n for n, f in [(p, getattr(deftype, p)) for p in dir(deftype) if not p in dir(cls)] if callable(f)]
85 Setting.emulators[deftype] = emul
87 return emul(name, default, set, field_context)