Exemple #1
0
def _get_tagged_from_redis(viewer, nav):
    comments = Comment.browse_objects

    if nav.sort == 'hot':
        ids = _get_hot_slice_by_threads(Tag(nav.tag).popular, nav.slice)
        comments = comments.exclude(reply_content__id=None)

    elif nav.sort == 'top':
        # this is so gross
        ids = nav.get_top_data(Tag(nav.tag))[nav.slice]
    else:
        ids = Tag(nav.tag).images_only[nav.slice]

    # Remove user-hidden comments.
    ids = remove_hidden_comment_ids(viewer, ids)

    return comments.in_bulk_list(ids)
    def top_reply_ids(self, force_show=False):
        """ `force_show` if True will return as many top reply IDs as we have. """
        num_top = min(self.num_replies / Config['posts_per_top_reply'], Config['maximum_top_replies'])
        min_top_replies = Config['minimum_top_replies']
        top_reply_ids = []
        # Avoid even hitting redis if we know we aren't going to use the data.
        if force_show or num_top >= min_top_replies and not self.explicit_page_view:
            top_reply_ids = self._op_comment.top_replies
            if force_show:
                top_reply_ids = top_reply_ids[:]
            else:
                top_reply_ids = top_reply_ids[:num_top]
            if not force_show and len(top_reply_ids) < min_top_replies:
                top_reply_ids = []

        top_reply_ids = remove_hidden_comment_ids(self.request.user, top_reply_ids)

        return top_reply_ids
Exemple #3
0
def _get_from_redis(viewer, nav):
    manager = Comment.browse_objects

    if nav.sort == 'pinned':
        if not viewer.is_authenticated():
            raise PermissionDenied()
        rlbb = viewer.redis.pinned_bump_buffer
    else:
        # The 'curated' visibility is used to curate the sections of everything.
        if nav.category == Category.ALL:
            manager = Comment.curated_browse_objects

        if nav.category == Category.MY:
            if not viewer.is_authenticated():
                raise PermissionDenied()
            following = Category.objects.filter(followers__user=viewer)
            rlbb = _get_aggregate_rlbb(following, nav)
        elif not viewer.is_authenticated() and nav.category == Category.ALL:
            # Implicitly whitelisted groups
            rlbb = _get_aggregate_rlbb(Category.objects.in_bulk_list(Category.get_whitelisted()), nav)
        else:
            rlbb = _get_buffer(nav.category, nav)

    if nav.sort != 'hot':
        ids = rlbb[nav.slice]
    elif nav.hot_sort_type:
        ids = frontpage_algorithms.get_slice(nav.hot_sort_type, nav.slice)
    else:
        ids = _get_hot_slice_by_threads(rlbb, nav.slice)

    qs = manager
    if nav.sort not in ['active']:
        qs = qs.exclude(reply_content=None)

    # Remove user-hidden comments.
    ids = remove_hidden_comment_ids(viewer, ids)

    return qs.in_bulk_list(ids)
    def __init__(self, request, short_id, page=None, gotoreply=None):
        try:
            mapping_id = get_mapping_id_from_short_id(short_id)
        except ValueError:
            raise Http404

        self.request = request
        self.short_id = short_id
        self.page = page
        try:
            self.gotoreply = int(gotoreply)
        except TypeError:
            self.gotoreply = None

        _op_comment = get_object_or_404(Comment.published, id=mapping_id)

        # Get relevant OP data.
        _op_content = _op_comment.reply_content
        op_content = _op_content.details() if _op_content else None

        op_comment = _op_comment.details
        op_category = _op_comment.category.details() if _op_comment.category else Category.ALL.details()

        linked_comment = op_comment
        _linked_comment = _op_comment

        reply_ids = list(_op_comment.get_replies().values_list('id', flat=True))

        # Remove user-hidden comments.
        reply_ids = remove_hidden_comment_ids(request.user, reply_ids)

        # Pagination.
        explicit_page_view = bool(page)
        num_replies = len(reply_ids)
        if gotoreply is not None:
            gotoreply = int(gotoreply)
            try:
                page = page_divide(reply_ids.index(gotoreply)+1, knobs.COMMENTS_PER_PAGE)
                # If going to a reply on the last page, show 'current'.
                if reply_ids.index(gotoreply) + 1 >= num_replies - knobs.COMMENTS_PER_PAGE:
                    page = 'current'
            except ValueError:
                page = '1'

            # Grab the gotoreply's content for metadata
            _linked_comment = Comment.objects.get_or_none(id=gotoreply)
            if _linked_comment:
                linked_comment = _linked_comment.details
        elif page is None:
            page = '1'

        self.page_reply_ids, self.page_current, self.page_next, self.page_last = paginate(
                reply_ids, page=page, per_page=knobs.COMMENTS_PER_PAGE)
        self.page_penultimate = self.page_last - 1
        # Make it easy for Django to do a for loop on the page numbers.
        pages = xrange(1, self.page_last + 1)

        self.op_comment     = op_comment
        self._op_comment    = _op_comment
        self.op_content     = op_content
        self._op_content    = _op_content
        self.op_category    = op_category
        self.linked_comment = linked_comment
        self._linked_comment = _linked_comment
        self.page           = page
        self.pages          = pages
        self.per_page       = knobs.COMMENTS_PER_PAGE
        self.num_replies    = num_replies
        self.reply_ids      = reply_ids
        self.explicit_page_view = explicit_page_view
        self.is_author = request.user == _op_comment.author