annotate gpp/forums/forms.py @ 91:62af8cd8f57b

Forums: added support for Post get_absolute_url()
author Brian Neal <bgneal@gmail.com>
date Sun, 13 Sep 2009 04:05:37 +0000
parents 021492db4aad
children 23035afdeae8
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@83 5
bgneal@83 6 from forums.models import Topic
bgneal@83 7 from forums.models import Post
bgneal@83 8
bgneal@83 9
bgneal@86 10 class PostForm(forms.Form):
bgneal@86 11 """Form for creating a new post."""
bgneal@86 12 body = forms.CharField(label='', widget=forms.Textarea)
bgneal@89 13 topic_id = forms.IntegerField(widget=forms.HiddenInput)
bgneal@89 14 topic = None
bgneal@83 15
bgneal@89 16 class Media:
bgneal@89 17 js = ('js/forums.js', )
bgneal@89 18
bgneal@89 19 def clean_topic_id(self):
bgneal@89 20 id = self.cleaned_data['topic_id']
bgneal@89 21 print '*********', id
bgneal@89 22 try:
bgneal@89 23 self.topic = Topic.objects.get(pk=id)
bgneal@89 24 print '******** Got a topic'
bgneal@89 25 except Topic.DoesNotExist:
bgneal@89 26 raise forms.ValidationError('invalid topic')
bgneal@89 27 return id
bgneal@89 28
bgneal@89 29 def save(self, user, ip=None):
bgneal@86 30 """
bgneal@86 31 Creates a new post from the form data and supplied arguments.
bgneal@86 32 """
bgneal@89 33 post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'],
bgneal@89 34 user_ip=ip)
bgneal@86 35 post.save()
bgneal@89 36 return post
bgneal@83 37
bgneal@83 38
bgneal@83 39 class NewTopicForm(forms.Form):
bgneal@83 40 """Form for creating a new topic and 1st post to that topic."""
bgneal@83 41 name = forms.CharField(label='Subject', max_length=255)
bgneal@83 42 body = forms.CharField(label='', widget=forms.Textarea)
bgneal@83 43
bgneal@83 44 def save(self, forum, user, ip=None):
bgneal@83 45 """
bgneal@83 46 Creates the new Topic and first Post from the form data and supplied
bgneal@86 47 arguments.
bgneal@83 48 """
bgneal@83 49 topic = Topic(forum=forum,
bgneal@83 50 name=self.cleaned_data['name'],
bgneal@83 51 user=user)
bgneal@83 52 topic.save()
bgneal@83 53
bgneal@83 54 post = Post(topic=topic,
bgneal@83 55 user=user,
bgneal@83 56 body=self.cleaned_data['body'],
bgneal@83 57 user_ip=ip)
bgneal@83 58 post.save()
bgneal@83 59
bgneal@83 60 return topic