annotate gpp/forums/forms.py @ 107:e94398f5e027

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