summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-12 13:29:04 -0400
committerEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-12 13:29:04 -0400
commitc2587287e11a31ced679e7127af2b204ce559abd (patch)
treebe4ab3f98a225b0e7abb8bfca5644a3662e65fbe
parentc8b8eaeb0f971d16809c0902c085bdecc2f147f0 (diff)
parent2e6653f348a7496f7be08c919c8197e4ca8c1a6d (diff)
downloadaskbot-c2587287e11a31ced679e7127af2b204ce559abd.tar.gz
askbot-c2587287e11a31ced679e7127af2b204ce559abd.tar.bz2
askbot-c2587287e11a31ced679e7127af2b204ce559abd.zip
merged Adolfo's branch
-rw-r--r--askbot/skins/common/templates/question/answer_controls.html2
-rw-r--r--askbot/skins/default/templates/question.html2
-rw-r--r--askbot/tests/misc_tests.py91
-rw-r--r--askbot/views/writers.py24
4 files changed, 113 insertions, 6 deletions
diff --git a/askbot/skins/common/templates/question/answer_controls.html b/askbot/skins/common/templates/question/answer_controls.html
index 4e53f880..21d12dd6 100644
--- a/askbot/skins/common/templates/question/answer_controls.html
+++ b/askbot/skins/common/templates/question/answer_controls.html
@@ -69,6 +69,6 @@
</form>
</span>
<script type="text/javascript">
- askbot['functions']['renderPostControls']('{{answer.id}}');
askbot['functions']['hideConvertAnswerLinks']('{{answer.id}}');
+ askbot['functions']['renderPostControls']('{{answer.id}}');
</script>
diff --git a/askbot/skins/default/templates/question.html b/askbot/skins/default/templates/question.html
index 85210bbd..794853c2 100644
--- a/askbot/skins/default/templates/question.html
+++ b/askbot/skins/default/templates/question.html
@@ -175,7 +175,7 @@
function hide_convert_links(){
if (!askbot['data']['userIsAdminOrMod']){
- var links = document.getElementsByClassName('convert');
+ var links = document.getElementsByClassName('convert-comment');
for (i=0; i<links.length; i++){
links[i].setAttribute('style', 'display:none;');
}
diff --git a/askbot/tests/misc_tests.py b/askbot/tests/misc_tests.py
new file mode 100644
index 00000000..328c213f
--- /dev/null
+++ b/askbot/tests/misc_tests.py
@@ -0,0 +1,91 @@
+from askbot.tests.utils import AskbotTestCase
+from askbot.models.post import PostRevision
+
+from django.test.client import Client
+from django.core.urlresolvers import reverse
+
+class MiscTests(AskbotTestCase):
+
+ def setUp(self):
+ self.u1 = self.create_user(username='user1')
+ self.u2 = self.create_user(username='user2')
+ self.u3 = self.create_user(username='user3')
+
+ def test_proper_PostRevision_manager_is_used(self):
+ "Makes sure that both normal and related managers for PostRevision don't implement .create() method"
+ question = self.post_question(user=self.u1)
+ self.assertRaises(NotImplementedError, question.revisions.create)
+ self.assertRaises(NotImplementedError, PostRevision.objects.create)
+
+class ContentConvertionTests(AskbotTestCase):
+
+ def setUp(self):
+ self.u1 = self.create_user(username='user1')
+ self.u1.set_password('password')
+ self.u1.set_admin_status()
+ self.u1.save()
+ self.u2 = self.create_user(username='notadmin')
+ self.client = Client()
+
+ #content
+ self.question = self.post_question(user=self.u1)
+ self.answer_to_convert = self.post_answer(user=self.u2,
+ question=self.question)
+ self.comment_on_answer = self.post_comment(user=self.u1,
+ parent_post=self.answer_to_convert)
+ self.another_answer = self.post_answer(user=self.u1,
+ question=self.question)
+ self.comment_to_convert = self.post_comment(user=self.u1,
+ parent_post=self.another_answer)
+
+ def test_convert_comment_to_answer(self):
+ self.client.login(username='user1', password='password')
+ old_parent_comment_count = self.another_answer.comment_count
+ answer_count = self.question.thread.answer_count
+ self.client.post(reverse('comment_to_answer'),
+ {'comment_id': self.comment_to_convert.id})
+ converted_answer = self.reload_object(self.comment_to_convert)
+ #old_parent = self.another_answer
+ old_parent = self.reload_object(self.another_answer)
+
+ #test for convertion
+ self.assertEquals(converted_answer.post_type, 'answer')
+ #test for parent change
+ self.assertNotEquals(old_parent.id, converted_answer.parent.id)
+ #test for answer count update
+ self.assertEquals(converted_answer.thread.answer_count, answer_count + 1)
+ #test for comment count update
+ self.assertEquals(old_parent.comment_count, old_parent_comment_count - 1)
+
+ #test the delete post view for errors
+ response = self.client.post(reverse('delete_post'),
+ {'post_id': converted_answer.id,
+ 'cancel_vote': 'false'},
+ HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ self.assertEquals(response.status_code, 200)
+ self.assertTrue('is_deleted' in response.content)
+
+ def test_convert_answer_to_comment(self):
+ comment_count = self.question.comment_count
+ #because the answer itself has a comment too!
+ comment_count += self.answer_to_convert.comment_count
+
+ answer_count = self.question.thread.answer_count
+ self.client.login(username='user1', password='password')
+ self.client.post(reverse('answer_to_comment'),
+ {'answer_id': self.answer_to_convert.id})
+ converted_comment = self.reload_object(self.answer_to_convert)
+ old_parent = self.reload_object(self.question)
+
+ #test for convertion
+ self.assertEquals(converted_comment.post_type, 'comment')
+ #test for answer count update
+ self.assertEquals(converted_comment.thread.answer_count, answer_count - 1)
+ #test for comment count update
+ self.assertEquals(old_parent.comment_count, comment_count + 1)
+
+ #test the delete comment view for errors
+ response = self.client.post(reverse('delete_comment'),
+ {'comment_id': converted_comment.id},
+ HTTP_X_REQUESTED_WITH='XMLHttpRequest')
+ self.assertEquals(response.status_code, 200)
diff --git a/askbot/views/writers.py b/askbot/views/writers.py
index 422a89fa..fbe76650 100644
--- a/askbot/views/writers.py
+++ b/askbot/views/writers.py
@@ -731,8 +731,19 @@ def comment_to_answer(request):
comment = get_object_or_404(models.Post,
post_type='comment', id=comment_id)
comment.post_type = 'answer'
+ old_parent = comment.parent
+
+ comment.parent = comment.thread._question_post()
comment.save()
+
comment.thread.update_answer_count()
+
+ comment.parent.comment_count += 1
+ comment.parent.save()
+
+ old_parent.comment_count -= 1
+ old_parent.save()
+
comment.thread.invalidate_cached_data()
return HttpResponseRedirect(comment.get_absolute_url())
@@ -750,14 +761,19 @@ def answer_to_comment(request):
if len(answer.text) <= 300:
answer.post_type = 'comment'
answer.parent = answer.thread._question_post()
- answer_comments = models.Post.objects.get_comments().filter(parent=answer)
+ #can we trust this?
+ old_comment_count = answer.comment_count
+ answer.comment_count = 0
- for comment in answer_comments:
- comment.parent = answer.parent
- comment.save()
+ answer_comments = models.Post.objects.get_comments().filter(parent=answer)
+ answer_comments.update(parent=answer.parent)
answer.parse_and_save(author=answer.author)
answer.thread.update_answer_count()
+
+ answer.parent.comment_count = 1 + old_comment_count
+ answer.parent.save()
+
answer.thread.invalidate_cached_data()
else:
request.user.message_set.create(message = _("the selected answer cannot be a comment"))