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
10 class IPListField(forms.CharField):
11 def clean(self, value):
12 ips = [ip.strip() for ip in value.strip().strip(',').split(',')]
16 raise forms.ValidationError(_('Please input at least one ip address'))
22 raise forms.ValidationError(_('Invalid ip address: %s' % ip))
24 if not len(ip.split('.')) == 4:
25 raise forms.ValidationError(_('Please use the dotted quad notation for the ip addresses'))
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'),
35 widget=forms.TextInput(attrs={'class': 'longstring'}))
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)
43 ('default', _('Default')),
44 ('sidebar', _('Default with sidebar')),
49 ('markdown', _('Markdown')),
51 ('escape', _('Escaped'))
54 class UrlFieldWidget(forms.TextInput):
55 def render(self, name, value, attrs=None):
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}
66 class PageForm(forms.Form):
67 def __init__(self, page, *args, **kwargs):
70 initial.update(dict(title=page.title, content=page.body))
71 super(PageForm, self).__init__(initial=initial, *args, **kwargs)
73 super(PageForm, self).__init__(*args, **kwargs)
76 title = forms.CharField(label=_('Title'), max_length=255, widget=forms.TextInput(attrs={'class': 'longstring'}),
78 path = forms.CharField(label=_('Page URL'), widget=UrlFieldWidget, initial='pages/new/')
80 content = forms.CharField(label=_('Page Content'), widget=forms.Textarea(attrs={'rows': 30}))
81 mimetype = forms.CharField(label=_('Mime Type'), initial='text/html')
83 render = forms.ChoiceField(widget=forms.RadioSelect, choices=RENDER_CHOICES, initial='markdown',
84 label=_('Render Mode'))
86 template = forms.ChoiceField(widget=forms.RadioSelect, choices=TEMPLATE_CHOICES, initial='default',
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'))
93 comments = forms.BooleanField(label=_("Allow comments"), initial=False, required=False)
95 from forum.forms.auth import SimpleRegistrationForm
96 from forum.forms.general import SetPasswordForm
99 class CreateUserForm(SimpleRegistrationForm, SetPasswordForm):
100 validate_email = forms.BooleanField(required=False, label=_('send validation email'))
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')