]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/settings/forms.py
Prevents empty digests from being sent.
[osqa.git] / forum / settings / forms.py
index aa5a35285f6c03294bba0999ee52bd4d49d1cd61..0c123011cbc99976a71a9107eba0f50a858fd850 100644 (file)
@@ -1,6 +1,7 @@
 import os
+from string import strip
 from django import forms
-from base import Setting
+from forum.settings.base import Setting
 from django.utils.translation import ugettext as _
 from django.core.files.storage import FileSystemStorage
 
@@ -13,14 +14,24 @@ class UnfilteredField(forms.CharField):
 
 
 class SettingsSetForm(forms.Form):
-    def __init__(self, set, data=None, *args, **kwargs):
-        if data is None:
-            data = dict([(setting.name, setting.value) for setting in set])
+    def __init__(self, set, data=None, unsaved=None, *args, **kwargs):
+        initial = dict([(setting.name, setting.value) for setting in set])
 
-        super(SettingsSetForm, self).__init__(data, *args, **kwargs)
+        if unsaved:
+            initial.update(unsaved)
+
+        super(SettingsSetForm, self).__init__(data, initial=initial, *args, **kwargs)
 
         for setting in set:
-            if isinstance(setting, Setting.emulators.get(str, DummySetting)):
+            widget = setting.field_context.get('widget', None)
+
+            if widget is forms.CheckboxSelectMultiple or widget is forms.SelectMultiple or isinstance(widget, forms.SelectMultiple):
+                field = forms.MultipleChoiceField(**setting.field_context)
+            elif widget is forms.RadioSelect or isinstance(widget, forms.RadioSelect):
+                field = forms.ChoiceField(**setting.field_context)
+            elif isinstance(setting, (Setting.emulators.get(str, DummySetting), Setting.emulators.get(unicode, DummySetting))):
+                if not setting.field_context.get('widget', None):
+                    setting.field_context['widget'] = forms.TextInput(attrs={'class': 'longstring'})
                 field = forms.CharField(**setting.field_context)
             elif isinstance(setting, Setting.emulators.get(float, DummySetting)):
                 field = forms.FloatField(**setting.field_context)
@@ -35,6 +46,12 @@ class SettingsSetForm(forms.Form):
 
         self.set = set
 
+    def as_table(self):
+        return self._html_output(
+                u'<tr><th>%(label)s' + ('<br /><a class="fieldtool context" href="#">%s</a><span class="sep">|</span><a class="fieldtool default" href="#">%s</a></th>' % (
+                    _('context'), _('default'))) + u'<td>%(errors)s%(field)s%(help_text)s</td>',
+                u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
+
     def save(self):
         for setting in self.set:
             setting.set_value(self.cleaned_data[setting.name])
@@ -73,7 +90,7 @@ class StringListWidget(forms.Widget):
         ret = ""
         for s in value:
             ret += """
-            <div>
+            <div class="string-list-input">
                 <input type="text" name="%(name)s" value="%(value)s" />
                 <button class="string_list_widget_button">-</button>
             </div>
@@ -92,4 +109,18 @@ class StringListWidget(forms.Widget):
         else:
             return data[name]
 
+class CommaStringListWidget(forms.Textarea):
+    def render(self, name, value, attrs=None):
+        return super(CommaStringListWidget, self).render(name, ', '.join(value), attrs)
+
+
+    def value_from_datadict(self, data, files, name):
+        if 'submit' in data:
+            return map(strip, data[name].split(','))
+        else:
+            return ', '.join(data[name])    
+
+
+
+