bgneal@83: """ bgneal@83: Forms for the forums application. bgneal@83: """ bgneal@83: from django import forms bgneal@95: from django.conf import settings bgneal@83: bgneal@83: from forums.models import Topic bgneal@83: from forums.models import Post bgneal@83: bgneal@83: bgneal@96: def bump_post_count(user): bgneal@96: """ bgneal@96: Increments the forum_post_count for the given user. bgneal@96: """ bgneal@96: profile = user.get_profile() bgneal@96: profile.forum_post_count += 1 bgneal@96: profile.save() bgneal@96: bgneal@96: 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@95: css = { bgneal@95: 'all': settings.GPP_THIRD_PARTY_CSS['markitup'], bgneal@95: } bgneal@95: js = settings.GPP_THIRD_PARTY_JS['markitup'] + \ bgneal@95: ('js/forums.js', ) bgneal@89: bgneal@89: def clean_topic_id(self): bgneal@89: id = self.cleaned_data['topic_id'] bgneal@89: try: bgneal@89: self.topic = Topic.objects.get(pk=id) 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@96: bump_post_count(user) 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@95: name = forms.CharField(label='Subject', max_length=255, bgneal@95: widget=forms.TextInput(attrs={'size': 64})) bgneal@83: body = forms.CharField(label='', widget=forms.Textarea) bgneal@83: bgneal@95: class Media: bgneal@95: css = { bgneal@95: 'all': settings.GPP_THIRD_PARTY_CSS['markitup'], bgneal@95: } bgneal@95: js = settings.GPP_THIRD_PARTY_JS['markitup'] + \ bgneal@95: ('js/forums.js', ) bgneal@95: 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@96: bump_post_count(user) bgneal@83: return topic