annotate gpp/forums/forms.py @ 96:93d9e74a471e

Forums: added forum post counts for each user in the user profile.
author Brian Neal <bgneal@gmail.com>
date Sun, 13 Sep 2009 18:43:08 +0000
parents 23035afdeae8
children eb9f99382476
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@95 5 from django.conf import settings
bgneal@83 6
bgneal@83 7 from forums.models import Topic
bgneal@83 8 from forums.models import Post
bgneal@83 9
bgneal@83 10
bgneal@96 11 def bump_post_count(user):
bgneal@96 12 """
bgneal@96 13 Increments the forum_post_count for the given user.
bgneal@96 14 """
bgneal@96 15 profile = user.get_profile()
bgneal@96 16 profile.forum_post_count += 1
bgneal@96 17 profile.save()
bgneal@96 18
bgneal@96 19
bgneal@86 20 class PostForm(forms.Form):
bgneal@86 21 """Form for creating a new post."""
bgneal@86 22 body = forms.CharField(label='', widget=forms.Textarea)
bgneal@89 23 topic_id = forms.IntegerField(widget=forms.HiddenInput)
bgneal@89 24 topic = None
bgneal@83 25
bgneal@89 26 class Media:
bgneal@95 27 css = {
bgneal@95 28 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
bgneal@95 29 }
bgneal@95 30 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
bgneal@95 31 ('js/forums.js', )
bgneal@89 32
bgneal@89 33 def clean_topic_id(self):
bgneal@89 34 id = self.cleaned_data['topic_id']
bgneal@89 35 print '*********', id
bgneal@89 36 try:
bgneal@89 37 self.topic = Topic.objects.get(pk=id)
bgneal@89 38 print '******** Got a topic'
bgneal@89 39 except Topic.DoesNotExist:
bgneal@89 40 raise forms.ValidationError('invalid topic')
bgneal@89 41 return id
bgneal@89 42
bgneal@89 43 def save(self, user, ip=None):
bgneal@86 44 """
bgneal@86 45 Creates a new post from the form data and supplied arguments.
bgneal@86 46 """
bgneal@89 47 post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'],
bgneal@89 48 user_ip=ip)
bgneal@86 49 post.save()
bgneal@96 50 bump_post_count(user)
bgneal@89 51 return post
bgneal@83 52
bgneal@83 53
bgneal@83 54 class NewTopicForm(forms.Form):
bgneal@83 55 """Form for creating a new topic and 1st post to that topic."""
bgneal@95 56 name = forms.CharField(label='Subject', max_length=255,
bgneal@95 57 widget=forms.TextInput(attrs={'size': 64}))
bgneal@83 58 body = forms.CharField(label='', widget=forms.Textarea)
bgneal@83 59
bgneal@95 60 class Media:
bgneal@95 61 css = {
bgneal@95 62 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
bgneal@95 63 }
bgneal@95 64 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
bgneal@95 65 ('js/forums.js', )
bgneal@95 66
bgneal@83 67 def save(self, forum, user, ip=None):
bgneal@83 68 """
bgneal@83 69 Creates the new Topic and first Post from the form data and supplied
bgneal@86 70 arguments.
bgneal@83 71 """
bgneal@83 72 topic = Topic(forum=forum,
bgneal@83 73 name=self.cleaned_data['name'],
bgneal@83 74 user=user)
bgneal@83 75 topic.save()
bgneal@83 76
bgneal@83 77 post = Post(topic=topic,
bgneal@83 78 user=user,
bgneal@83 79 body=self.cleaned_data['body'],
bgneal@83 80 user_ip=ip)
bgneal@83 81 post.save()
bgneal@83 82
bgneal@96 83 bump_post_count(user)
bgneal@83 84 return topic