diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2014-07-24 20:59:41 -0300 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2014-07-24 20:59:41 -0300 |
commit | 383fbc831da08cd1c21ed30420d55e0fb4e81b35 (patch) | |
tree | 9971b84b44dff0b882b03d744a92bfae0d55e611 | |
parent | 6778e6f453b4d9afb5e4aef318506f61401e8ccf (diff) | |
download | askbot-383fbc831da08cd1c21ed30420d55e0fb4e81b35.tar.gz askbot-383fbc831da08cd1c21ed30420d55e0fb4e81b35.tar.bz2 askbot-383fbc831da08cd1c21ed30420d55e0fb4e81b35.zip |
started work on merge duplicate threads feature
-rw-r--r-- | askbot/models/__init__.py | 41 | ||||
-rw-r--r-- | askbot/models/post.py | 7 |
2 files changed, 48 insertions, 0 deletions
diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 7d91216e..77f3a2fd 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -787,6 +787,14 @@ def user_assert_can_upload_file(request_user): ) +def user_assert_can_merge_questions(self): + _assert_user_can( + user=self, + action_display=_('merge duplicate questions'), + admin_or_moderator_required=True + ) + + def user_assert_can_post_text(self, text): """Raises exceptions.PermissionDenied, if user does not have privilege to post given text, depending on the contents @@ -1385,6 +1393,37 @@ def user_mark_tags( return cleaned_tagnames, cleaned_wildcards +def user_merge_duplicate_threads(self, from_thread, to_thread): + """merges content from the ``from_thread`` to the ``to-thread``""" + #todo: maybe assertion will depend on which questions are merged + self.assert_can_merge_questions() + from_q = from_thread._question_post() + to_q = to_thread._question_post() + to_q.merge_post(from_q) + from_q.delete() + #set new thread value to all posts + posts = from_thread.posts.all() + posts.update(thread=to_thread) + + if askbot_settings.LIMIT_ONE_ANSWER_PER_USER: + #merge answers if only one is allowed per user + answers = to_thread.all_answers() + answer_map = collections.defaultdict(list) + #compile all answers by user + for answer in answers: + author = answer.author + answer_map[author].append(answer) + + for author in answer_map: + if len(answer_map[author]) > 1: + answers = answer_map[author] + first_answer = answers.pop(0) + for answer in answers: + first_answer.merge_post(answer) + + from_thread.delete() + + @auto_now_timestamp def user_retag_question( self, @@ -3018,6 +3057,7 @@ User.add_to_class('follow_question', user_follow_question) User.add_to_class('unfollow_question', user_unfollow_question) User.add_to_class('is_following_question', user_is_following_question) User.add_to_class('mark_tags', user_mark_tags) +User.add_to_class('merge_duplicate_threads', user_merge_duplicate_threads) User.add_to_class('update_response_counts', user_update_response_counts) User.add_to_class('can_create_tags', user_can_create_tags) User.add_to_class('can_have_strong_url', user_can_have_strong_url) @@ -3076,6 +3116,7 @@ User.add_to_class('is_read_only', user_is_read_only) User.add_to_class('assert_can_vote_for_post', user_assert_can_vote_for_post) User.add_to_class('assert_can_revoke_old_vote', user_assert_can_revoke_old_vote) User.add_to_class('assert_can_upload_file', user_assert_can_upload_file) +User.add_to_class('assert_can_merge_questions', user_assert_can_merge_questions) User.add_to_class('assert_can_post_question', user_assert_can_post_question) User.add_to_class('assert_can_post_answer', user_assert_can_post_answer) User.add_to_class('assert_can_post_comment', user_assert_can_post_comment) diff --git a/askbot/models/post.py b/askbot/models/post.py index 594e1d9f..690e4188 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -788,6 +788,13 @@ class Post(models.Model): groups = (Group.objects.get_global_group(),) self.add_to_groups(groups) + def merge_post(self, post): + """merge with other post""" + #take latest revision of current post R1 + #for each revision of other post Ri + #append content of Ri to R1 and use author + + def is_private(self): """true, if post belongs to the global group""" if askbot_settings.GROUPS_ENABLED: |