annotate gpp/forums/forms.py @ 83:5b4c812b448e

Forums: Added the ability to add a new topic. This is very much a work in progress.
author Brian Neal <bgneal@gmail.com>
date Sat, 29 Aug 2009 20:54:16 +0000
parents
children f81226b5e87b
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@83 10 class TopicForm(forms.ModelForm):
bgneal@83 11 """Form for creating a new topic."""
bgneal@83 12
bgneal@83 13 class Meta:
bgneal@83 14 model = Topic
bgneal@83 15 fields = ('name', )
bgneal@83 16
bgneal@83 17
bgneal@83 18 class PostForm(forms.ModelForm):
bgneal@83 19 """Form for creating a new post."""
bgneal@83 20
bgneal@83 21 class Meta:
bgneal@83 22 model = Post
bgneal@83 23 fields = ('body', )
bgneal@83 24
bgneal@83 25
bgneal@83 26 class NewTopicForm(forms.Form):
bgneal@83 27 """Form for creating a new topic and 1st post to that topic."""
bgneal@83 28 name = forms.CharField(label='Subject', max_length=255)
bgneal@83 29 body = forms.CharField(label='', widget=forms.Textarea)
bgneal@83 30
bgneal@83 31 def save(self, forum, user, ip=None):
bgneal@83 32 """
bgneal@83 33 Creates the new Topic and first Post from the form data and supplied
bgneal@83 34 forum and user objects.
bgneal@83 35 """
bgneal@83 36 topic = Topic(forum=forum,
bgneal@83 37 name=self.cleaned_data['name'],
bgneal@83 38 user=user)
bgneal@83 39 topic.save()
bgneal@83 40
bgneal@83 41 post = Post(topic=topic,
bgneal@83 42 user=user,
bgneal@83 43 body=self.cleaned_data['body'],
bgneal@83 44 user_ip=ip)
bgneal@83 45 post.save()
bgneal@83 46
bgneal@83 47 return topic