From d26ab50da99b50bdd6e6cdb9dc60bceed8553def Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Fri, 11 Jul 2014 23:48:41 -0300 Subject: initial work to show pending posts to authors with premoderation mode --- askbot/models/post.py | 5 ++++ askbot/models/question.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++- askbot/views/readers.py | 5 ++-- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/askbot/models/post.py b/askbot/models/post.py index 4e9e5d6f..95e33083 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -1036,6 +1036,11 @@ class Post(models.Model): self._cached_comments = list() return self._cached_comments + def add_cached_comment(self, comment): + comments = self.get_cached_comments() + if comment not in comments: + comments.append(comment) + def add_comment( self, comment=None, diff --git a/askbot/models/question.py b/askbot/models/question.py index 2cdcdf71..82a3e192 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -2,6 +2,7 @@ import datetime import operator import re +from copy import copy from django.conf import settings as django_settings from django.db import models from django.db.models import F @@ -1049,7 +1050,79 @@ class Thread(models.Model): else: self.update_summary_html() - def get_cached_post_data(self, user = None, sort_method = None): + def get_post_data_for_question_view(self, user=None, sort_method=None): + """loads post data for use in the question details view + """ + post_data = self.get_cached_post_data(user=user, sort_method=sort_method) + if askbot_settings.CONTENT_MODERATION_MODE == 'premoderation' and user.is_watched(): + #in this branch we patch post_data with the edits suggested by the + #watched user + post_ids = self.posts.filter(author=user).values_list('id', flat=True) + from askbot.models import PostRevision + suggested_revs = PostRevision.objects.filter( + author=user, + post__id__in=post_ids, + revision=0 + ) + #get ids of posts that we need to patch with suggested data + if len(suggested_revs): + #find posts that we need to patch + def find_posts(posts, need_ids): + """posts - is source list + need_ids - set of post ids + """ + found = dict() + for post in posts: + if post.id in need_ids: + found[post.id] = post + need_ids.remove(post.id) + comments = post.get_cached_comments() + found.update(find_posts(comments, need_ids)) + return found + + suggested_post_ids = set([rev.post_id for rev in suggested_revs]) + + question = post_data[0] + answers = post_data[1] + post_to_author = post_data[2] + + post_id_set = set(suggested_post_ids) + + all_posts = copy(answers) + all_posts.append(question) + posts = find_posts(all_posts, post_id_set) + + rev_map = dict(zip(suggested_post_ids, suggested_revs)) + + for post_id, post in posts.items(): + rev = rev_map[post_id] + #patching work + post.text = rev.text + post.html = post.parse_post_text()['html'] + + if len(post_id_set): + #brand new suggested posts + from askbot.models import Post + #order by insures that + posts = Post.objects.filter(id__in=post_id_set).order_by('post_type') + for post in posts: + rev = rev_map[post.id] + post.text = rev.text + post.html = post.parse_post_text()['html'] + if post.is_comment(): + parents = find_posts(all_posts, set([post.parent_id])) + parent = parents.values()[0] + parent.add_cached_comment(post) + if post.is_answer(): + answers.insert(0, post) + all_posts.append(post)#add b/c there may be self-comments + if post.is_question(): + post_data[0] = post + + return post_data + + + def get_cached_post_data(self, user=None, sort_method=None): """returns cached post data, as calculated by the method get_post_data()""" sort_method = sort_method or askbot_settings.DEFAULT_ANSWER_SORT_METHOD diff --git a/askbot/views/readers.py b/askbot/views/readers.py index 80f1d2ec..e90b7e97 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -382,6 +382,8 @@ def tags(request):#view showing a listing of available tags - plain list def question(request, id):#refactor - long subroutine. display question body, answers and comments """view that displays body of the question and all answers to it + + todo: convert this view into class """ #process url parameters #todo: fix inheritance of sort method from questions @@ -506,7 +508,7 @@ def question(request, id):#refactor - long subroutine. display question body, an #load answers and post id's->athor_id mapping #posts are pre-stuffed with the correctly ordered comments - updated_question_post, answers, post_to_author, published_answer_ids = thread.get_cached_post_data( + updated_question_post, answers, post_to_author, published_answer_ids = thread.get_post_data_for_question_view( sort_method=answer_sort_method, user=request.user ) @@ -514,7 +516,6 @@ def question(request, id):#refactor - long subroutine. display question body, an updated_question_post.get_cached_comments() ) - #Post.objects.precache_comments(for_posts=[question_post] + answers, visitor=request.user) user_votes = {} -- cgit v1.2.3-1-g7c22