2 from django import forms
3 from base import Setting
4 from django.utils.translation import ugettext as _
5 from django.core.files.storage import FileSystemStorage
10 class UnfilteredField(forms.CharField):
11 def clean(self, value):
15 class SettingsSetForm(forms.Form):
16 def __init__(self, set, data=None, *args, **kwargs):
18 data = dict([(setting.name, setting.value) for setting in set])
20 super(SettingsSetForm, self).__init__(data, *args, **kwargs)
23 if isinstance(setting, Setting.emulators.get(str, DummySetting)):
24 field = forms.CharField(**setting.field_context)
25 elif isinstance(setting, Setting.emulators.get(float, DummySetting)):
26 field = forms.FloatField(**setting.field_context)
27 elif isinstance(setting, Setting.emulators.get(int, DummySetting)):
28 field = forms.IntegerField(**setting.field_context)
29 elif isinstance(setting, Setting.emulators.get(bool, DummySetting)):
30 field = forms.BooleanField(**setting.field_context)
32 field = UnfilteredField(**setting.field_context)
34 self.fields[setting.name] = field
39 for setting in self.set:
40 setting.set_value(self.cleaned_data[setting.name])
42 class ImageFormWidget(forms.Widget):
43 def render(self, name, value, attrs=None):
45 <img src="%(value)s" /><br />
46 %(change)s: <input type="file" name="%(name)s" />
47 <input type="hidden" name="%(name)s_old" value="%(value)s" />
48 """ % {'name': name, 'value': value, 'change': _('Change this:')}
50 def value_from_datadict(self, data, files, name):
55 file_name_suffix = os.path.splitext(f.name)[1].lower()
57 if not file_name_suffix in ('.jpg', '.jpeg', '.gif', '.png', '.bmp', '.tiff', '.ico'):
58 raise Exception('File type not allowed')
60 from forum.settings import UPFILES_FOLDER, UPFILES_ALIAS
62 storage = FileSystemStorage(str(UPFILES_FOLDER), str(UPFILES_ALIAS))
63 new_file_name = storage.save(f.name, f)
64 return str(UPFILES_ALIAS) + new_file_name
66 if "%s_old" % name in data:
67 return data["%s_old" % name]
71 class StringListWidget(forms.Widget):
72 def render(self, name, value, attrs=None):
77 <input type="text" name="%(name)s" value="%(value)s" />
78 <button class="string_list_widget_button">-</button>
80 """ % {'name': name, 'value': s}
83 <div class="string_list_widgets">
85 <div><button name="%(name)s" class="string_list_widget_button add">+</button></div>
87 """ % dict(name=name, ret=ret)
89 def value_from_datadict(self, data, files, name):
91 return data.getlist(name)