def check_downvote(self, vote_kind):
        """Checks whether this account has enough karma to cast a downvote.

        vote_kind is 'link' or 'comment' depending on the type of vote that's
        being cast.

        This makes the assumption that the user can't cast a vote for something
        on the non-current subreddit.
        """
        from r2.models.vote import Vote, Link, Comment

        def get_cached_downvotes(content_cls):
            kind = content_cls.__name__.lower()
            downvotes = g.cache.get(self.vote_cache_key(kind))
            if downvotes is None:
                vote_cls = Vote.rel(Account, content_cls)
                downvotes = len(list(vote_cls._query(Vote.c._thing1_id == self._id,
                                                          Vote.c._name == str(-1))))
                g.cache.set(self.vote_cache_key(kind), downvotes)
            return downvotes

        link_downvote_karma = get_cached_downvotes(Link) * c.current_or_default_sr.post_karma_multiplier
        comment_downvote_karma = get_cached_downvotes(Comment)
        karma_spent = link_downvote_karma + comment_downvote_karma

        karma_balance = self.safe_karma * 4
        vote_cost = c.current_or_default_sr.post_karma_multiplier if vote_kind == 'link' else 1
        if karma_spent + vote_cost > karma_balance:
            points_needed = abs(karma_balance - karma_spent - vote_cost)
            msg = strings.not_enough_downvote_karma % (points_needed, plurals.N_points(points_needed))
            raise NotEnoughKarma(msg)
Beispiel #2
0
    def check_downvote(self, vote_kind):
        """Checks whether this account has enough karma to cast a downvote.

        vote_kind is 'link' or 'comment' depending on the type of vote that's
        being cast.

        This makes the assumption that the user can't cast a vote for something
        on the non-current subreddit.
        """

        raise NotEnoughKarma('Downvoting temporarily disabled.')
        from r2.models.vote import Vote, Link, Comment

        def get_cached_downvotes(content_cls):
            kind = content_cls.__name__.lower()
            cache_key = self.downvote_cache_key(kind)
            downvotes = g.cache.get(cache_key)
            if downvotes is None:
                vote_cls = Vote.rel(Account, content_cls)

                # Get a count of content_cls downvotes
                type = tdb.rel_types_id[vote_cls._type_id]
                # rt = rel table
                # dt = data table
                # tt = thing table
                rt, account_tt, content_cls_tt, dt = type.rel_table

                cols = [sa.func.count(rt.c.rel_id)]
                where = sa.and_(rt.c.thing1_id == self._id, rt.c.name == '-1')
                query = sa.select(cols, where)
                downvotes = query.execute().scalar()

                g.cache.set(cache_key, downvotes)
            return downvotes

        link_downvote_karma = get_cached_downvotes(
            Link) * c.current_or_default_sr.post_karma_multiplier
        comment_downvote_karma = get_cached_downvotes(Comment)
        karma_spent = link_downvote_karma + comment_downvote_karma

        karma_balance = self.safe_karma * 4
        vote_cost = c.current_or_default_sr.post_karma_multiplier if vote_kind == 'link' else 1
        vote_cost *= self.vote_multiplier
        if karma_spent + vote_cost > karma_balance:
            points_needed = abs(karma_balance - karma_spent - vote_cost)
            msg = strings.not_enough_downvote_karma % (
                points_needed, plurals.N_points(points_needed))
            raise NotEnoughKarma(msg)