]> git.openstreetmap.org Git - osqa.git/blob - forum/settings/forms.py
General cleanup. Moved many hardcoded stuff still left in the code to an admin option...
[osqa.git] / forum / settings / forms.py
1 import os
2 from string import strip
3 from django import forms
4 from base import Setting
5 from django.utils.translation import ugettext as _
6 from django.core.files.storage import FileSystemStorage
7
8 class DummySetting:
9     pass
10
11 class UnfilteredField(forms.CharField):
12     def clean(self, value):
13             return value
14
15
16 class SettingsSetForm(forms.Form):
17     def __init__(self, set, data=None, *args, **kwargs):
18         if data is None:
19             data = dict([(setting.name, setting.value) for setting in set])
20
21         super(SettingsSetForm, self).__init__(data, *args, **kwargs)
22
23         for setting in set:
24             if isinstance(setting, (Setting.emulators.get(str, DummySetting), Setting.emulators.get(unicode, DummySetting))):
25                 field = forms.CharField(**setting.field_context)
26             elif isinstance(setting, Setting.emulators.get(float, DummySetting)):
27                 field = forms.FloatField(**setting.field_context)
28             elif isinstance(setting, Setting.emulators.get(int, DummySetting)):
29                 field = forms.IntegerField(**setting.field_context)
30             elif isinstance(setting, Setting.emulators.get(bool, DummySetting)):
31                 field = forms.BooleanField(**setting.field_context)
32             else:
33                 field = UnfilteredField(**setting.field_context)
34
35             self.fields[setting.name] = field
36
37         self.set = set
38
39     def save(self):
40         for setting in self.set:
41             setting.set_value(self.cleaned_data[setting.name])
42
43 class ImageFormWidget(forms.Widget):
44     def render(self, name, value, attrs=None):
45         return """
46             <img src="%(value)s" /><br />
47             %(change)s: <input type="file" name="%(name)s" />
48             <input type="hidden" name="%(name)s_old" value="%(value)s" />
49             """ % {'name': name, 'value': value, 'change': _('Change this:')}
50
51     def value_from_datadict(self, data, files, name):
52         if name in files:
53             f = files[name]
54
55             # check file type
56             file_name_suffix = os.path.splitext(f.name)[1].lower()
57
58             if not file_name_suffix in ('.jpg', '.jpeg', '.gif', '.png', '.bmp', '.tiff', '.ico'):
59                 raise Exception('File type not allowed')
60
61             from forum.settings import UPFILES_FOLDER, UPFILES_ALIAS
62
63             storage = FileSystemStorage(str(UPFILES_FOLDER), str(UPFILES_ALIAS))
64             new_file_name = storage.save(f.name, f)
65             return str(UPFILES_ALIAS) + new_file_name
66         else:
67             if "%s_old" % name in data:
68                 return data["%s_old" % name]
69             elif name in data:
70                 return data[name]
71
72 class StringListWidget(forms.Widget):
73     def render(self, name, value, attrs=None):
74         ret = ""
75         for s in value:
76             ret += """
77             <div>
78                 <input type="text" name="%(name)s" value="%(value)s" />
79                 <button class="string_list_widget_button">-</button>
80             </div>
81             """  % {'name': name, 'value': s}
82
83         return """
84             <div class="string_list_widgets">
85                 %(ret)s
86                 <div><button name="%(name)s" class="string_list_widget_button add">+</button></div>
87             </div>
88             """ % dict(name=name, ret=ret)
89
90     def value_from_datadict(self, data, files, name):
91         if 'submit' in data:
92             return data.getlist(name)
93         else:
94             return data[name]
95
96 class CommaStringListWidget(forms.Textarea):
97     def value_from_datadict(self, data, files, name):
98         if 'submit' in data:
99             return map(strip, data[name].split(','))
100         else:
101             return ', '.join(data[name])    
102
103
104
105