]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/settings/forms.py
General cleanup. Moved many hardcoded stuff still left in the code to an admin option...
[osqa.git] / forum / settings / forms.py
index 337dea134961083319b5e38f95b243b0fdf61df9..81e5446583ff8cd66d0a8f15ddae5e5afa4c3bb3 100644 (file)
@@ -1,9 +1,18 @@
 import os
+from string import strip
 from django import forms
-from base import Setting, StringSetting, IntegerSetting, BoolSetting, FloatSetting
+from base import Setting
 from django.utils.translation import ugettext as _
 from django.core.files.storage import FileSystemStorage
 
+class DummySetting:
+    pass
+
+class UnfilteredField(forms.CharField):
+    def clean(self, value):
+            return value
+
+
 class SettingsSetForm(forms.Form):
     def __init__(self, set, data=None, *args, **kwargs):
         if data is None:
@@ -12,16 +21,16 @@ class SettingsSetForm(forms.Form):
         super(SettingsSetForm, self).__init__(data, *args, **kwargs)
 
         for setting in set:
-            if isinstance(setting, StringSetting):
+            if isinstance(setting, (Setting.emulators.get(str, DummySetting), Setting.emulators.get(unicode, DummySetting))):
                 field = forms.CharField(**setting.field_context)
-            elif isinstance(setting, FloatSetting):
+            elif isinstance(setting, Setting.emulators.get(float, DummySetting)):
                 field = forms.FloatField(**setting.field_context)
-            elif isinstance(setting, IntegerSetting):
+            elif isinstance(setting, Setting.emulators.get(int, DummySetting)):
                 field = forms.IntegerField(**setting.field_context)
-            elif isinstance(setting, BoolSetting):
+            elif isinstance(setting, Setting.emulators.get(bool, DummySetting)):
                 field = forms.BooleanField(**setting.field_context)
             else:
-                field = forms.CharField(**setting.field_context)
+                field = UnfilteredField(**setting.field_context)
 
             self.fields[setting.name] = field
 
@@ -60,4 +69,37 @@ class ImageFormWidget(forms.Widget):
             elif name in data:
                 return data[name]
 
+class StringListWidget(forms.Widget):
+    def render(self, name, value, attrs=None):
+        ret = ""
+        for s in value:
+            ret += """
+            <div>
+                <input type="text" name="%(name)s" value="%(value)s" />
+                <button class="string_list_widget_button">-</button>
+            </div>
+            """  % {'name': name, 'value': s}
+
+        return """
+            <div class="string_list_widgets">
+                %(ret)s
+                <div><button name="%(name)s" class="string_list_widget_button add">+</button></div>
+            </div>
+            """ % dict(name=name, ret=ret)
+
+    def value_from_datadict(self, data, files, name):
+        if 'submit' in data:
+            return data.getlist(name)
+        else:
+            return data[name]
+
+class CommaStringListWidget(forms.Textarea):
+    def value_from_datadict(self, data, files, name):
+        if 'submit' in data:
+            return map(strip, data[name].split(','))
+        else:
+            return ', '.join(data[name])    
+
+
+