annotate gpp/forums/forms.py @ 188:d293b423bdaf

Adding support for the django debug toolbar.
author Brian Neal <bgneal@gmail.com>
date Wed, 31 Mar 2010 01:58:20 +0000
parents 13330e1836f3
children 8fd4984d5c3b
rev   line source
bgneal@83 1 """
bgneal@83 2 Forms for the forums application.
bgneal@83 3 """
bgneal@83 4 from django import forms
bgneal@95 5 from django.conf import settings
bgneal@83 6
bgneal@110 7 from forums.models import Forum
bgneal@83 8 from forums.models import Topic
bgneal@83 9 from forums.models import Post
bgneal@83 10
bgneal@83 11
bgneal@106 12 class NewPostForm(forms.Form):
bgneal@86 13 """Form for creating a new post."""
bgneal@135 14 body = forms.CharField(label='',
bgneal@135 15 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
bgneal@89 16 topic_id = forms.IntegerField(widget=forms.HiddenInput)
bgneal@89 17 topic = None
bgneal@83 18
bgneal@89 19 class Media:
bgneal@95 20 css = {
bgneal@123 21 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
bgneal@123 22 settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
bgneal@95 23 }
bgneal@123 24 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
bgneal@123 25 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
bgneal@123 26 ('js/forums.js', ))
bgneal@89 27
bgneal@89 28 def clean_topic_id(self):
bgneal@89 29 id = self.cleaned_data['topic_id']
bgneal@89 30 try:
bgneal@102 31 self.topic = Topic.objects.select_related().get(pk=id)
bgneal@89 32 except Topic.DoesNotExist:
bgneal@89 33 raise forms.ValidationError('invalid topic')
bgneal@89 34 return id
bgneal@89 35
bgneal@89 36 def save(self, user, ip=None):
bgneal@86 37 """
bgneal@86 38 Creates a new post from the form data and supplied arguments.
bgneal@86 39 """
bgneal@89 40 post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'],
bgneal@89 41 user_ip=ip)
bgneal@86 42 post.save()
bgneal@89 43 return post
bgneal@83 44
bgneal@83 45
bgneal@83 46 class NewTopicForm(forms.Form):
bgneal@102 47 """
bgneal@102 48 Form for creating a new topic and 1st post to that topic.
bgneal@102 49 Superusers and moderators can also create the topic as a sticky or initially
bgneal@102 50 locked.
bgneal@102 51 """
bgneal@95 52 name = forms.CharField(label='Subject', max_length=255,
bgneal@95 53 widget=forms.TextInput(attrs={'size': 64}))
bgneal@135 54 body = forms.CharField(label='',
bgneal@135 55 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
bgneal@102 56 user = None
bgneal@102 57 forum = None
bgneal@102 58 has_mod_fields = False
bgneal@83 59
bgneal@95 60 class Media:
bgneal@95 61 css = {
bgneal@123 62 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
bgneal@123 63 settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
bgneal@95 64 }
bgneal@123 65 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
bgneal@123 66 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
bgneal@123 67 ('js/forums.js', ))
bgneal@95 68
bgneal@102 69 def __init__(self, user, forum, *args, **kwargs):
bgneal@102 70 super(NewTopicForm, self).__init__(*args, **kwargs)
bgneal@102 71 self.user = user
bgneal@102 72 self.forum = forum
bgneal@102 73
bgneal@102 74 if user.is_superuser or user in forum.moderators.all():
bgneal@102 75 self.fields['sticky'] = forms.BooleanField(required=False)
bgneal@102 76 self.fields['locked'] = forms.BooleanField(required=False)
bgneal@102 77 self.has_mod_fields = True
bgneal@102 78
bgneal@102 79 def save(self, ip=None):
bgneal@83 80 """
bgneal@83 81 Creates the new Topic and first Post from the form data and supplied
bgneal@86 82 arguments.
bgneal@83 83 """
bgneal@102 84 topic = Topic(forum=self.forum,
bgneal@83 85 name=self.cleaned_data['name'],
bgneal@102 86 user=self.user,
bgneal@102 87 sticky=self.has_mod_fields and self.cleaned_data['sticky'],
bgneal@102 88 locked=self.has_mod_fields and self.cleaned_data['locked'])
bgneal@83 89 topic.save()
bgneal@83 90
bgneal@83 91 post = Post(topic=topic,
bgneal@102 92 user=self.user,
bgneal@83 93 body=self.cleaned_data['body'],
bgneal@83 94 user_ip=ip)
bgneal@83 95 post.save()
bgneal@83 96 return topic
bgneal@106 97
bgneal@106 98
bgneal@106 99 class PostForm(forms.ModelForm):
bgneal@106 100 """
bgneal@108 101 Form for editing an existing post or a new, non-quick post.
bgneal@106 102 """
bgneal@135 103 body = forms.CharField(label='',
bgneal@135 104 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
bgneal@106 105
bgneal@106 106 class Meta:
bgneal@106 107 model = Post
bgneal@106 108 fields = ('body', )
bgneal@106 109
bgneal@106 110 class Media:
bgneal@106 111 css = {
bgneal@123 112 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
bgneal@123 113 settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
bgneal@106 114 }
bgneal@123 115 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
bgneal@123 116 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
bgneal@123 117 ('js/forums.js', ))
bgneal@110 118
bgneal@110 119
bgneal@110 120 class MoveTopicForm(forms.Form):
bgneal@111 121 """
bgneal@111 122 Form for a moderator to move a topic to a forum.
bgneal@111 123 """
bgneal@111 124 forums = forms.ModelChoiceField(label='Move to forum',
bgneal@111 125 queryset=Forum.objects.none())
bgneal@110 126
bgneal@111 127 def __init__(self, user, *args, **kwargs):
bgneal@111 128 hide_label = kwargs.pop('hide_label', False)
bgneal@111 129 required = kwargs.pop('required', True)
bgneal@111 130 super(MoveTopicForm, self).__init__(*args, **kwargs)
bgneal@111 131 self.fields['forums'].queryset = \
bgneal@111 132 Forum.objects.forums_for_user(user).order_by('name')
bgneal@111 133 if hide_label:
bgneal@111 134 self.fields['forums'].label = ''
bgneal@111 135 self.fields['forums'].required = required
bgneal@110 136
bgneal@115 137
bgneal@115 138 class SplitTopicForm(forms.Form):
bgneal@115 139 """
bgneal@115 140 Form for a moderator to split posts from a topic to a new topic.
bgneal@115 141 """
bgneal@115 142 name = forms.CharField(label='New topic title', max_length=255,
bgneal@115 143 widget=forms.TextInput(attrs={'size': 64}))
bgneal@115 144 forums = forms.ModelChoiceField(label='Forum for new topic',
bgneal@115 145 queryset=Forum.objects.none())
bgneal@115 146 post_ids = []
bgneal@115 147 split_at = False
bgneal@115 148
bgneal@115 149 def __init__(self, user, *args, **kwargs):
bgneal@115 150 super(SplitTopicForm, self).__init__(*args, **kwargs)
bgneal@115 151 self.fields['forums'].queryset = \
bgneal@115 152 Forum.objects.forums_for_user(user).order_by('name')
bgneal@115 153
bgneal@115 154 def clean(self):
bgneal@115 155 self.post_ids = self.data.getlist('post_ids')
bgneal@115 156 if len(self.post_ids) == 0:
bgneal@115 157 raise forms.ValidationError('Please select some posts')
bgneal@115 158
bgneal@115 159 self.split_at = 'split-at' in self.data
bgneal@115 160 if self.split_at and len(self.post_ids) > 1:
bgneal@115 161 raise forms.ValidationError('Please select only one post to split the topic at')
bgneal@115 162
bgneal@115 163 return self.cleaned_data