annotate gpp/forums/forms.py @ 123:3ae999b0c53b

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