]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/settings/forms.py
Makes tips on the ask a question page editable in the admin.
[osqa.git] / forum / settings / forms.py
index 27581d5412eb95c3e953865102c1386c545a6184..3e3cc5afbf92833045346342b74d5b5c090df4aa 100644 (file)
@@ -1,10 +1,10 @@
 import os
-import socket
 from string import strip
 from django import forms
-from base import Setting
+from forum.settings.base import Setting
 from django.utils.translation import ugettext as _
 from django.core.files.storage import FileSystemStorage
+from django.core.urlresolvers import reverse
 
 class DummySetting:
     pass
@@ -15,14 +15,22 @@ class UnfilteredField(forms.CharField):
 
 
 class SettingsSetForm(forms.Form):
-    def __init__(self, set, data=None, *args, **kwargs):
-        if data is None:
-            data = dict([(setting.name, setting.value) for setting in set])
+    def __init__(self, set, data=None, unsaved=None, *args, **kwargs):
+        initial = dict([(setting.name, setting.value) for setting in set])
 
-        super(SettingsSetForm, self).__init__(data, *args, **kwargs)
+        if unsaved:
+            initial.update(unsaved)
+
+        super(SettingsSetForm, self).__init__(data, initial=initial, *args, **kwargs)
 
         for setting in set:
-            if isinstance(setting, (Setting.emulators.get(str, DummySetting), Setting.emulators.get(unicode, DummySetting))):
+            widget = setting.field_context.get('widget', None)
+
+            if widget is forms.CheckboxSelectMultiple or widget is forms.SelectMultiple or isinstance(widget, forms.SelectMultiple):
+                field = forms.MultipleChoiceField(**setting.field_context)
+            elif widget is forms.RadioSelect or isinstance(widget, forms.RadioSelect):
+                field = forms.ChoiceField(**setting.field_context)
+            elif isinstance(setting, (Setting.emulators.get(str, DummySetting), Setting.emulators.get(unicode, DummySetting))):
                 if not setting.field_context.get('widget', None):
                     setting.field_context['widget'] = forms.TextInput(attrs={'class': 'longstring'})
                 field = forms.CharField(**setting.field_context)
@@ -103,43 +111,30 @@ class StringListWidget(forms.Widget):
             return data[name]
 
 class CommaStringListWidget(forms.Textarea):
+    def render(self, name, value, attrs=None):
+        return super(CommaStringListWidget, self).render(name, ', '.join(value), attrs)
+
+
     def value_from_datadict(self, data, files, name):
         if 'submit' in data:
             return map(strip, data[name].split(','))
         else:
             return ', '.join(data[name])    
 
+class TestEmailSettingsWidget(forms.TextInput):
+    def render(self, name, value, attrs=None):
+        if not value:
+            value = ''
 
-class IPListField(forms.CharField):
-    def clean(self, value):
-        ips = [ip.strip() for ip in value.strip().strip(',').split(',')]
-        iplist = []
-
-        if len(ips) < 1:
-            raise forms.ValidationError(_('Please input at least one ip address'))    
-
-        for ip in ips:
-            try:
-                socket.inet_aton(ip)
-            except socket.error:
-                raise forms.ValidationError(_('Invalid ip address: %s' % ip))
-
-            if not len(ip.split('.')) == 4:
-                raise forms.ValidationError(_('Please use the dotted quad notation for the ip addresses'))
-
-            iplist.append(ip)
-
-        return iplist
-
-class MaintenanceModeForm(forms.Form):
-    ips = IPListField(label=_('Allow ips'),
-                      help_text=_('Comma separated list of ips allowed to access the site while in maintenance'),
-                      required=True,
-                      widget=forms.TextInput(attrs={'class': 'longstring'}))
-
-    message = forms.CharField(label=_('Message'),
-                              help_text=_('A message to display to your site visitors while in maintainance mode'),
-                              widget=forms.Textarea)
-
-
-
+        return """
+            <div id="test_email_settings">
+                <a href="%s" onclick="return false;" class="button test_button" href="/">Test</a>
+
+                <div style="margin-top: 7px">
+                <div style="display: none" class="ajax_indicator">
+                    Testing your current e-mail settings. Please, wait.
+                </div>
+                <div class="test_status"></div>
+                </div>
+            </div>
+            """ % reverse("test_email_settings")
\ No newline at end of file