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 try:
|
bgneal@89
|
36 self.topic = Topic.objects.get(pk=id)
|
bgneal@89
|
37 except Topic.DoesNotExist:
|
bgneal@89
|
38 raise forms.ValidationError('invalid topic')
|
bgneal@89
|
39 return id
|
bgneal@89
|
40
|
bgneal@89
|
41 def save(self, user, ip=None):
|
bgneal@86
|
42 """
|
bgneal@86
|
43 Creates a new post from the form data and supplied arguments.
|
bgneal@86
|
44 """
|
bgneal@89
|
45 post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'],
|
bgneal@89
|
46 user_ip=ip)
|
bgneal@86
|
47 post.save()
|
bgneal@96
|
48 bump_post_count(user)
|
bgneal@89
|
49 return post
|
bgneal@83
|
50
|
bgneal@83
|
51
|
bgneal@83
|
52 class NewTopicForm(forms.Form):
|
bgneal@83
|
53 """Form for creating a new topic and 1st post to that topic."""
|
bgneal@95
|
54 name = forms.CharField(label='Subject', max_length=255,
|
bgneal@95
|
55 widget=forms.TextInput(attrs={'size': 64}))
|
bgneal@83
|
56 body = forms.CharField(label='', widget=forms.Textarea)
|
bgneal@83
|
57
|
bgneal@95
|
58 class Media:
|
bgneal@95
|
59 css = {
|
bgneal@95
|
60 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
|
bgneal@95
|
61 }
|
bgneal@95
|
62 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
|
bgneal@95
|
63 ('js/forums.js', )
|
bgneal@95
|
64
|
bgneal@83
|
65 def save(self, forum, user, ip=None):
|
bgneal@83
|
66 """
|
bgneal@83
|
67 Creates the new Topic and first Post from the form data and supplied
|
bgneal@86
|
68 arguments.
|
bgneal@83
|
69 """
|
bgneal@83
|
70 topic = Topic(forum=forum,
|
bgneal@83
|
71 name=self.cleaned_data['name'],
|
bgneal@83
|
72 user=user)
|
bgneal@83
|
73 topic.save()
|
bgneal@83
|
74
|
bgneal@83
|
75 post = Post(topic=topic,
|
bgneal@83
|
76 user=user,
|
bgneal@83
|
77 body=self.cleaned_data['body'],
|
bgneal@83
|
78 user_ip=ip)
|
bgneal@83
|
79 post.save()
|
bgneal@83
|
80
|
bgneal@96
|
81 bump_post_count(user)
|
bgneal@83
|
82 return topic
|