]> git.openstreetmap.org Git - osqa.git/blob - forum/forms/admin.py
Bulk management changes:
[osqa.git] / forum / forms / admin.py
1 import socket
2 from django import forms
3 from django.utils.translation import ugettext as _
4 from django.contrib.admin.widgets import FilteredSelectMultiple, AdminDateWidget
5 from qanda import TitleField, EditorField
6 from forum import settings
7 from forum.models.node import NodeMetaClass
8 from forum.models import User
9
10 class IPListField(forms.CharField):
11     def clean(self, value):
12         ips = [ip.strip() for ip in value.strip().strip(',').split(',')]
13         iplist = []
14
15         if len(ips) < 1:
16             raise forms.ValidationError(_('Please input at least one ip address'))
17
18         for ip in ips:
19             try:
20                 socket.inet_aton(ip)
21             except socket.error:
22                 raise forms.ValidationError(_('Invalid ip address: %s' % ip))
23
24             if not len(ip.split('.')) == 4:
25                 raise forms.ValidationError(_('Please use the dotted quad notation for the ip addresses'))
26
27             iplist.append(ip)
28
29         return iplist
30
31 class MaintenanceModeForm(forms.Form):
32     ips = IPListField(label=_('Allow ips'),
33                       help_text=_('Comma separated list of ips allowed to access the site while in maintenance'),
34                       required=True,
35                       widget=forms.TextInput(attrs={'class': 'longstring'}))
36
37     message = forms.CharField(label=_('Message'),
38                               help_text=_('A message to display to your site visitors while in maintainance mode'),
39                               widget=forms.Textarea)
40
41
42 TEMPLATE_CHOICES = (
43 ('default', _('Default')),
44 ('sidebar', _('Default with sidebar')),
45 ('none', _('None')),
46 )
47
48 RENDER_CHOICES = (
49 ('markdown', _('Markdown')),
50 ('html', _('HTML')),
51 ('escape', _('Escaped'))
52 )
53
54 class UrlFieldWidget(forms.TextInput):
55     def render(self, name, value, attrs=None):
56         if not value:
57             value = ''
58
59         return """
60                 <input class="url_field" type="text" name="%(name)s" value="%(value)s" />
61                 <a class="url_field_anchor" target="_blank" href="%(app_url)s%(script_alias)s"></a>
62             """  % {'name': name, 'value': value, 'app_url': settings.APP_URL,
63                     'script_alias': settings.FORUM_SCRIPT_ALIAS}
64
65
66 class PageForm(forms.Form):
67     def __init__(self, page, *args, **kwargs):
68         if page:
69             initial = page.extra
70             initial.update(dict(title=page.title, content=page.body))
71             super(PageForm, self).__init__(initial=initial, *args, **kwargs)
72         else:
73             super(PageForm, self).__init__(*args, **kwargs)
74
75
76     title  = forms.CharField(label=_('Title'), max_length=255, widget=forms.TextInput(attrs={'class': 'longstring'}),
77                              initial='New page')
78     path  = forms.CharField(label=_('Page URL'), widget=UrlFieldWidget, initial='pages/new/')
79
80     content = forms.CharField(label=_('Page Content'), widget=forms.Textarea(attrs={'rows': 30}))
81     mimetype = forms.CharField(label=_('Mime Type'), initial='text/html')
82
83     render = forms.ChoiceField(widget=forms.RadioSelect, choices=RENDER_CHOICES, initial='markdown',
84                                label=_('Render Mode'))
85
86     template = forms.ChoiceField(widget=forms.RadioSelect, choices=TEMPLATE_CHOICES, initial='default',
87                                  label=_('Template'))
88     sidebar = forms.CharField(label=_('Sidebar Content'), widget=forms.Textarea(attrs={'rows': 20}), required=False)
89     sidebar_wrap = forms.BooleanField(label=_("Wrap sidebar block"), initial=True, required=False)
90     sidebar_render = forms.ChoiceField(widget=forms.RadioSelect, choices=RENDER_CHOICES, initial='markdown',
91                                        label=_('Sidebar Render Mode'))
92
93     comments = forms.BooleanField(label=_("Allow comments"), initial=False, required=False)
94
95 from forum.forms.auth import SimpleRegistrationForm
96 from forum.forms.general import SetPasswordForm
97
98
99 class CreateUserForm(SimpleRegistrationForm, SetPasswordForm):
100     validate_email = forms.BooleanField(required=False, label=_('send validation email'))
101
102     def __init__(self, *args, **kwargs):
103         super(CreateUserForm, self).__init__(*args, **kwargs)
104         self.fields.keyOrder = ['username', 'email', 'validate_email', 'password1', 'password2']
105         self.fields['email'].label = _('email address')
106
107
108