comparison gpp/forums/forms.py @ 102:e67c4dd98db5

Forums: new topic form sprouts boolean fields for sticky and locking if the user has rights. Implemented the locked logic. Fixed a bug where topics where getting out of order (the view_count was bumping the update_date because of auto_now).
author Brian Neal <bgneal@gmail.com>
date Wed, 16 Sep 2009 02:01:57 +0000
parents eb9f99382476
children cb72577785df
comparison
equal deleted inserted replaced
101:4bbb6a9aa317 102:e67c4dd98db5
31 ('js/forums.js', ) 31 ('js/forums.js', )
32 32
33 def clean_topic_id(self): 33 def clean_topic_id(self):
34 id = self.cleaned_data['topic_id'] 34 id = self.cleaned_data['topic_id']
35 try: 35 try:
36 self.topic = Topic.objects.get(pk=id) 36 self.topic = Topic.objects.select_related().get(pk=id)
37 except Topic.DoesNotExist: 37 except Topic.DoesNotExist:
38 raise forms.ValidationError('invalid topic') 38 raise forms.ValidationError('invalid topic')
39 return id 39 return id
40 40
41 def save(self, user, ip=None): 41 def save(self, user, ip=None):
48 bump_post_count(user) 48 bump_post_count(user)
49 return post 49 return post
50 50
51 51
52 class NewTopicForm(forms.Form): 52 class NewTopicForm(forms.Form):
53 """Form for creating a new topic and 1st post to that topic.""" 53 """
54 Form for creating a new topic and 1st post to that topic.
55 Superusers and moderators can also create the topic as a sticky or initially
56 locked.
57 """
54 name = forms.CharField(label='Subject', max_length=255, 58 name = forms.CharField(label='Subject', max_length=255,
55 widget=forms.TextInput(attrs={'size': 64})) 59 widget=forms.TextInput(attrs={'size': 64}))
56 body = forms.CharField(label='', widget=forms.Textarea) 60 body = forms.CharField(label='', widget=forms.Textarea)
61 user = None
62 forum = None
63 has_mod_fields = False
57 64
58 class Media: 65 class Media:
59 css = { 66 css = {
60 'all': settings.GPP_THIRD_PARTY_CSS['markitup'], 67 'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
61 } 68 }
62 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \ 69 js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
63 ('js/forums.js', ) 70 ('js/forums.js', )
64 71
65 def save(self, forum, user, ip=None): 72 def __init__(self, user, forum, *args, **kwargs):
73 super(NewTopicForm, self).__init__(*args, **kwargs)
74 self.user = user
75 self.forum = forum
76
77 if user.is_superuser or user in forum.moderators.all():
78 self.fields['sticky'] = forms.BooleanField(required=False)
79 self.fields['locked'] = forms.BooleanField(required=False)
80 self.has_mod_fields = True
81
82 def save(self, ip=None):
66 """ 83 """
67 Creates the new Topic and first Post from the form data and supplied 84 Creates the new Topic and first Post from the form data and supplied
68 arguments. 85 arguments.
69 """ 86 """
70 topic = Topic(forum=forum, 87 topic = Topic(forum=self.forum,
71 name=self.cleaned_data['name'], 88 name=self.cleaned_data['name'],
72 user=user) 89 user=self.user,
90 sticky=self.has_mod_fields and self.cleaned_data['sticky'],
91 locked=self.has_mod_fields and self.cleaned_data['locked'])
73 topic.save() 92 topic.save()
74 93
75 post = Post(topic=topic, 94 post = Post(topic=topic,
76 user=user, 95 user=self.user,
77 body=self.cleaned_data['body'], 96 body=self.cleaned_data['body'],
78 user_ip=ip) 97 user_ip=ip)
79 post.save() 98 post.save()
80 99
81 bump_post_count(user) 100 bump_post_count(self.user)
82 return topic 101 return topic