2 from string import strip
3 from django import forms
4 from forum.settings.base import Setting
5 from django.utils.translation import ugettext as _
6 from django.core.files.storage import FileSystemStorage
11 class UnfilteredField(forms.CharField):
12 def clean(self, value):
16 class SettingsSetForm(forms.Form):
17 def __init__(self, set, data=None, *args, **kwargs):
19 data = dict([(setting.name, setting.value) for setting in set])
21 super(SettingsSetForm, self).__init__(data, *args, **kwargs)
24 if isinstance(setting, (Setting.emulators.get(str, DummySetting), Setting.emulators.get(unicode, DummySetting))):
25 if not setting.field_context.get('widget', None):
26 setting.field_context['widget'] = forms.TextInput(attrs={'class': 'longstring'})
27 field = forms.CharField(**setting.field_context)
28 elif isinstance(setting, Setting.emulators.get(float, DummySetting)):
29 field = forms.FloatField(**setting.field_context)
30 elif isinstance(setting, Setting.emulators.get(int, DummySetting)):
31 field = forms.IntegerField(**setting.field_context)
32 elif isinstance(setting, Setting.emulators.get(bool, DummySetting)):
33 field = forms.BooleanField(**setting.field_context)
35 field = UnfilteredField(**setting.field_context)
37 self.fields[setting.name] = field
42 return self._html_output(
43 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>' % (
44 _('context'), _('default'))) + u'<td>%(errors)s%(field)s%(help_text)s</td>',
45 u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
48 for setting in self.set:
49 setting.set_value(self.cleaned_data[setting.name])
51 class ImageFormWidget(forms.Widget):
52 def render(self, name, value, attrs=None):
54 <img src="%(value)s" /><br />
55 %(change)s: <input type="file" name="%(name)s" />
56 <input type="hidden" name="%(name)s_old" value="%(value)s" />
57 """ % {'name': name, 'value': value, 'change': _('Change this:')}
59 def value_from_datadict(self, data, files, name):
64 file_name_suffix = os.path.splitext(f.name)[1].lower()
66 if not file_name_suffix in ('.jpg', '.jpeg', '.gif', '.png', '.bmp', '.tiff', '.ico'):
67 raise Exception('File type not allowed')
69 from forum.settings import UPFILES_FOLDER, UPFILES_ALIAS
71 storage = FileSystemStorage(str(UPFILES_FOLDER), str(UPFILES_ALIAS))
72 new_file_name = storage.save(f.name, f)
73 return str(UPFILES_ALIAS) + new_file_name
75 if "%s_old" % name in data:
76 return data["%s_old" % name]
80 class StringListWidget(forms.Widget):
81 def render(self, name, value, attrs=None):
85 <div class="string-list-input">
86 <input type="text" name="%(name)s" value="%(value)s" />
87 <button class="string_list_widget_button">-</button>
89 """ % {'name': name, 'value': s}
92 <div class="string_list_widgets">
94 <div><button name="%(name)s" class="string_list_widget_button add">+</button></div>
96 """ % dict(name=name, ret=ret)
98 def value_from_datadict(self, data, files, name):
100 return data.getlist(name)
104 class CommaStringListWidget(forms.Textarea):
105 def value_from_datadict(self, data, files, name):
107 return map(strip, data[name].split(','))
109 return ', '.join(data[name])