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