]> git.openstreetmap.org Git - osqa.git/blob - forum/modules/template_loader.py/forum/forms/admin.py
Creating my branch
[osqa.git] / forum / modules / template_loader.py / 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"></a>
62             """  % {'name': name, 'value': value, 'app_url': settings.APP_URL}
63
64
65 class PageForm(forms.Form):
66     def __init__(self, page, *args, **kwargs):
67         if page:
68             initial = page.extra
69             initial.update(dict(title=page.title, content=page.body))
70             super(PageForm, self).__init__(initial=initial, *args, **kwargs)
71         else:
72             super(PageForm, self).__init__(*args, **kwargs)
73
74
75     title  = forms.CharField(label=_('Title'), max_length=255, widget=forms.TextInput(attrs={'class': 'longstring'}),
76                              initial='New page')
77     path  = forms.CharField(label=_('Page URL'), widget=UrlFieldWidget, initial='pages/new/')
78
79     content = forms.CharField(label=_('Page Content'), widget=forms.Textarea(attrs={'rows': 30}))
80     mimetype = forms.CharField(label=_('Mime Type'), initial='text/html')
81
82     render = forms.ChoiceField(widget=forms.RadioSelect, choices=RENDER_CHOICES, initial='markdown',
83                                label=_('Render Mode'))
84
85     template = forms.ChoiceField(widget=forms.RadioSelect, choices=TEMPLATE_CHOICES, initial='default',
86                                  label=_('Template'))
87     sidebar = forms.CharField(label=_('Sidebar Content'), widget=forms.Textarea(attrs={'rows': 20}), required=False)
88     sidebar_wrap = forms.BooleanField(label=_("Wrap sidebar block"), initial=True, required=False)
89     sidebar_render = forms.ChoiceField(widget=forms.RadioSelect, choices=RENDER_CHOICES, initial='markdown',
90                                        label=_('Sidebar Render Mode'))
91
92     comments = forms.BooleanField(label=_("Allow comments"), initial=False, required=False)
93
94 from forum.forms.auth import SimpleRegistrationForm
95 from forum.forms.general import SetPasswordForm
96
97
98 class CreateUserForm(SimpleRegistrationForm, SetPasswordForm):
99     validate_email = forms.BooleanField(required=False, label=_('send validation email'))
100
101     def __init__(self, *args, **kwargs):
102         super(CreateUserForm, self).__init__(*args, **kwargs)
103         self.fields.keyOrder = ['username', 'email', 'validate_email', 'password1', 'password2']
104         self.fields['email'].label = _('email address')
105
106
107