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@86
|
11 class PostForm(forms.Form):
|
bgneal@86
|
12 """Form for creating a new post."""
|
bgneal@86
|
13 body = forms.CharField(label='', widget=forms.Textarea)
|
bgneal@89
|
14 topic_id = forms.IntegerField(widget=forms.HiddenInput)
|
bgneal@89
|
15 topic = None
|
bgneal@83
|
16
|
bgneal@89
|
17 class Media:
|
bgneal@95
|
18 css = {
|
bgneal@95
|
19 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
|
bgneal@95
|
20 }
|
bgneal@95
|
21 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
|
bgneal@95
|
22 ('js/forums.js', )
|
bgneal@89
|
23
|
bgneal@89
|
24 def clean_topic_id(self):
|
bgneal@89
|
25 id = self.cleaned_data['topic_id']
|
bgneal@89
|
26 print '*********', id
|
bgneal@89
|
27 try:
|
bgneal@89
|
28 self.topic = Topic.objects.get(pk=id)
|
bgneal@89
|
29 print '******** Got a topic'
|
bgneal@89
|
30 except Topic.DoesNotExist:
|
bgneal@89
|
31 raise forms.ValidationError('invalid topic')
|
bgneal@89
|
32 return id
|
bgneal@89
|
33
|
bgneal@89
|
34 def save(self, user, ip=None):
|
bgneal@86
|
35 """
|
bgneal@86
|
36 Creates a new post from the form data and supplied arguments.
|
bgneal@86
|
37 """
|
bgneal@89
|
38 post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'],
|
bgneal@89
|
39 user_ip=ip)
|
bgneal@86
|
40 post.save()
|
bgneal@89
|
41 return post
|
bgneal@83
|
42
|
bgneal@83
|
43
|
bgneal@83
|
44 class NewTopicForm(forms.Form):
|
bgneal@83
|
45 """Form for creating a new topic and 1st post to that topic."""
|
bgneal@95
|
46 name = forms.CharField(label='Subject', max_length=255,
|
bgneal@95
|
47 widget=forms.TextInput(attrs={'size': 64}))
|
bgneal@83
|
48 body = forms.CharField(label='', widget=forms.Textarea)
|
bgneal@83
|
49
|
bgneal@95
|
50 class Media:
|
bgneal@95
|
51 css = {
|
bgneal@95
|
52 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
|
bgneal@95
|
53 }
|
bgneal@95
|
54 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
|
bgneal@95
|
55 ('js/forums.js', )
|
bgneal@95
|
56
|
bgneal@83
|
57 def save(self, forum, user, ip=None):
|
bgneal@83
|
58 """
|
bgneal@83
|
59 Creates the new Topic and first Post from the form data and supplied
|
bgneal@86
|
60 arguments.
|
bgneal@83
|
61 """
|
bgneal@83
|
62 topic = Topic(forum=forum,
|
bgneal@83
|
63 name=self.cleaned_data['name'],
|
bgneal@83
|
64 user=user)
|
bgneal@83
|
65 topic.save()
|
bgneal@83
|
66
|
bgneal@83
|
67 post = Post(topic=topic,
|
bgneal@83
|
68 user=user,
|
bgneal@83
|
69 body=self.cleaned_data['body'],
|
bgneal@83
|
70 user_ip=ip)
|
bgneal@83
|
71 post.save()
|
bgneal@83
|
72
|
bgneal@83
|
73 return topic
|