bgneal@83: """
bgneal@83: Forms for the forums application.
bgneal@83: """
bgneal@83: from django import forms
bgneal@83: 
bgneal@83: from forums.models import Topic
bgneal@83: from forums.models import Post
bgneal@83: 
bgneal@83: 
bgneal@83: class TopicForm(forms.ModelForm):
bgneal@83:     """Form for creating a new topic."""
bgneal@83:     
bgneal@83:     class Meta:
bgneal@83:         model = Topic
bgneal@83:         fields = ('name', )
bgneal@83: 
bgneal@83: 
bgneal@83: class PostForm(forms.ModelForm):
bgneal@83:     """Form for creating a new post."""
bgneal@83:     
bgneal@83:     class Meta:
bgneal@83:         model = Post
bgneal@83:         fields = ('body', )
bgneal@83: 
bgneal@83: 
bgneal@83: class NewTopicForm(forms.Form):
bgneal@83:     """Form for creating a new topic and 1st post to that topic."""
bgneal@83:     name = forms.CharField(label='Subject', max_length=255)
bgneal@83:     body = forms.CharField(label='', widget=forms.Textarea)
bgneal@83: 
bgneal@83:     def save(self, forum, user, ip=None):
bgneal@83:         """
bgneal@83:         Creates the new Topic and first Post from the form data and supplied
bgneal@83:         forum and user objects.
bgneal@83:         """
bgneal@83:         topic = Topic(forum=forum,
bgneal@83:                 name=self.cleaned_data['name'],
bgneal@83:                 user=user)
bgneal@83:         topic.save()
bgneal@83: 
bgneal@83:         post = Post(topic=topic,
bgneal@83:                 user=user,
bgneal@83:                 body=self.cleaned_data['body'],
bgneal@83:                 user_ip=ip)
bgneal@83:         post.save()
bgneal@83: 
bgneal@83:         return topic