X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/1a949f7c97dc2f34c135f5cdf088df2927d3d652..8b123b1f0d204c972d5a2554f9b27e4f7d8ae33f:/forum/settings/forms.py diff --git a/forum/settings/forms.py b/forum/settings/forms.py index 337dea1..3e3cc5a 100644 --- a/forum/settings/forms.py +++ b/forum/settings/forms.py @@ -1,32 +1,58 @@ import os +from string import strip from django import forms -from base import Setting, StringSetting, IntegerSetting, BoolSetting, FloatSetting +from forum.settings.base import Setting from django.utils.translation import ugettext as _ from django.core.files.storage import FileSystemStorage +from django.core.urlresolvers import reverse + +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: - 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, StringSetting): + 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, 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 self.set = set + def as_table(self): + return self._html_output( + u'