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@110
|
7 from forums.models import Forum
|
bgneal@83
|
8 from forums.models import Topic
|
bgneal@83
|
9 from forums.models import Post
|
bgneal@83
|
10
|
bgneal@83
|
11
|
bgneal@106
|
12 class NewPostForm(forms.Form):
|
bgneal@86
|
13 """Form for creating a new post."""
|
bgneal@86
|
14 body = forms.CharField(label='', widget=forms.Textarea)
|
bgneal@89
|
15 topic_id = forms.IntegerField(widget=forms.HiddenInput)
|
bgneal@89
|
16 topic = None
|
bgneal@83
|
17
|
bgneal@89
|
18 class Media:
|
bgneal@95
|
19 css = {
|
bgneal@95
|
20 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
|
bgneal@95
|
21 }
|
bgneal@95
|
22 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
|
bgneal@95
|
23 ('js/forums.js', )
|
bgneal@89
|
24
|
bgneal@89
|
25 def clean_topic_id(self):
|
bgneal@89
|
26 id = self.cleaned_data['topic_id']
|
bgneal@89
|
27 try:
|
bgneal@102
|
28 self.topic = Topic.objects.select_related().get(pk=id)
|
bgneal@89
|
29 except Topic.DoesNotExist:
|
bgneal@89
|
30 raise forms.ValidationError('invalid topic')
|
bgneal@89
|
31 return id
|
bgneal@89
|
32
|
bgneal@89
|
33 def save(self, user, ip=None):
|
bgneal@86
|
34 """
|
bgneal@86
|
35 Creates a new post from the form data and supplied arguments.
|
bgneal@86
|
36 """
|
bgneal@89
|
37 post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'],
|
bgneal@89
|
38 user_ip=ip)
|
bgneal@86
|
39 post.save()
|
bgneal@89
|
40 return post
|
bgneal@83
|
41
|
bgneal@83
|
42
|
bgneal@83
|
43 class NewTopicForm(forms.Form):
|
bgneal@102
|
44 """
|
bgneal@102
|
45 Form for creating a new topic and 1st post to that topic.
|
bgneal@102
|
46 Superusers and moderators can also create the topic as a sticky or initially
|
bgneal@102
|
47 locked.
|
bgneal@102
|
48 """
|
bgneal@95
|
49 name = forms.CharField(label='Subject', max_length=255,
|
bgneal@95
|
50 widget=forms.TextInput(attrs={'size': 64}))
|
bgneal@83
|
51 body = forms.CharField(label='', widget=forms.Textarea)
|
bgneal@102
|
52 user = None
|
bgneal@102
|
53 forum = None
|
bgneal@102
|
54 has_mod_fields = False
|
bgneal@83
|
55
|
bgneal@95
|
56 class Media:
|
bgneal@95
|
57 css = {
|
bgneal@95
|
58 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
|
bgneal@95
|
59 }
|
bgneal@95
|
60 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
|
bgneal@95
|
61 ('js/forums.js', )
|
bgneal@95
|
62
|
bgneal@102
|
63 def __init__(self, user, forum, *args, **kwargs):
|
bgneal@102
|
64 super(NewTopicForm, self).__init__(*args, **kwargs)
|
bgneal@102
|
65 self.user = user
|
bgneal@102
|
66 self.forum = forum
|
bgneal@102
|
67
|
bgneal@102
|
68 if user.is_superuser or user in forum.moderators.all():
|
bgneal@102
|
69 self.fields['sticky'] = forms.BooleanField(required=False)
|
bgneal@102
|
70 self.fields['locked'] = forms.BooleanField(required=False)
|
bgneal@102
|
71 self.has_mod_fields = True
|
bgneal@102
|
72
|
bgneal@102
|
73 def save(self, ip=None):
|
bgneal@83
|
74 """
|
bgneal@83
|
75 Creates the new Topic and first Post from the form data and supplied
|
bgneal@86
|
76 arguments.
|
bgneal@83
|
77 """
|
bgneal@102
|
78 topic = Topic(forum=self.forum,
|
bgneal@83
|
79 name=self.cleaned_data['name'],
|
bgneal@102
|
80 user=self.user,
|
bgneal@102
|
81 sticky=self.has_mod_fields and self.cleaned_data['sticky'],
|
bgneal@102
|
82 locked=self.has_mod_fields and self.cleaned_data['locked'])
|
bgneal@83
|
83 topic.save()
|
bgneal@83
|
84
|
bgneal@83
|
85 post = Post(topic=topic,
|
bgneal@102
|
86 user=self.user,
|
bgneal@83
|
87 body=self.cleaned_data['body'],
|
bgneal@83
|
88 user_ip=ip)
|
bgneal@83
|
89 post.save()
|
bgneal@83
|
90 return topic
|
bgneal@106
|
91
|
bgneal@106
|
92
|
bgneal@106
|
93 class PostForm(forms.ModelForm):
|
bgneal@106
|
94 """
|
bgneal@108
|
95 Form for editing an existing post or a new, non-quick post.
|
bgneal@106
|
96 """
|
bgneal@106
|
97 body = forms.CharField(label='', widget=forms.Textarea)
|
bgneal@106
|
98
|
bgneal@106
|
99 class Meta:
|
bgneal@106
|
100 model = Post
|
bgneal@106
|
101 fields = ('body', )
|
bgneal@106
|
102
|
bgneal@106
|
103 class Media:
|
bgneal@106
|
104 css = {
|
bgneal@106
|
105 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
|
bgneal@106
|
106 }
|
bgneal@106
|
107 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
|
bgneal@106
|
108 ('js/forums.js', )
|
bgneal@110
|
109
|
bgneal@110
|
110
|
bgneal@110
|
111 class MoveTopicForm(forms.Form):
|
bgneal@111
|
112 """
|
bgneal@111
|
113 Form for a moderator to move a topic to a forum.
|
bgneal@111
|
114 """
|
bgneal@111
|
115 forums = forms.ModelChoiceField(label='Move to forum',
|
bgneal@111
|
116 queryset=Forum.objects.none())
|
bgneal@110
|
117
|
bgneal@111
|
118 def __init__(self, user, *args, **kwargs):
|
bgneal@111
|
119 hide_label = kwargs.pop('hide_label', False)
|
bgneal@111
|
120 required = kwargs.pop('required', True)
|
bgneal@111
|
121 super(MoveTopicForm, self).__init__(*args, **kwargs)
|
bgneal@111
|
122 self.fields['forums'].queryset = \
|
bgneal@111
|
123 Forum.objects.forums_for_user(user).order_by('name')
|
bgneal@111
|
124 if hide_label:
|
bgneal@111
|
125 self.fields['forums'].label = ''
|
bgneal@111
|
126 self.fields['forums'].required = required
|
bgneal@110
|
127
|