comparison gpp/forums/forms.py @ 285:8fd4984d5c3b

This is a first rough commit for #95, adding the ability to embed YouTube videos in forum posts. Some more polish and testing needs to happen at this point. I wanted to get all these changes off my hard drive and into the repository.
author Brian Neal <bgneal@gmail.com>
date Thu, 14 Oct 2010 02:39:35 +0000
parents 13330e1836f3
children 72fd300685d5
comparison
equal deleted inserted replaced
284:df2c81f705a8 285:8fd4984d5c3b
5 from django.conf import settings 5 from django.conf import settings
6 6
7 from forums.models import Forum 7 from forums.models import Forum
8 from forums.models import Topic 8 from forums.models import Topic
9 from forums.models import Post 9 from forums.models import Post
10 from forums.attachments import AttachmentProcessor
10 11
11 12
12 class NewPostForm(forms.Form): 13 class NewPostForm(forms.Form):
13 """Form for creating a new post.""" 14 """Form for creating a new post."""
14 body = forms.CharField(label='', 15 body = forms.CharField(label='',
23 } 24 }
24 js = (settings.GPP_THIRD_PARTY_JS['markitup'] + 25 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
25 settings.GPP_THIRD_PARTY_JS['jquery-ui'] + 26 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
26 ('js/forums.js', )) 27 ('js/forums.js', ))
27 28
29 def __init__(self, *args, **kwargs):
30 super(NewPostForm, self).__init__(*args, **kwargs)
31 attachments = args[0].getlist('attachment') if len(args) else []
32 self.attach_proc = AttachmentProcessor(attachments)
33
28 def clean_topic_id(self): 34 def clean_topic_id(self):
29 id = self.cleaned_data['topic_id'] 35 id = self.cleaned_data['topic_id']
30 try: 36 try:
31 self.topic = Topic.objects.select_related().get(pk=id) 37 self.topic = Topic.objects.select_related().get(pk=id)
32 except Topic.DoesNotExist: 38 except Topic.DoesNotExist:
38 Creates a new post from the form data and supplied arguments. 44 Creates a new post from the form data and supplied arguments.
39 """ 45 """
40 post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'], 46 post = Post(topic=self.topic, user=user, body=self.cleaned_data['body'],
41 user_ip=ip) 47 user_ip=ip)
42 post.save() 48 post.save()
49 self.attach_proc.save_attachments(post)
43 return post 50 return post
44 51
45 52
46 class NewTopicForm(forms.Form): 53 class NewTopicForm(forms.Form):
47 """ 54 """
74 if user.is_superuser or user in forum.moderators.all(): 81 if user.is_superuser or user in forum.moderators.all():
75 self.fields['sticky'] = forms.BooleanField(required=False) 82 self.fields['sticky'] = forms.BooleanField(required=False)
76 self.fields['locked'] = forms.BooleanField(required=False) 83 self.fields['locked'] = forms.BooleanField(required=False)
77 self.has_mod_fields = True 84 self.has_mod_fields = True
78 85
86 attachments = args[0].getlist('attachment') if len(args) else []
87 self.attach_proc = AttachmentProcessor(attachments)
88
79 def save(self, ip=None): 89 def save(self, ip=None):
80 """ 90 """
81 Creates the new Topic and first Post from the form data and supplied 91 Creates the new Topic and first Post from the form data and supplied
82 arguments. 92 arguments.
83 """ 93 """
91 post = Post(topic=topic, 101 post = Post(topic=topic,
92 user=self.user, 102 user=self.user,
93 body=self.cleaned_data['body'], 103 body=self.cleaned_data['body'],
94 user_ip=ip) 104 user_ip=ip)
95 post.save() 105 post.save()
106
107 self.attach_proc.save_attachments(post)
108
96 return topic 109 return topic
97 110
98 111
99 class PostForm(forms.ModelForm): 112 class PostForm(forms.ModelForm):
100 """ 113 """