Total Complexity | 2 |
Total Lines | 27 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | from django import forms |
||
6 | class ThreadCreationForm(forms.Form): |
||
7 | """ Form for creating new threads """ |
||
8 | topic = forms.ModelChoiceField(queryset=models.Topic.objects.all()) |
||
9 | title = forms.CharField(max_length=200) |
||
10 | body = forms.CharField(label='Post Body', widget=forms.Textarea) |
||
11 | |||
12 | def save(self, user): |
||
13 | """ Save the contents of the form. |
||
14 | |||
15 | Uses the title field to create a new thread, and uses the body |
||
16 | field to create a new message associated with the created |
||
17 | thread. |
||
18 | """ |
||
19 | if self.is_valid(): |
||
20 | |||
21 | topic = self.cleaned_data['topic'] |
||
22 | |||
23 | thread = models.Thread.objects.create( |
||
24 | topic=topic, |
||
25 | title=self.cleaned_data['title']) |
||
26 | |||
27 | models.Message.objects.create( |
||
28 | user=user, |
||
29 | thread=thread, |
||
30 | body=self.cleaned_data['body']) |
||
31 | |||
32 | return thread |
||
33 | |||
49 |