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