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, unsaved=None, *args, **kwargs):
18 initial = dict([(setting.name, setting.value) for setting in set])
21 initial.update(unsaved)
23 super(SettingsSetForm, self).__init__(data, initial=initial, *args, **kwargs)
26 widget = setting.field_context.get('widget', None)
28 if widget is forms.CheckboxSelectMultiple or widget is forms.SelectMultiple or isinstance(widget, forms.SelectMultiple):
29 field = forms.MultipleChoiceField(**setting.field_context)
30 elif widget is forms.RadioSelect or isinstance(widget, forms.RadioSelect):
31 field = forms.ChoiceField(**setting.field_context)
32 elif isinstance(setting, (Setting.emulators.get(str, DummySetting), Setting.emulators.get(unicode, DummySetting))):
33 if not setting.field_context.get('widget', None):
34 setting.field_context['widget'] = forms.TextInput(attrs={'class': 'longstring'})
35 field = forms.CharField(**setting.field_context)
36 elif isinstance(setting, Setting.emulators.get(float, DummySetting)):
37 field = forms.FloatField(**setting.field_context)
38 elif isinstance(setting, Setting.emulators.get(int, DummySetting)):
39 field = forms.IntegerField(**setting.field_context)
40 elif isinstance(setting, Setting.emulators.get(bool, DummySetting)):
41 field = forms.BooleanField(**setting.field_context)
43 field = UnfilteredField(**setting.field_context)
45 self.fields[setting.name] = field
50 return self._html_output(
51 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>' % (
52 _('context'), _('default'))) + u'<td>%(errors)s%(field)s%(help_text)s</td>',
53 u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
56 for setting in self.set:
57 setting.set_value(self.cleaned_data[setting.name])
59 class ImageFormWidget(forms.Widget):
60 def render(self, name, value, attrs=None):
62 <img src="%(value)s" /><br />
63 %(change)s: <input type="file" name="%(name)s" />
64 <input type="hidden" name="%(name)s_old" value="%(value)s" />
65 """ % {'name': name, 'value': value, 'change': _('Change this:')}
67 def value_from_datadict(self, data, files, name):
72 file_name_suffix = os.path.splitext(f.name)[1].lower()
74 if not file_name_suffix in ('.jpg', '.jpeg', '.gif', '.png', '.bmp', '.tiff', '.ico'):
75 raise Exception('File type not allowed')
77 from forum.settings import UPFILES_FOLDER, UPFILES_ALIAS
79 storage = FileSystemStorage(str(UPFILES_FOLDER), str(UPFILES_ALIAS))
80 new_file_name = storage.save(f.name, f)
81 return str(UPFILES_ALIAS) + new_file_name
83 if "%s_old" % name in data:
84 return data["%s_old" % name]
88 class StringListWidget(forms.Widget):
89 def render(self, name, value, attrs=None):
93 <div class="string-list-input">
94 <input type="text" name="%(name)s" value="%(value)s" />
95 <button class="string_list_widget_button">-</button>
97 """ % {'name': name, 'value': s}
100 <div class="string_list_widgets">
102 <div><button name="%(name)s" class="string_list_widget_button add">+</button></div>
104 """ % dict(name=name, ret=ret)
106 def value_from_datadict(self, data, files, name):
108 return data.getlist(name)
112 class CommaStringListWidget(forms.Textarea):
113 def render(self, name, value, attrs=None):
114 return super(CommaStringListWidget, self).render(name, ', '.join(value), attrs)
117 def value_from_datadict(self, data, files, name):
119 return map(strip, data[name].split(','))
121 return ', '.join(data[name])