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