annotate gpp/forums/forms.py @ 108:80ab249d1adc

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