bgneal@83: """ bgneal@83: Forms for the forums application. bgneal@83: """ bgneal@83: from django import forms bgneal@95: from django.conf import settings bgneal@83: bgneal@83: from forums.models import Topic bgneal@83: from forums.models import Post bgneal@83: bgneal@83: bgneal@96: def bump_post_count(user): bgneal@96: """ bgneal@96: Increments the forum_post_count for the given user. bgneal@96: """ bgneal@96: profile = user.get_profile() bgneal@96: profile.forum_post_count += 1 bgneal@96: profile.save() bgneal@96: bgneal@96: bgneal@86: class PostForm(forms.Form): bgneal@86: """Form for creating a new post.""" bgneal@86: body = forms.CharField(label='', widget=forms.Textarea) bgneal@89: topic_id = forms.IntegerField(widget=forms.HiddenInput) bgneal@89: topic = None bgneal@83: bgneal@89: class Media: bgneal@95: css = { bgneal@95: 'all': settings.GPP_THIRD_PARTY_CSS['markitup'], bgneal@95: } bgneal@95: js = settings.GPP_THIRD_PARTY_JS['markitup'] + \ bgneal@95: ('js/forums.js', ) bgneal@89: bgneal@89: def clean_topic_id(self): bgneal@89: id = self.cleaned_data['topic_id'] bgneal@89: try: bgneal@102: self.topic = Topic.objects.select_related().get(pk=id) bgneal@89: except Topic.DoesNotExist: bgneal@89: raise forms.ValidationError('invalid topic') bgneal@89: return id bgneal@89: bgneal@89: def save(self, user, ip=None): bgneal@86: """ bgneal@86: Creates a new post from the form data and supplied arguments. bgneal@86: """ bgneal@89: post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'], bgneal@89: user_ip=ip) bgneal@86: post.save() bgneal@96: bump_post_count(user) bgneal@89: return post bgneal@83: bgneal@83: bgneal@83: class NewTopicForm(forms.Form): bgneal@102: """ bgneal@102: Form for creating a new topic and 1st post to that topic. bgneal@102: Superusers and moderators can also create the topic as a sticky or initially bgneal@102: locked. bgneal@102: """ bgneal@95: name = forms.CharField(label='Subject', max_length=255, bgneal@95: widget=forms.TextInput(attrs={'size': 64})) bgneal@83: body = forms.CharField(label='', widget=forms.Textarea) bgneal@102: user = None bgneal@102: forum = None bgneal@102: has_mod_fields = False bgneal@83: bgneal@95: class Media: bgneal@95: css = { bgneal@95: 'all': settings.GPP_THIRD_PARTY_CSS['markitup'], bgneal@95: } bgneal@95: js = settings.GPP_THIRD_PARTY_JS['markitup'] + \ bgneal@95: ('js/forums.js', ) bgneal@95: bgneal@102: def __init__(self, user, forum, *args, **kwargs): bgneal@102: super(NewTopicForm, self).__init__(*args, **kwargs) bgneal@102: self.user = user bgneal@102: self.forum = forum bgneal@102: bgneal@102: if user.is_superuser or user in forum.moderators.all(): bgneal@102: self.fields['sticky'] = forms.BooleanField(required=False) bgneal@102: self.fields['locked'] = forms.BooleanField(required=False) bgneal@102: self.has_mod_fields = True bgneal@102: bgneal@102: def save(self, ip=None): bgneal@83: """ bgneal@83: Creates the new Topic and first Post from the form data and supplied bgneal@86: arguments. bgneal@83: """ bgneal@102: topic = Topic(forum=self.forum, bgneal@83: name=self.cleaned_data['name'], bgneal@102: user=self.user, bgneal@102: sticky=self.has_mod_fields and self.cleaned_data['sticky'], bgneal@102: locked=self.has_mod_fields and self.cleaned_data['locked']) bgneal@83: topic.save() bgneal@83: bgneal@83: post = Post(topic=topic, bgneal@102: user=self.user, bgneal@83: body=self.cleaned_data['body'], bgneal@83: user_ip=ip) bgneal@83: post.save() bgneal@83: bgneal@102: bump_post_count(self.user) bgneal@83: return topic