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@86: class PostForm(forms.Form):
bgneal@86:     """Form for creating a new post."""
bgneal@86:     body = forms.CharField(label='', widget=forms.Textarea)
bgneal@89:     topic_id = forms.IntegerField(widget=forms.HiddenInput)
bgneal@89:     topic = None
bgneal@83: 
bgneal@89:     class Media:
bgneal@89:         js = ('js/forums.js', )
bgneal@89: 
bgneal@89:     def clean_topic_id(self):
bgneal@89:         id = self.cleaned_data['topic_id']
bgneal@89:         print '*********', id
bgneal@89:         try:
bgneal@89:             self.topic = Topic.objects.get(pk=id)
bgneal@89:             print '******** Got a topic'
bgneal@89:         except Topic.DoesNotExist:
bgneal@89:             raise forms.ValidationError('invalid topic')
bgneal@89:         return id 
bgneal@89: 
bgneal@89:     def save(self, user, ip=None):
bgneal@86:         """
bgneal@86:         Creates a new post from the form data and supplied arguments.
bgneal@86:         """
bgneal@89:         post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'],
bgneal@89:                 user_ip=ip)
bgneal@86:         post.save()
bgneal@89:         return post
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@86:         arguments.
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