def get_object(self): user = self.request.user obj = super(PostDetails, self).get_object() # Update the post views. Post.update_post_views(obj, request=self.request) # Adds the permissions obj = post_permissions(request=self.request, post=obj) # This will be piggybacked on the main object. obj.sub = Subscription.get_sub(post=obj, user=user) # Just a sanity check to start at top level. if obj != obj.root: obj = obj.root # Populate the object to build a tree that contains all posts in the thread. # Answers sorted before comments. thread = [post_permissions(request=self.request, post=post) for post in Post.objects.get_thread(obj)] # Do a little preprocessing. answers = [p for p in thread if p.type == Post.ANSWER] tree = OrderedDict() for post in thread: if post.type == Post.COMMENT: tree.setdefault(post.parent_id, []).append(post) store = {Vote.UP: set(), Vote.BOOKMARK: set()} if user.is_authenticated(): pids = [p.id for p in thread] votes = Vote.objects.filter(post_id__in=pids, author=user).values_list("post_id", "type") for post_id, vote_type in votes: store.setdefault(vote_type, set()).add(post_id) # Shortcuts to each storage. bookmarks = store[Vote.BOOKMARK] upvotes = store[Vote.UP] def decorate(post): post.has_bookmark = post.id in bookmarks post.has_upvote = post.id in upvotes # Add attributes by mutating the objects map(decorate, thread + [obj]) # Additional attributes used during rendering obj.tree = tree obj.answers = answers # Add the more like this field post = super(PostDetails, self).get_object() return obj
def get_object(self): user = self.request.user obj = super(PostDetails, self).get_object() # Update the post views. Post.update_post_views(obj, request=self.request) # Adds the permissions obj = post_permissions(request=self.request, post=obj) # This will be piggybacked on the main object. obj.sub = Subscription.get_sub(post=obj, user=user) # Bail out if not at top level. if not obj.is_toplevel: return obj # Populate the object to build a tree that contains all posts in the thread. # Answers sorted before comments. thread = [ post_permissions(request=self.request, post=post) for post in Post.objects.get_thread(obj, user) ] # Do a little preprocessing. answers = [p for p in thread if p.type == Post.ANSWER] tree = OrderedDict() for post in thread: if post.type == Post.COMMENT: tree.setdefault(post.parent_id, []).append(post) store = {Vote.UP: set(), Vote.DOWN: set(), Vote.BOOKMARK: set()} if user.is_authenticated(): pids = [p.id for p in thread] votes = Vote.objects.filter(post_id__in=pids, author=user).values_list( "post_id", "type") for post_id, vote_type in votes: store.setdefault(vote_type, set()).add(post_id) # Shortcuts to each storage. bookmarks = store[Vote.BOOKMARK] upvotes = store[Vote.UP] downvotes = store[Vote.DOWN] # Can the current user accept answers can_accept = obj.author == user def decorate(post): post.has_bookmark = post.id in bookmarks post.has_upvote = post.id in upvotes post.has_downvote = post.id in downvotes post.can_accept = can_accept or post.has_accepted # Add attributes by mutating the objects map(decorate, thread + [obj]) # Additional attributes used during rendering obj.tree = tree obj.answers = answers # Add the more like this field post = super(PostDetails, self).get_object() return obj
def get_object(self): user = self.request.user obj = super(PostDetails, self).get_object() # Raise 404 if a deleted post is viewed by an anonymous user if (obj.status == Post.DELETED) and not self.request.user.is_moderator: raise Http404() # Update the post views. Post.update_post_views(obj, request=self.request) # Adds the permissions obj = post_permissions(request=self.request, post=obj) # This will be piggybacked on the main object. obj.sub = Subscription.get_sub(post=obj, user=user) # Bail out if not at top level. if not obj.is_toplevel: return obj # Populate the object to build a tree that contains all posts in the thread. # Answers sorted before comments. thread = [post_permissions(request=self.request, post=post) for post in Post.objects.get_thread(obj, user)] # Do a little preprocessing. answers = [p for p in thread if p.type == Post.ANSWER] tree = OrderedDict() for post in thread: if post.type == Post.COMMENT: tree.setdefault(post.parent_id, []).append(post) store = {Vote.UP: set(), Vote.BOOKMARK: set()} if user.is_authenticated(): pids = [p.id for p in thread] votes = Vote.objects.filter(post_id__in=pids, author=user).values_list("post_id", "type") for post_id, vote_type in votes: store.setdefault(vote_type, set()).add(post_id) # Shortcuts to each storage. bookmarks = store[Vote.BOOKMARK] upvotes = store[Vote.UP] # Can the current user accept answers can_accept = obj.author == user def decorate(post): post.has_bookmark = post.id in bookmarks post.has_upvote = post.id in upvotes post.can_accept = can_accept or post.has_accepted # Add attributes by mutating the objects decorate(obj) for post in thread: decorate(post) # Additional attributes used during rendering obj.tree = tree obj.answers = answers # Add the more like this field post = super(PostDetails, self).get_object() return obj