+class StringListWidget(forms.Widget):
+ def render(self, name, value, attrs=None):
+ ret = ""
+ for s in value:
+ ret += """
+ <div class="string-list-input">
+ <input type="text" name="%(name)s" value="%(value)s" />
+ <button class="string_list_widget_button">-</button>
+ </div>
+ """ % {'name': name, 'value': s}
+
+ return """
+ <div class="string_list_widgets">
+ %(ret)s
+ <div><button name="%(name)s" class="string_list_widget_button add">+</button></div>
+ </div>
+ """ % dict(name=name, ret=ret)
+
+ def value_from_datadict(self, data, files, name):
+ if 'submit' in data:
+ return data.getlist(name)
+ else:
+ return data[name]
+
+class CommaStringListWidget(forms.Textarea):
+ def value_from_datadict(self, data, files, name):
+ if 'submit' in data:
+ return map(strip, data[name].split(','))
+ else:
+ return ', '.join(data[name])
+
+
+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)
+
+