]> git.openstreetmap.org Git - osqa.git/blob - forum/settings/forms.py
Adds the option to create static pages with several options to customize its behaviour.
[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
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                 if not setting.field_context.get('widget', None):
26                     setting.field_context['widget'] = forms.TextInput(attrs={'class': 'longstring'})
27                 field = forms.CharField(**setting.field_context)
28             elif isinstance(setting, Setting.emulators.get(float, DummySetting)):
29                 field = forms.FloatField(**setting.field_context)
30             elif isinstance(setting, Setting.emulators.get(int, DummySetting)):
31                 field = forms.IntegerField(**setting.field_context)
32             elif isinstance(setting, Setting.emulators.get(bool, DummySetting)):
33                 field = forms.BooleanField(**setting.field_context)
34             else:
35                 field = UnfilteredField(**setting.field_context)
36
37             self.fields[setting.name] = field
38
39         self.set = set
40
41     def as_table(self):
42         return self._html_output(
43                 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>' % (
44                     _('context'), _('default'))) + u'<td>%(errors)s%(field)s%(help_text)s</td>',
45                 u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
46
47     def save(self):
48         for setting in self.set:
49             setting.set_value(self.cleaned_data[setting.name])
50
51 class ImageFormWidget(forms.Widget):
52     def render(self, name, value, attrs=None):
53         return """
54             <img src="%(value)s" /><br />
55             %(change)s: <input type="file" name="%(name)s" />
56             <input type="hidden" name="%(name)s_old" value="%(value)s" />
57             """ % {'name': name, 'value': value, 'change': _('Change this:')}
58
59     def value_from_datadict(self, data, files, name):
60         if name in files:
61             f = files[name]
62
63             # check file type
64             file_name_suffix = os.path.splitext(f.name)[1].lower()
65
66             if not file_name_suffix in ('.jpg', '.jpeg', '.gif', '.png', '.bmp', '.tiff', '.ico'):
67                 raise Exception('File type not allowed')
68
69             from forum.settings import UPFILES_FOLDER, UPFILES_ALIAS
70
71             storage = FileSystemStorage(str(UPFILES_FOLDER), str(UPFILES_ALIAS))
72             new_file_name = storage.save(f.name, f)
73             return str(UPFILES_ALIAS) + new_file_name
74         else:
75             if "%s_old" % name in data:
76                 return data["%s_old" % name]
77             elif name in data:
78                 return data[name]
79
80 class StringListWidget(forms.Widget):
81     def render(self, name, value, attrs=None):
82         ret = ""
83         for s in value:
84             ret += """
85             <div class="string-list-input">
86                 <input type="text" name="%(name)s" value="%(value)s" />
87                 <button class="string_list_widget_button">-</button>
88             </div>
89             """  % {'name': name, 'value': s}
90
91         return """
92             <div class="string_list_widgets">
93                 %(ret)s
94                 <div><button name="%(name)s" class="string_list_widget_button add">+</button></div>
95             </div>
96             """ % dict(name=name, ret=ret)
97
98     def value_from_datadict(self, data, files, name):
99         if 'submit' in data:
100             return data.getlist(name)
101         else:
102             return data[name]
103
104 class CommaStringListWidget(forms.Textarea):
105     def value_from_datadict(self, data, files, name):
106         if 'submit' in data:
107             return map(strip, data[name].split(','))
108         else:
109             return ', '.join(data[name])    
110
111
112
113