]> git.openstreetmap.org Git - osqa.git/blob - forum/settings/forms.py
Merging in changesets from revisions 809, 811, 812, 825, 826, and 827. These all...
[osqa.git] / forum / settings / forms.py
1 import os
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
8
9 class DummySetting:
10     pass
11
12 class UnfilteredField(forms.CharField):
13     def clean(self, value):
14             return value
15
16
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])
20
21         if unsaved:
22             initial.update(unsaved)
23
24         super(SettingsSetForm, self).__init__(data, initial=initial, *args, **kwargs)
25
26         for setting in set:
27             widget = setting.field_context.get('widget', None)
28
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)
43             else:
44                 field = UnfilteredField(**setting.field_context)
45
46             self.fields[setting.name] = field
47
48         self.set = set
49
50     def as_table(self):
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)
55
56     def save(self):
57         for setting in self.set:
58             setting.set_value(self.cleaned_data[setting.name])
59
60 class ImageFormWidget(forms.Widget):
61     def render(self, name, value, attrs=None):
62         return """
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:')}
67
68     def value_from_datadict(self, data, files, name):
69         if name in files:
70             f = files[name]
71
72             # check file type
73             file_name_suffix = os.path.splitext(f.name)[1].lower()
74
75             if not file_name_suffix in ('.jpg', '.jpeg', '.gif', '.png', '.bmp', '.tiff', '.ico'):
76                 raise Exception('File type not allowed')
77
78             from forum.settings import UPFILES_FOLDER, UPFILES_ALIAS
79
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
83         else:
84             if "%s_old" % name in data:
85                 return data["%s_old" % name]
86             elif name in data:
87                 return data[name]
88
89 class StringListWidget(forms.Widget):
90     def render(self, name, value, attrs=None):
91         ret = ""
92         for s in value:
93             ret += """
94             <div class="string-list-input">
95                 <input type="text" name="%(name)s" value="%(value)s" />
96                 <button class="string_list_widget_button">-</button>
97             </div>
98             """  % {'name': name, 'value': s}
99
100         return """
101             <div class="string_list_widgets">
102                 %(ret)s
103                 <div><button name="%(name)s" class="string_list_widget_button add">+</button></div>
104             </div>
105             """ % dict(name=name, ret=ret)
106
107     def value_from_datadict(self, data, files, name):
108         if 'submit' in data:
109             return data.getlist(name)
110         else:
111             return data[name]
112
113 class CommaStringListWidget(forms.Textarea):
114     def render(self, name, value, attrs=None):
115         return super(CommaStringListWidget, self).render(name, ', '.join(value), attrs)
116
117
118     def value_from_datadict(self, data, files, name):
119         if 'submit' in data:
120             return map(strip, data[name].split(','))
121         else:
122             return ', '.join(data[name])    
123
124 class TestEmailSettingsWidget(forms.TextInput):
125     def render(self, name, value, attrs=None):
126         if not value:
127             value = ''
128
129         return """
130             <div id="test_email_settings">
131                 <a href="%s" onclick="return false;" class="button test_button" href="/">Test</a>
132
133                 <div style="margin-top: 7px">
134                 <div style="display: none" class="ajax_indicator">
135                     Testing your current e-mail settings. Please, wait.
136                 </div>
137                 <div class="test_status"></div>
138                 </div>
139             </div>
140             """ % reverse("test_email_settings")