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
7 from django.core.urlresolvers import reverse
12 class UnfilteredField(forms.CharField):
13 def clean(self, value):
17 class SettingsSetForm(forms.Form):
18 def __init__(self, set, data=None, unsaved=None, *args, **kwargs):
19 initial = dict([(setting.name, setting.value) for setting in set])
22 initial.update(unsaved)
24 super(SettingsSetForm, self).__init__(data, initial=initial, *args, **kwargs)
27 widget = setting.field_context.get('widget', None)
29 if widget is forms.CheckboxSelectMultiple or widget is forms.SelectMultiple or isinstance(widget, forms.SelectMultiple):
30 field = forms.MultipleChoiceField(**setting.field_context)
31 elif widget is forms.RadioSelect or isinstance(widget, forms.RadioSelect):
32 field = forms.ChoiceField(**setting.field_context)
33 elif isinstance(setting, (Setting.emulators.get(str, DummySetting), Setting.emulators.get(unicode, DummySetting))):
34 if not setting.field_context.get('widget', None):
35 setting.field_context['widget'] = forms.TextInput(attrs={'class': 'longstring'})
36 field = forms.CharField(**setting.field_context)
37 elif isinstance(setting, Setting.emulators.get(float, DummySetting)):
38 field = forms.FloatField(**setting.field_context)
39 elif isinstance(setting, Setting.emulators.get(int, DummySetting)):
40 field = forms.IntegerField(**setting.field_context)
41 elif isinstance(setting, Setting.emulators.get(bool, DummySetting)):
42 field = forms.BooleanField(**setting.field_context)
44 field = UnfilteredField(**setting.field_context)
46 self.fields[setting.name] = field
51 return self._html_output(
52 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>' % (
53 _('context'), _('default'))) + u'<td>%(errors)s%(field)s%(help_text)s</td>',
54 u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
57 for setting in self.set:
58 setting.set_value(self.cleaned_data[setting.name])
60 class ImageFormWidget(forms.Widget):
61 def render(self, name, value, attrs=None):
63 <img src="%(value)s" /><br />
64 %(change)s: <input type="file" name="%(name)s" />
65 <input type="hidden" name="%(name)s_old" value="%(value)s" />
66 """ % {'name': name, 'value': value, 'change': _('Change this:')}
68 def value_from_datadict(self, data, files, name):
73 file_name_suffix = os.path.splitext(f.name)[1].lower()
75 if not file_name_suffix in ('.jpg', '.jpeg', '.gif', '.png', '.bmp', '.tiff', '.ico'):
76 raise Exception('File type not allowed')
78 from forum.settings import UPFILES_FOLDER, UPFILES_ALIAS
80 storage = FileSystemStorage(str(UPFILES_FOLDER), str(UPFILES_ALIAS))
81 new_file_name = storage.save(f.name, f)
82 return str(UPFILES_ALIAS) + new_file_name
84 if "%s_old" % name in data:
85 return data["%s_old" % name]
89 class StringListWidget(forms.Widget):
90 def render(self, name, value, attrs=None):
94 <div class="string-list-input">
95 <input type="text" name="%(name)s" value="%(value)s" />
96 <button class="string_list_widget_button">-</button>
98 """ % {'name': name, 'value': s}
101 <div class="string_list_widgets">
103 <div><button name="%(name)s" class="string_list_widget_button add">+</button></div>
105 """ % dict(name=name, ret=ret)
107 def value_from_datadict(self, data, files, name):
109 return data.getlist(name)
113 class CommaStringListWidget(forms.Textarea):
114 def render(self, name, value, attrs=None):
115 return super(CommaStringListWidget, self).render(name, ', '.join(value), attrs)
118 def value_from_datadict(self, data, files, name):
120 return map(strip, data[name].split(','))
122 return ', '.join(data[name])
124 class TestEmailSettingsWidget(forms.TextInput):
125 def render(self, name, value, attrs=None):
130 <div id="test_email_settings">
131 <a href="%s" onclick="return false;" class="button test_button" href="/">Test</a>
133 <div style="margin-top: 7px">
134 <div style="display: none" class="ajax_indicator">
135 Testing your current e-mail settings. Please, wait.
137 <div class="test_status"></div>
140 """ % reverse("test_email_settings")