def __create_testpost(self): # Create post ... p = Post(category=self.c, subject='Just a test post', body='hehe', author=self.testuser) p.save() return p
def post(request, group = None, category_id = None, post_id = None, thread_id = None): if 'type' in request.REQUEST and request.REQUEST['type'] == 'preview': previewpost = Post( body = request.REQUEST['body'], markup = request.REQUEST.get('markup', None), ) return HttpResponse( unicode(previewpost.body_escaped()) ) post = None thread = None category = None context = { } if post_id is None and 'post_id' in request.REQUEST: # if no post_id is given take it from the request. post_id = request.REQUEST['post_id'] if post_id: try: post = Post.allobjects.get( pk = post_id ) except Post.DoesNotExist: raise Http404 if not post.allowEditing(): raise PermissionDenied() thread = post.thread if 'thread' in request.REQUEST: thread_id = request.REQUEST['thread'] if thread_id: try: thread = Post.allobjects.get( pk = thread_id ) except Post.DoesNotExist: raise Http404 category = thread.category context['thread'] = thread if not thread.allowPosting( request.user ): raise PermissionDenied() else: category = get_object_or_404(Category, pk = category_id) if not category.allowPostThread( request.user ): raise PermissionDenied() context['category'] = category category_type = category.get_category_type() MyPostForm = PostForm if category_type is not None: MyPostForm = category_type.get_post_form_class(thread, post) attachmentForm = None allow_attachments = get_sph_setting('board_allow_attachments') if request.method == 'POST': postForm = MyPostForm(request.POST) postForm.init_for_category_type(category_type, post) pollForm = PostPollForm(request.POST) create_post = True if allow_attachments: attachmentForm = PostAttachmentForm(request.POST, request.FILES, prefix = 'attachment') if 'cmd_addfile' in request.POST: create_post = False if attachmentForm.is_valid(): attachment = attachmentForm.save(commit = False) if attachment.fileupload: # Only save attachments if there was an upload... # If the form is valid, store the attachment if not post: # if there is no post yet.. we need to create a draft post = Post( category = category, author = request.user, thread = thread, is_hidden = 1, ) post.set_new( True ) post.save() # Reference the post and save the attachment attachment.post = post attachment.save() if create_post \ and postForm.is_valid() \ and ('createpoll' not in request.POST \ or pollForm.is_valid()): data = postForm.cleaned_data if post: newpost = post newpost.subject = data['subject'] newpost.body = data['body'] # make post visible newpost.is_hidden = 0 if not post.is_new() and category_type.append_edit_message_to_post(post): newpost.body += "\n\n" + _(u'--- Last Edited by %(username)s at %(edit_date)s ---') % {'username':get_user_displayname( request.user ), 'edit_date':format_date( datetime.today())} else: user = request.user.is_authenticated() and request.user or None newpost = Post( category = category, subject = data['subject'], body = data['body'], author = user, thread = thread, ) if 'markup' in data: newpost.markup = data['markup'] elif len( POST_MARKUP_CHOICES ) == 1: newpost.markup = POST_MARKUP_CHOICES[0][0] newpost.save() category_type.save_post( newpost, data ) # Creating monitor if request.POST.get( 'addmonitor', False ): newpost.toggle_monitor() if 'createpoll' in request.POST and request.POST['createpoll'] == '1': newpost.set_poll( True ); newpost.save() # Creating poll... polldata = pollForm.cleaned_data newpoll = Poll( post = newpost, question = polldata['question'], choices_per_user = polldata['choicesPerUser']) newpoll.save() choices = polldata['answers'].splitlines() i=0 for choice in choices: pollchoice = PollChoice( poll = newpoll, choice = choice, count = 0, sortorder = i) i+=1 pollchoice.save() if request.user.is_authenticated(): request.user.message_set.create( message = ugettext(u'Vote created successfully.') ) if request.user.is_authenticated(): if post: request.user.message_set.create( message = ugettext(u'Post edited successfully.') ) else: request.user.message_set.create( message = ugettext(u'Post created successfully.') ) if thread == None: thread = newpost return HttpResponseRedirect( newpost.get_absolute_url() ) else: postForm = MyPostForm( ) postForm.init_for_category_type(category_type, post) pollForm = PostPollForm() if allow_attachments: attachmentForm = PostAttachmentForm(prefix = 'attachment') if post: postForm.fields['subject'].initial = post.subject postForm.fields['body'].initial = post.body if 'markup' in postForm.fields: postForm.fields['markup'].initial = post.markup context['post'] = post context['thread'] = post.thread or post elif 'quote' in request.REQUEST: quotepost = Post.objects.get( pk = request.REQUEST['quote'] ) postForm.fields['subject'].initial = 'Re: %s' % thread.subject if quotepost.author == None: username = '******' else: username = quotepost.author.username postForm.fields['body'].initial = '[quote=%s;%s]\n%s\n[/quote]\n' % (username, quotepost.id, quotepost.body) elif thread: postForm.fields['subject'].initial = 'Re: %s' % thread.subject context['form'] = postForm # Only allow polls if this is a new _thread_ (not a reply) if (not thread and not post) or (post and post.is_new() and post.thread is None): context['pollform'] = pollForm context['attachmentForm'] = attachmentForm if 'createpoll' in request.REQUEST: context['createpoll'] = request.REQUEST['createpoll'] res = render_to_response( "sphene/sphboard/post.html", context, context_instance = RequestContext(request) ) # Maybe the user is in the 'edit' form, which should not be cached. res.sph_lastmodified = True return res
class PostMovingTester(TestCase): def setUp(self): """ We have 2 categories cat1 and cat2 and 2 threads (total 4 posts): cat1/ -- category cat1 cat1_p1 -- thread/post c1_p1 in category cat1 cat1_p2 -- post c1_p2 in thread cat1_p1 cat1_p3 -- post c1_p3 in thread cat1_p1 cat2/ -- category cat2 cat2_p1 -- thread/post cat2_p1 in category cat2 """ self.testuser = testutils.get_testuser() self.superuser = testutils.get_superuser() self.testgroup = testutils.get_testgroup() testutils.setup_threadlocals(self.testuser, self.testgroup) # Setup test role .. self.testrole = testutils.get_testrole() # Test category 1 self.cat1 = Category(name='Category 1', allowview=3, allowreplies=3, allowthreads=3, group=self.testgroup) self.cat1.save() self.cat2 = Category(name='Category 2', allowview=3, allowreplies=3, allowthreads=3, group=self.testgroup) self.cat2.save() # create thread 1 in category c1 self.cat1_p1 = Post(category=self.cat1, subject='Post p1 in category c1', body="post 1", markup='bbcode', author=self.testuser) self.cat1_p1.save() self.cat1_p2 = Post(category=self.cat1, subject='Post p2 in category c1', body="post 2", markup='bbcode', thread=self.cat1_p1, author=self.testuser) self.cat1_p2.save() self.cat1_p3 = Post(category=self.cat1, subject='Post p3 in category c1', body="post 3", markup='bbcode', thread=self.cat1_p1, author=self.testuser) self.cat1_p3.save() # create thread 2 in category cat2 self.cat2_p1 = Post(category=self.cat2, subject='Post p1 in category cat2', body="post 1", markup='bbcode', author=self.testuser) self.cat2_p1.save() # log in the user logged_in = self.client.login(username='******', password='******') def test_move_cat1_p2_to_cat2(self): """ Test moving post p2 from category cat1 directly into category cat2. Expected output is to have new thread (created from post p2) in category cat2. Thread cat1_p1 in category c1 will have less posts now """ mv1url = self.cat1_p2.get_absolute_moveposturl() self.assertEqual( mv1url, sph_reverse('move_post_1', kwargs={'post_id': self.cat1_p2.pk})) # check first step response = self.client.get(mv1url, {}) self.assertEqual(response.status_code, 200) # check second step (category is selected) mv2url = sph_reverse('move_post_2', kwargs={ 'post_id': self.cat1_p2.pk, 'category_id': self.cat2.pk }) response = self.client.get(mv2url, {}) self.assertEqual(response.status_code, 200) # check step 3 (with GET) - annotation form for post moved into category mv3url = sph_reverse('move_post_3', kwargs={ 'post_id': self.cat1_p2.pk, 'category_id': self.cat2.pk }) response = self.client.get(mv3url, {}) self.assertEqual(response.status_code, 200) # submit annotation form and move the post! self.assertEqual(self.cat2.threadCount(), 1) response = self.client.post(mv3url, {'body': 'test body'}) self.assertEqual(response.status_code, 302) # get fresh instance of moved post p2 = Post.objects.get(pk=self.cat1_p2.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in category cat2 self.assertEqual(self.cat2.threadCount(), 2) # check if post p2 was removed form thread p1 self.assertEqual(self.cat1_p1.postCount(), 2) # check if post p2 is now new thread in category cat2 self.assertEqual(p2.get_thread(), p2) # check if ThreadInformation for post p1 was updated ti = self.cat1_p1.get_threadinformation() self.assertEqual(ti.post_count, 2) # check if number of ThreadInformation objects has been changed self.assertEqual(ThreadInformation.objects.all().count(), 3) # check ThreadInformation for new thread ti2 = p2.get_threadinformation() self.assertEqual(ti2.post_count, 1) def test_move_cat1_p1_to_cat2(self): """ Test moving post p1 (root post of thread!) from category cat1 directly into category cat2. Expected output is to have new thread (created from post p1) in category cat2 and new thread in category cat1 created from second post in former p1 thread. Old ThreadInformation object for thread p1 should be removed. Two new ThreadInformation objects will be crated """ mv3url = sph_reverse('move_post_3', kwargs={ 'post_id': self.cat1_p1.pk, 'category_id': self.cat2.pk }) # submit annotation form and move the post! self.assertEqual(self.cat2.threadCount(), 1) response = self.client.post(mv3url, {'body': 'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts p1 = Post.objects.get(pk=self.cat1_p1.pk) p2 = Post.objects.get(pk=self.cat1_p2.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in category cat2 self.assertEqual(self.cat2.threadCount(), 2) # check if post p2 is now thread self.assertEqual(p2.get_thread(), p2) # check if post p1 is now new thread in category cat2 self.assertEqual(p1.get_thread(), p1) # check if ThreadInformation for post p2 was created properly ti = p2.get_threadinformation() self.assertEqual(ti.post_count, 2) # check if number of ThreadInformation objects has been changed self.assertEqual(ThreadInformation.objects.all().count(), 3) def test_move_cat1_p1_to_cat1(self): """ Test moving post p1 (root post of thread!) from category c1 directly into category cat1. Expected output is to have new thread (created from post p1) in category c1 and new thread in category c1 created from second post in former p1 thread. Old ThreadInformation object for thread p1 should be removed. Two new ThreadInformation objects will be crated """ mv3url = sph_reverse('move_post_3', kwargs={ 'post_id': self.cat1_p1.pk, 'category_id': self.cat1.pk }) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body': 'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts p1 = Post.objects.get(pk=self.cat1_p1.pk) p2 = Post.objects.get(pk=self.cat1_p2.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in category cat1 self.assertEqual(self.cat1.threadCount(), 2) # check if post p2 is now thread self.assertEqual(p2.get_thread(), p2) # check if post p1 is now new thread in category cat1 self.assertEqual(p1.get_thread(), p1) # check if ThreadInformation for post p2 was created properly ti = p2.get_threadinformation() self.assertEqual(ti.post_count, 2) self.assertEqual(ti.category, self.cat1) # check if ThreadInformation for post p1 was updated properly ti2 = p1.get_threadinformation() self.assertEqual(ti2.post_count, 1) self.assertEqual(ti2.category, self.cat1) # check if number of ThreadInformation objects has been changed self.assertEqual(ThreadInformation.objects.all().count(), 3) def test_move_cat1_p3_to_cat1(self): """ Test moving post p3 from category cat1 directly into category cat1. Expected output is to have new thread (created from post p3) in category c1. Old ThreadInformation object for thread p1 should be updated. """ mv3url = sph_reverse('move_post_3', kwargs={ 'post_id': self.cat1_p3.pk, 'category_id': self.cat1.pk }) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body': 'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts p1 = Post.objects.get(pk=self.cat1_p1.pk) p3 = Post.objects.get(pk=self.cat1_p3.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in category cat1 self.assertEqual(self.cat1.threadCount(), 2) # check if post p3 is now thread self.assertEqual(p3.get_thread(), p3) # check if ThreadInformation for post p3 was created properly ti = p3.get_threadinformation() self.assertEqual(ti.post_count, 1) self.assertEqual(ti.category, self.cat1) # check if ThreadInformation for post p1 was updated properly ti = p1.get_threadinformation() self.assertEqual(ti.post_count, 2) def test_move_cat2_p1_to_cat1(self): """ Test moving post p1 from category cat2 directly into category cat1. Expected output is to have new thread (created from post cat2_p1) in category c1. Old ThreadInformation object from cat2 should be removed. """ mv3url = sph_reverse('move_post_3', kwargs={ 'post_id': self.cat2_p1.pk, 'category_id': self.cat1.pk }) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body': 'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts cat2_p1 = Post.objects.get(pk=self.cat2_p1.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in cat1 self.assertEqual(self.cat1.threadCount(), 2) # check if no threads left in cat2 self.assertEqual(self.cat2.threadCount(), 0) # check if post cat2_p1 is thread self.assertEqual(cat2_p1.get_thread(), cat2_p1) # check if ThreadInformation for post cat2_p1 was created properly ti = cat2_p1.get_threadinformation() self.assertEqual(ti.post_count, 1) self.assertEqual(ti.category, self.cat1) def test_move_cat1_p3_to_cat2_p1(self): """ Test moving post p3 from thread cat1_p1 into thread cat2_p1. Expected output is to have thread cat2_p1 updated and containing 2 posts. Thread cat1_p1 should be updated too as it now contains only 2 posts """ mv3url = sph_reverse('move_post_3', kwargs={ 'post_id': self.cat1_p3.pk, 'category_id': self.cat1.pk, 'thread_id': self.cat2_p1.pk }) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body': 'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts p1 = Post.objects.get(pk=self.cat1_p1.pk) p3 = Post.objects.get(pk=self.cat1_p3.pk) cat2_p1 = Post.objects.get(pk=self.cat2_p1.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if thread cat2_p1 was updated ti = cat2_p1.get_threadinformation() self.assertEqual(ti.post_count, 2) # check if ThreadInformation for post p1 was updated properly ti = p1.get_threadinformation() self.assertEqual(ti.post_count, 2) self.assertEqual(ti.category, self.cat1) # check if post cat1_p3 is now in thread cat2_p1 self.assertEqual(p3.get_thread(), cat2_p1) def test_move_cat1_p1_to_cat2_p1(self): """ Test moving post cat1_p1 (root post of thread!) from thread cat1_p1 into thread cat2_p1. Expected output is to have thread cat2_p1 updated and containing 2 posts. Thread cat1_p1 should be updated too as it now contains only 2 posts """ mv3url = sph_reverse('move_post_3', kwargs={ 'post_id': self.cat1_p1.pk, 'category_id': self.cat1.pk, 'thread_id': self.cat2_p1.pk }) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body': 'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts cat1_p1 = Post.objects.get(pk=self.cat1_p1.pk) cat1_p2 = Post.objects.get(pk=self.cat1_p2.pk) cat2_p1 = Post.objects.get(pk=self.cat2_p1.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if thread cat2_p1 was updated ti = cat2_p1.get_threadinformation() self.assertEqual(ti.post_count, 2) # check if ThreadInformation for post p2 was created properly ti = cat1_p2.get_threadinformation() self.assertEqual(ti.post_count, 2) self.assertEqual(ti.category, self.cat1) # check if post cat1_p1 is now in thread cat2_p1 self.assertEqual(cat1_p1.get_thread(), cat2_p1) # check if post cat1_p1 was added at the end of thread self.assertEqual(cat2_p1.get_latest_post(), cat1_p1)
def post(request, group=None, category_id=None, post_id=None, thread_id=None): """ View method to allow users to: - create new threads (post_id and thread_id is None) - reply to threads (post_id is None) - edit posts (post_id is the post which should be edited, thread_id is None) post_id and thread_id can either be passed in by URL (named parameters to this method) or by request.REQUEST parameters. """ if "type" in request.REQUEST and request.REQUEST["type"] == "preview": # If user just wants a preview, simply create a dummy post so it can be rendered. previewpost = Post(body=request.REQUEST["body"], markup=request.REQUEST.get("markup", None)) return HttpResponse(unicode(previewpost.body_escaped())) # All available objects should be loaded from the _id variables. post = None thread = None category = None context = { "bbcodewysiwyg": enable_wysiwyg_editor() or (get_sph_setting("board_wysiwyg_testing") and request.REQUEST.get("wysiwyg", False)) } if post_id is None and "post_id" in request.REQUEST: # if no post_id is given take it from the request. post_id = request.REQUEST["post_id"] if post_id is not None: # User wants to edit a post .. try: post = Post.allobjects.get(pk=post_id) except Post.DoesNotExist: raise Http404 if not post.allowEditing(): raise PermissionDenied() thread = post.thread category = post.category else: # User wants to create a new post (thread or reply) if "thread" in request.REQUEST: thread_id = request.REQUEST["thread"] if thread_id is not None: # User is posting (replying) to a thread. try: thread = Post.allobjects.get(pk=thread_id) except Post.DoesNotExist: raise Http404 category = thread.category if not thread.allowPosting(request.user): raise PermissionDenied() else: # User is creating a new thread. category = get_object_or_404(Category, pk=category_id) if not category.allowPostThread(request.user): raise PermissionDenied() context["thread"] = thread context["category"] = category category_type = category.get_category_type() MyPostForm = PostForm if category_type is not None: MyPostForm = category_type.get_post_form_class(thread, post) attachmentForm = None allow_attachments = get_sph_setting("board_allow_attachments") if request.method == "POST": postForm = MyPostForm(request.POST) postForm.init_for_category_type(category_type, post) pollForm = PostPollForm(request.POST) create_post = True if allow_attachments: attachmentForm = PostAttachmentForm(request.POST, request.FILES, prefix="attachment") if "cmd_addfile" in request.POST: create_post = False if attachmentForm.is_valid(): attachment = attachmentForm.save(commit=False) if attachment.fileupload: # Only save attachments if there was an upload... # If the form is valid, store the attachment if not post: # if there is no post yet.. we need to create a draft post = Post(category=category, author=request.user, thread=thread, is_hidden=1) post.set_new(True) post.save() # Reference the post and save the attachment attachment.post = post attachment.save() if create_post and postForm.is_valid() and ("createpoll" not in request.POST or pollForm.is_valid()): data = postForm.cleaned_data if post: newpost = post newpost.subject = data["subject"] newpost.body = data["body"] # make post visible newpost.is_hidden = 0 if not post.is_new() and category_type.append_edit_message_to_post(post): newpost.body += "\n\n" + _(u"--- Last Edited by %(username)s at %(edit_date)s ---") % { "username": get_user_displayname(request.user), "edit_date": format_date(datetime.today()), } else: user = request.user.is_authenticated() and request.user or None newpost = Post( category=category, subject=data["subject"], body=data["body"], author=user, thread=thread ) if "markup" in data: newpost.markup = data["markup"] elif len(POST_MARKUP_CHOICES) == 1: newpost.markup = POST_MARKUP_CHOICES[0][0] newpost.save(additional_data=data) # category_type.save_post( newpost, data ) # Creating monitor if request.POST.get("addmonitor", False): newpost.toggle_monitor() if "createpoll" in request.POST and request.POST["createpoll"] == "1": newpost.set_poll(True) newpost.save() # Creating poll... polldata = pollForm.cleaned_data newpoll = Poll(post=newpost, question=polldata["question"], choices_per_user=polldata["choicesPerUser"]) newpoll.save() choices = polldata["answers"].splitlines() i = 0 for choice in choices: pollchoice = PollChoice(poll=newpoll, choice=choice, count=0, sortorder=i) i += 1 pollchoice.save() if request.user.is_authenticated(): request.user.message_set.create(message=ugettext(u"Vote created successfully.")) if request.user.is_authenticated(): if post: request.user.message_set.create(message=ugettext(u"Post edited successfully.")) else: request.user.message_set.create(message=ugettext(u"Post created successfully.")) if thread == None: thread = newpost return HttpResponseRedirect(newpost.get_absolute_url()) else: postForm = MyPostForm() postForm.init_for_category_type(category_type, post) pollForm = PostPollForm() if allow_attachments: attachmentForm = PostAttachmentForm(prefix="attachment") if post: postForm.fields["subject"].initial = post.subject postForm.fields["body"].initial = post.body if "markup" in postForm.fields: postForm.fields["markup"].initial = post.markup context["post"] = post context["thread"] = post.thread or post elif "quote" in request.REQUEST: quotepost = Post.objects.get(pk=request.REQUEST["quote"]) postForm.fields["subject"].initial = "Re: %s" % thread.subject if quotepost.author == None: username = "******" else: username = quotepost.author.username postForm.fields["body"].initial = "[quote=%s;%s]\n%s\n[/quote]\n" % (username, quotepost.id, quotepost.body) elif thread: postForm.fields["subject"].initial = "Re: %s" % thread.subject context["form"] = postForm # Only allow polls if this is a new _thread_ (not a reply) if (not thread and not post) or (post and post.is_new() and post.thread is None): context["pollform"] = pollForm context["attachmentForm"] = attachmentForm if "createpoll" in request.REQUEST: context["createpoll"] = request.REQUEST["createpoll"] res = render_to_response("sphene/sphboard/post.html", context, context_instance=RequestContext(request)) # Maybe the user is in the 'edit' form, which should not be cached. res.sph_lastmodified = True return res
def post(request, group=None, category_id=None, post_id=None, thread_id=None): """ View method to allow users to: - create new threads (post_id and thread_id is None) - reply to threads (post_id is None) - edit posts (post_id is the post which should be edited, thread_id is None) post_id and thread_id can either be passed in by URL (named parameters to this method) or by request.REQUEST parameters. """ if 'type' in request.REQUEST and request.REQUEST['type'] == 'preview': # If user just wants a preview, simply create a dummy post so it can be rendered. previewpost = Post( body=request.REQUEST['body'], markup=request.REQUEST.get('markup', None), ) return HttpResponse(unicode(previewpost.body_escaped())) # All available objects should be loaded from the _id variables. post_obj = None thread = None category = None context = { 'bbcodewysiwyg': enable_wysiwyg_editor() \ or (get_sph_setting('board_wysiwyg_testing') \ and request.REQUEST.get('wysiwyg', False)) } if post_id is None and 'post_id' in request.REQUEST: # if no post_id is given take it from the request. post_id = request.REQUEST['post_id'] if post_id is not None: # User wants to edit a post .. try: post_obj = Post.allobjects.get(pk=post_id) except Post.DoesNotExist: raise Http404 if not post_obj.allowEditing(): raise PermissionDenied() thread = post_obj.thread category = post_obj.category else: # User wants to create a new post (thread or reply) if 'thread' in request.REQUEST: thread_id = request.REQUEST['thread'] if thread_id is not None: # User is posting (replying) to a thread. try: thread = Post.allobjects.get(pk=thread_id) except Post.DoesNotExist: raise Http404 category = thread.category if not thread.allowPosting(request.user): raise PermissionDenied() else: # User is creating a new thread. category = get_object_or_404(Category, pk=category_id) if not category.allowPostThread(request.user): raise PermissionDenied() context['thread'] = thread context['category'] = category category_type = category.get_category_type() MyPostForm = PostForm if category_type is not None: MyPostForm = category_type.get_post_form_class(thread, post_obj) allow_attachments = get_sph_setting('board_allow_attachments') allowedattachments = 0 if allow_attachments: allowedattachments = 1 # bool is a subclass of int (hehe) if isinstance(allow_attachments, int) and type(allow_attachments) != bool: allowedattachments = allow_attachments PostAttachmentFormSet = modelformset_factory(PostAttachment, form=PostAttachmentForm, fields=('fileupload', ), can_delete=True, max_num=allowedattachments) post_attachment_qs = PostAttachment.objects.none() if post_obj: post_attachment_qs = post_obj.attachments.all() if request.method == 'POST': postForm = MyPostForm(request.POST) postForm.init_for_category_type(category_type, post_obj) pollForm = PostPollForm(request.POST) create_post = True if allowedattachments and 'cmd_addfile' in request.POST: create_post = False post_attachment_formset = PostAttachmentFormSet( request.POST, request.FILES, queryset=post_attachment_qs, prefix='attachment') if post_attachment_formset.is_valid(): instances = post_attachment_formset.save(commit=False) for attachment in instances: if not post_obj: # if there is no post yet.. we need to create a draft post_obj = Post( category=category, author=request.user, thread=thread, is_hidden=1, ) post_obj.set_new(True) post_obj.save() # Reference the post and save the attachment attachment.post = post_obj attachment.save() if create_post \ and postForm.is_valid() \ and ('createpoll' not in request.POST \ or pollForm.is_valid()): data = postForm.cleaned_data if post_obj: newpost = post_obj newpost.subject = data['subject'] newpost.body = data['body'] # make post visible newpost.is_hidden = 0 if not post_obj.is_new( ) and category_type.append_edit_message_to_post(post_obj): newpost.body += "\n\n" + _( u'--- Last Edited by %(username)s at %(edit_date)s ---' ) % { 'username': get_user_displayname(request.user), 'edit_date': format_date(timezone.now()) } else: user = request.user.is_authenticated() and request.user or None newpost = Post( category=category, subject=data['subject'], body=data['body'], author=user, thread=thread, ) if 'markup' in data: newpost.markup = data['markup'] elif len(POST_MARKUP_CHOICES) == 1: newpost.markup = POST_MARKUP_CHOICES[0][0] newpost.save(additional_data=data) #category_type.save_post( newpost, data ) # Creating monitor if request.POST.get('addmonitor', False): newpost.toggle_monitor() if 'createpoll' in request.POST and request.POST[ 'createpoll'] == '1': newpost.set_poll(True) newpost.save() # Creating poll... polldata = pollForm.cleaned_data newpoll = Poll(post=newpost, question=polldata['question'], choices_per_user=polldata['choicesPerUser']) newpoll.save() choices = polldata['answers'].splitlines() i = 0 for choice in choices: pollchoice = PollChoice(poll=newpoll, choice=choice, count=0, sortorder=i) i += 1 pollchoice.save() if request.user.is_authenticated(): messages.success( request, message=ugettext(u'Vote created successfully.')) if request.user.is_authenticated(): if post_obj: messages.success( request, message=ugettext(u'Post edited successfully.')) else: messages.success( request, message=ugettext(u'Post created successfully.')) if thread == None: thread = newpost return HttpResponseRedirect(newpost.get_absolute_url()) else: postForm = MyPostForm() postForm.init_for_category_type(category_type, post_obj) pollForm = PostPollForm() post_attachment_formset = PostAttachmentFormSet( queryset=post_attachment_qs, prefix='attachment') if post_obj: postForm.fields['subject'].initial = post_obj.subject postForm.fields['body'].initial = post_obj.body if 'markup' in postForm.fields: postForm.fields['markup'].initial = post_obj.markup context['post'] = post_obj context['thread'] = post_obj.thread or post_obj elif 'quote' in request.REQUEST: quotepost = get_object_or_404(Post, pk=request.REQUEST['quote']) postForm.fields['subject'].initial = 'Re: %s' % thread.subject if quotepost.author == None: username = '******' else: username = quotepost.author.username postForm.fields['body'].initial = '[quote=%s;%s]\n%s\n[/quote]\n' % ( username, quotepost.id, quotepost.body) elif thread: postForm.fields['subject'].initial = 'Re: %s' % thread.subject context['form'] = postForm # Only allow polls if this is a new _thread_ (not a reply) if (not thread and not post_obj) or (post_obj and post_obj.is_new() and post_obj.thread is None): context['pollform'] = pollForm context['post_attachment_formset'] = post_attachment_formset if 'createpoll' in request.REQUEST: context['createpoll'] = request.REQUEST['createpoll'] res = render_to_response("sphene/sphboard/post.html", context, context_instance=RequestContext(request)) # Maybe the user is in the 'edit' form, which should not be cached. res.sph_lastmodified = True return res
class PostMovingTester(TestCase): def setUp(self): """ We have 2 categories cat1 and cat2 and 2 threads (total 4 posts): cat1/ -- category cat1 cat1_p1 -- thread/post c1_p1 in category cat1 cat1_p2 -- post c1_p2 in thread cat1_p1 cat1_p3 -- post c1_p3 in thread cat1_p1 cat2/ -- category cat2 cat2_p1 -- thread/post cat2_p1 in category cat2 """ self.testuser = testutils.get_testuser() self.superuser = testutils.get_superuser() self.testgroup = testutils.get_testgroup() testutils.setup_threadlocals(self.testuser, self.testgroup) # Setup test role .. self.testrole = testutils.get_testrole() # Test category 1 self.cat1 = Category(name = 'Category 1', allowview = 3, allowreplies = 3, allowthreads = 3, group = self.testgroup) self.cat1.save() self.cat2 = Category(name = 'Category 2', allowview = 3, allowreplies = 3, allowthreads = 3, group = self.testgroup) self.cat2.save() # create thread 1 in category c1 self.cat1_p1 = Post(category = self.cat1, subject = 'Post p1 in category c1', body = "post 1", markup = 'bbcode', author = self.testuser) self.cat1_p1.save() self.cat1_p2 = Post(category = self.cat1, subject = 'Post p2 in category c1', body = "post 2", markup = 'bbcode', thread = self.cat1_p1, author = self.testuser) self.cat1_p2.save() self.cat1_p3 = Post(category = self.cat1, subject = 'Post p3 in category c1', body = "post 3", markup = 'bbcode', thread = self.cat1_p1, author = self.testuser) self.cat1_p3.save() # create thread 2 in category cat2 self.cat2_p1 = Post(category = self.cat2, subject = 'Post p1 in category cat2', body = "post 1", markup = 'bbcode', author = self.testuser) self.cat2_p1.save() # log in the user logged_in = self.client.login(username='******', password='******') def test_move_cat1_p2_to_cat2(self): """ Test moving post p2 from category cat1 directly into category cat2. Expected output is to have new thread (created from post p2) in category cat2. Thread cat1_p1 in category c1 will have less posts now """ mv1url = self.cat1_p2.get_absolute_moveposturl() self.assertEqual(mv1url, sph_reverse('move_post_1', kwargs={'post_id':self.cat1_p2.pk})) # check first step response = self.client.get(mv1url, {}) self.assertEqual(response.status_code, 200) # check second step (category is selected) mv2url = sph_reverse('move_post_2', kwargs={'post_id':self.cat1_p2.pk, 'category_id':self.cat2.pk}) response = self.client.get(mv2url, {}) self.assertEqual(response.status_code, 200) # check step 3 (with GET) - annotation form for post moved into category mv3url = sph_reverse('move_post_3', kwargs={'post_id':self.cat1_p2.pk, 'category_id':self.cat2.pk}) response = self.client.get(mv3url, {}) self.assertEqual(response.status_code, 200) # submit annotation form and move the post! self.assertEqual(self.cat2.threadCount(), 1) response = self.client.post(mv3url, {'body':'test body'}) self.assertEqual(response.status_code, 302) # get fresh instance of moved post p2 = Post.objects.get(pk=self.cat1_p2.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in category cat2 self.assertEqual(self.cat2.threadCount(), 2) # check if post p2 was removed form thread p1 self.assertEqual(self.cat1_p1.postCount(), 2) # check if post p2 is now new thread in category cat2 self.assertEqual(p2.get_thread(), p2) # check if ThreadInformation for post p1 was updated ti = self.cat1_p1.get_threadinformation() self.assertEqual(ti.post_count, 2) # check if number of ThreadInformation objects has been changed self.assertEqual(ThreadInformation.objects.all().count(), 3) # check ThreadInformation for new thread ti2 = p2.get_threadinformation() self.assertEqual(ti2.post_count, 1) def test_move_cat1_p1_to_cat2(self): """ Test moving post p1 (root post of thread!) from category cat1 directly into category cat2. Expected output is to have new thread (created from post p1) in category cat2 and new thread in category cat1 created from second post in former p1 thread. Old ThreadInformation object for thread p1 should be removed. Two new ThreadInformation objects will be crated """ mv3url = sph_reverse('move_post_3', kwargs={'post_id':self.cat1_p1.pk, 'category_id':self.cat2.pk}) # submit annotation form and move the post! self.assertEqual(self.cat2.threadCount(), 1) response = self.client.post(mv3url, {'body':'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts p1 = Post.objects.get(pk=self.cat1_p1.pk) p2 = Post.objects.get(pk=self.cat1_p2.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in category cat2 self.assertEqual(self.cat2.threadCount(), 2) # check if post p2 is now thread self.assertEqual(p2.get_thread(), p2) # check if post p1 is now new thread in category cat2 self.assertEqual(p1.get_thread(), p1) # check if ThreadInformation for post p2 was created properly ti = p2.get_threadinformation() self.assertEqual(ti.post_count, 2) # check if number of ThreadInformation objects has been changed self.assertEqual(ThreadInformation.objects.all().count(), 3) def test_move_cat1_p1_to_cat1(self): """ Test moving post p1 (root post of thread!) from category c1 directly into category cat1. Expected output is to have new thread (created from post p1) in category c1 and new thread in category c1 created from second post in former p1 thread. Old ThreadInformation object for thread p1 should be removed. Two new ThreadInformation objects will be crated """ mv3url = sph_reverse('move_post_3', kwargs={'post_id':self.cat1_p1.pk, 'category_id':self.cat1.pk}) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body':'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts p1 = Post.objects.get(pk=self.cat1_p1.pk) p2 = Post.objects.get(pk=self.cat1_p2.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in category cat1 self.assertEqual(self.cat1.threadCount(), 2) # check if post p2 is now thread self.assertEqual(p2.get_thread(), p2) # check if post p1 is now new thread in category cat1 self.assertEqual(p1.get_thread(), p1) # check if ThreadInformation for post p2 was created properly ti = p2.get_threadinformation() self.assertEqual(ti.post_count, 2) self.assertEqual(ti.category, self.cat1) # check if ThreadInformation for post p1 was updated properly ti2 = p1.get_threadinformation() self.assertEqual(ti2.post_count, 1) self.assertEqual(ti2.category, self.cat1) # check if number of ThreadInformation objects has been changed self.assertEqual(ThreadInformation.objects.all().count(), 3) def test_move_cat1_p3_to_cat1(self): """ Test moving post p3 from category cat1 directly into category cat1. Expected output is to have new thread (created from post p3) in category c1. Old ThreadInformation object for thread p1 should be updated. """ mv3url = sph_reverse('move_post_3', kwargs={'post_id':self.cat1_p3.pk, 'category_id':self.cat1.pk}) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body':'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts p1 = Post.objects.get(pk=self.cat1_p1.pk) p3 = Post.objects.get(pk=self.cat1_p3.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in category cat1 self.assertEqual(self.cat1.threadCount(), 2) # check if post p3 is now thread self.assertEqual(p3.get_thread(), p3) # check if ThreadInformation for post p3 was created properly ti = p3.get_threadinformation() self.assertEqual(ti.post_count, 1) self.assertEqual(ti.category, self.cat1) # check if ThreadInformation for post p1 was updated properly ti = p1.get_threadinformation() self.assertEqual(ti.post_count, 2) def test_move_cat2_p1_to_cat1(self): """ Test moving post p1 from category cat2 directly into category cat1. Expected output is to have new thread (created from post cat2_p1) in category c1. Old ThreadInformation object from cat2 should be removed. """ mv3url = sph_reverse('move_post_3', kwargs={'post_id':self.cat2_p1.pk, 'category_id':self.cat1.pk}) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body':'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts cat2_p1 = Post.objects.get(pk=self.cat2_p1.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if new thread exists in cat1 self.assertEqual(self.cat1.threadCount(), 2) # check if no threads left in cat2 self.assertEqual(self.cat2.threadCount(), 0) # check if post cat2_p1 is thread self.assertEqual(cat2_p1.get_thread(), cat2_p1) # check if ThreadInformation for post cat2_p1 was created properly ti = cat2_p1.get_threadinformation() self.assertEqual(ti.post_count, 1) self.assertEqual(ti.category, self.cat1) def test_move_cat1_p3_to_cat2_p1(self): """ Test moving post p3 from thread cat1_p1 into thread cat2_p1. Expected output is to have thread cat2_p1 updated and containing 2 posts. Thread cat1_p1 should be updated too as it now contains only 2 posts """ mv3url = sph_reverse('move_post_3', kwargs={'post_id':self.cat1_p3.pk, 'category_id':self.cat1.pk, 'thread_id':self.cat2_p1.pk}) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body':'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts p1 = Post.objects.get(pk=self.cat1_p1.pk) p3 = Post.objects.get(pk=self.cat1_p3.pk) cat2_p1 = Post.objects.get(pk=self.cat2_p1.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if thread cat2_p1 was updated ti = cat2_p1.get_threadinformation() self.assertEqual(ti.post_count, 2) # check if ThreadInformation for post p1 was updated properly ti = p1.get_threadinformation() self.assertEqual(ti.post_count, 2) self.assertEqual(ti.category, self.cat1) # check if post cat1_p3 is now in thread cat2_p1 self.assertEqual(p3.get_thread(), cat2_p1) def test_move_cat1_p1_to_cat2_p1(self): """ Test moving post cat1_p1 (root post of thread!) from thread cat1_p1 into thread cat2_p1. Expected output is to have thread cat2_p1 updated and containing 2 posts. Thread cat1_p1 should be updated too as it now contains only 2 posts """ mv3url = sph_reverse('move_post_3', kwargs={'post_id':self.cat1_p1.pk, 'category_id':self.cat1.pk, 'thread_id':self.cat2_p1.pk}) # submit annotation form and move the post! self.assertEqual(self.cat1.threadCount(), 1) response = self.client.post(mv3url, {'body':'test body'}) self.assertEqual(response.status_code, 302) # get fresh instances of posts cat1_p1 = Post.objects.get(pk=self.cat1_p1.pk) cat1_p2 = Post.objects.get(pk=self.cat1_p2.pk) cat2_p1 = Post.objects.get(pk=self.cat2_p1.pk) # check if success message was created self.assertEqual(self.superuser.message_set.count(), 1) # check if thread cat2_p1 was updated ti = cat2_p1.get_threadinformation() self.assertEqual(ti.post_count, 2) # check if ThreadInformation for post p2 was created properly ti = cat1_p2.get_threadinformation() self.assertEqual(ti.post_count, 2) self.assertEqual(ti.category, self.cat1) # check if post cat1_p1 is now in thread cat2_p1 self.assertEqual(cat1_p1.get_thread(), cat2_p1) # check if post cat1_p1 was added at the end of thread self.assertEqual(cat2_p1.get_latest_post(), cat1_p1)
def __create_testpost(self): # Create post ... p = Post( category = self.c, subject = 'Just a test post', body = 'hehe', author = self.testuser ) p.save() return p
def post(request, group=None, category_id=None, post_id=None, thread_id=None): if 'type' in request.REQUEST and request.REQUEST['type'] == 'preview': previewpost = Post( body=request.REQUEST['body'], markup=request.REQUEST.get('markup', None), ) return HttpResponse(unicode(previewpost.body_escaped())) post = None thread = None category = None context = {} if post_id is None and 'post_id' in request.REQUEST: # if no post_id is given take it from the request. post_id = request.REQUEST['post_id'] if post_id: try: post = Post.allobjects.get(pk=post_id) except Post.DoesNotExist: raise Http404 if not post.allowEditing(): raise PermissionDenied() thread = post.thread if 'thread' in request.REQUEST: thread_id = request.REQUEST['thread'] if thread_id: try: thread = Post.allobjects.get(pk=thread_id) except Post.DoesNotExist: raise Http404 category = thread.category context['thread'] = thread if not thread.allowPosting(request.user): raise PermissionDenied() else: category = get_object_or_404(Category, pk=category_id) if not category.allowPostThread(request.user): raise PermissionDenied() context['category'] = category category_type = category.get_category_type() MyPostForm = PostForm if category_type is not None: MyPostForm = category_type.get_post_form_class(thread, post) attachmentForm = None allow_attachments = get_sph_setting('board_allow_attachments') if request.method == 'POST': postForm = MyPostForm(request.POST) postForm.init_for_category_type(category_type, post) pollForm = PostPollForm(request.POST) create_post = True if allow_attachments: attachmentForm = PostAttachmentForm(request.POST, request.FILES, prefix='attachment') if 'cmd_addfile' in request.POST: create_post = False if attachmentForm.is_valid(): attachment = attachmentForm.save(commit=False) if attachment.fileupload: # Only save attachments if there was an upload... # If the form is valid, store the attachment if not post: # if there is no post yet.. we need to create a draft post = Post( category=category, author=request.user, thread=thread, is_hidden=1, ) post.set_new(True) post.save() # Reference the post and save the attachment attachment.post = post attachment.save() if create_post \ and postForm.is_valid() \ and ('createpoll' not in request.POST \ or pollForm.is_valid()): data = postForm.cleaned_data if post: newpost = post newpost.subject = data['subject'] newpost.body = data['body'] # make post visible newpost.is_hidden = 0 if not post.is_new( ) and category_type.append_edit_message_to_post(post): newpost.body += "\n\n" + _( u'--- Last Edited by %(username)s at %(edit_date)s ---' ) % { 'username': get_user_displayname(request.user), 'edit_date': format_date(datetime.today()) } else: user = request.user.is_authenticated() and request.user or None newpost = Post( category=category, subject=data['subject'], body=data['body'], author=user, thread=thread, ) if 'markup' in data: newpost.markup = data['markup'] elif len(POST_MARKUP_CHOICES) == 1: newpost.markup = POST_MARKUP_CHOICES[0][0] newpost.save() category_type.save_post(newpost, data) # Creating monitor if request.POST.get('addmonitor', False): newpost.toggle_monitor() if 'createpoll' in request.POST and request.POST[ 'createpoll'] == '1': newpost.set_poll(True) newpost.save() # Creating poll... polldata = pollForm.cleaned_data newpoll = Poll(post=newpost, question=polldata['question'], choices_per_user=polldata['choicesPerUser']) newpoll.save() choices = polldata['answers'].splitlines() i = 0 for choice in choices: pollchoice = PollChoice(poll=newpoll, choice=choice, count=0, sortorder=i) i += 1 pollchoice.save() if request.user.is_authenticated(): request.user.message_set.create( message=ugettext(u'Vote created successfully.')) if request.user.is_authenticated(): if post: request.user.message_set.create( message=ugettext(u'Post edited successfully.')) else: request.user.message_set.create( message=ugettext(u'Post created successfully.')) if thread == None: thread = newpost return HttpResponseRedirect(newpost.get_absolute_url()) else: postForm = MyPostForm() postForm.init_for_category_type(category_type, post) pollForm = PostPollForm() if allow_attachments: attachmentForm = PostAttachmentForm(prefix='attachment') if post: postForm.fields['subject'].initial = post.subject postForm.fields['body'].initial = post.body if 'markup' in postForm.fields: postForm.fields['markup'].initial = post.markup context['post'] = post context['thread'] = post.thread or post elif 'quote' in request.REQUEST: quotepost = Post.objects.get(pk=request.REQUEST['quote']) postForm.fields['subject'].initial = 'Re: %s' % thread.subject if quotepost.author == None: username = '******' else: username = quotepost.author.username postForm.fields['body'].initial = '[quote=%s;%s]\n%s\n[/quote]\n' % ( username, quotepost.id, quotepost.body) elif thread: postForm.fields['subject'].initial = 'Re: %s' % thread.subject context['form'] = postForm # Only allow polls if this is a new _thread_ (not a reply) if (not thread and not post) or (post and post.is_new() and post.thread is None): context['pollform'] = pollForm context['attachmentForm'] = attachmentForm if 'createpoll' in request.REQUEST: context['createpoll'] = request.REQUEST['createpoll'] res = render_to_response("sphene/sphboard/post.html", context, context_instance=RequestContext(request)) # Maybe the user is in the 'edit' form, which should not be cached. res.sph_lastmodified = True return res
def post(request, group = None, category_id = None, post_id = None, thread_id = None): """ View method to allow users to: - create new threads (post_id and thread_id is None) - reply to threads (post_id is None) - edit posts (post_id is the post which should be edited, thread_id is None) post_id and thread_id can either be passed in by URL (named parameters to this method) or by request.REQUEST parameters. """ if 'type' in request.REQUEST and request.REQUEST['type'] == 'preview': # If user just wants a preview, simply create a dummy post so it can be rendered. previewpost = Post( body = request.REQUEST['body'], markup = request.REQUEST.get('markup', None), ) return HttpResponse( unicode(previewpost.body_escaped()) ) # All available objects should be loaded from the _id variables. post_obj = None thread = None category = None context = { 'bbcodewysiwyg': enable_wysiwyg_editor() \ or (get_sph_setting('board_wysiwyg_testing') \ and request.REQUEST.get('wysiwyg', False)) } if post_id is None and 'post_id' in request.REQUEST: # if no post_id is given take it from the request. post_id = request.REQUEST['post_id'] if post_id is not None: # User wants to edit a post .. try: post_obj = Post.allobjects.get( pk = post_id ) except Post.DoesNotExist: raise Http404 if not post_obj.allowEditing(): raise PermissionDenied() thread = post_obj.thread category = post_obj.category else: # User wants to create a new post (thread or reply) if 'thread' in request.REQUEST: thread_id = request.REQUEST['thread'] if thread_id is not None: # User is posting (replying) to a thread. try: thread = Post.allobjects.get( pk = thread_id ) except Post.DoesNotExist: raise Http404 category = thread.category if not thread.allowPosting( request.user ): raise PermissionDenied() else: # User is creating a new thread. category = get_object_or_404(Category, pk = category_id) if not category.allowPostThread( request.user ): raise PermissionDenied() context['thread'] = thread context['category'] = category category_type = category.get_category_type() MyPostForm = PostForm if category_type is not None: MyPostForm = category_type.get_post_form_class(thread, post_obj) allow_attachments = get_sph_setting('board_allow_attachments') allowedattachments = 0 if allow_attachments: allowedattachments = 1 # bool is a subclass of int (hehe) if isinstance(allow_attachments, int) and type(allow_attachments) != bool: allowedattachments = allow_attachments PostAttachmentFormSet = modelformset_factory(PostAttachment, form=PostAttachmentForm, fields=('fileupload',), can_delete=True, max_num=allowedattachments) post_attachment_qs = PostAttachment.objects.none() if post_obj: post_attachment_qs = post_obj.attachments.all() if request.method == 'POST': postForm = MyPostForm(request.POST) postForm.init_for_category_type(category_type, post_obj) pollForm = PostPollForm(request.POST) create_post = True if allowedattachments and 'cmd_addfile' in request.POST: create_post = False post_attachment_formset = PostAttachmentFormSet(request.POST, request.FILES, queryset = post_attachment_qs, prefix='attachment' ) if post_attachment_formset.is_valid(): instances = post_attachment_formset.save(commit=False) for attachment in instances: if not post_obj: # if there is no post yet.. we need to create a draft post_obj = Post( category = category, author = request.user, thread = thread, is_hidden = 1, ) post_obj.set_new( True ) post_obj.save() # Reference the post and save the attachment attachment.post = post_obj attachment.save() if create_post \ and postForm.is_valid() \ and ('createpoll' not in request.POST \ or pollForm.is_valid()): data = postForm.cleaned_data if post_obj: newpost = post_obj newpost.subject = data['subject'] newpost.body = data['body'] # make post visible newpost.is_hidden = 0 if not post_obj.is_new() and category_type.append_edit_message_to_post(post_obj): newpost.body += "\n\n" + _(u'--- Last Edited by %(username)s at %(edit_date)s ---') % {'username':get_user_displayname( request.user ), 'edit_date':format_date( timezone.now())} else: user = request.user.is_authenticated() and request.user or None newpost = Post( category = category, subject = data['subject'], body = data['body'], author = user, thread = thread, ) if 'markup' in data: newpost.markup = data['markup'] elif len( POST_MARKUP_CHOICES ) == 1: newpost.markup = POST_MARKUP_CHOICES[0][0] newpost.save(additional_data = data) #category_type.save_post( newpost, data ) # Creating monitor if request.POST.get( 'addmonitor', False ): newpost.toggle_monitor() if 'createpoll' in request.POST and request.POST['createpoll'] == '1': newpost.set_poll( True ); newpost.save() # Creating poll... polldata = pollForm.cleaned_data newpoll = Poll( post = newpost, question = polldata['question'], choices_per_user = polldata['choicesPerUser']) newpoll.save() choices = polldata['answers'].splitlines() i=0 for choice in choices: pollchoice = PollChoice( poll = newpoll, choice = choice, count = 0, sortorder = i) i+=1 pollchoice.save() if request.user.is_authenticated(): messages.success(request, message = ugettext(u'Vote created successfully.') ) if request.user.is_authenticated(): if post_obj: messages.success(request, message = ugettext(u'Post edited successfully.') ) else: messages.success(request, message = ugettext(u'Post created successfully.') ) if thread == None: thread = newpost return HttpResponseRedirect( newpost.get_absolute_url() ) else: postForm = MyPostForm( ) postForm.init_for_category_type(category_type, post_obj) pollForm = PostPollForm() post_attachment_formset = PostAttachmentFormSet(queryset = post_attachment_qs, prefix='attachment') if post_obj: postForm.fields['subject'].initial = post_obj.subject postForm.fields['body'].initial = post_obj.body if 'markup' in postForm.fields: postForm.fields['markup'].initial = post_obj.markup context['post'] = post_obj context['thread'] = post_obj.thread or post_obj elif 'quote' in request.REQUEST: quotepost = get_object_or_404(Post, pk = request.REQUEST['quote'] ) postForm.fields['subject'].initial = 'Re: %s' % thread.subject if quotepost.author == None: username = '******' else: username = quotepost.author.username postForm.fields['body'].initial = '[quote=%s;%s]\n%s\n[/quote]\n' % (username, quotepost.id, quotepost.body) elif thread: postForm.fields['subject'].initial = 'Re: %s' % thread.subject context['form'] = postForm # Only allow polls if this is a new _thread_ (not a reply) if (not thread and not post_obj) or (post_obj and post_obj.is_new() and post_obj.thread is None): context['pollform'] = pollForm context['post_attachment_formset'] = post_attachment_formset if 'createpoll' in request.REQUEST: context['createpoll'] = request.REQUEST['createpoll'] res = render_to_response( "sphene/sphboard/post.html", context, context_instance = RequestContext(request) ) # Maybe the user is in the 'edit' form, which should not be cached. res.sph_lastmodified = True return res