Esempio n. 1
0
File: vote.py Progetto: vin/reddit
    def vote(cls, sub, obj, dir, ip, spam=False, organic=False):
        from admintools import valid_user, valid_thing, update_score
        from r2.lib.count import incr_counts

        sr = obj.subreddit_slow
        kind = obj.__class__.__name__.lower()
        karma = sub.karma(kind, sr)

        is_self_link = kind == "link" and hasattr(obj, "is_self") and obj.is_self

        # check for old vote
        rel = cls.rel(sub, obj)
        oldvote = list(rel._query(rel.c._thing1_id == sub._id, rel.c._thing2_id == obj._id, data=True))

        amount = 1 if dir is True else 0 if dir is None else -1

        is_new = False
        # old vote
        if len(oldvote):
            v = oldvote[0]
            oldamount = int(v._name)
            v._name = str(amount)

            # these still need to be recalculated
            old_valid_thing = v.valid_thing
            v.valid_thing = valid_thing(v, karma) and v.valid_thing and not spam
            v.valid_user = v.valid_user and v.valid_thing and valid_user(v, sr, karma)
        # new vote
        else:
            is_new = True
            oldamount = 0
            v = rel(sub, obj, str(amount))
            v.author_id = obj.author_id
            v.ip = ip
            old_valid_thing = v.valid_thing = valid_thing(v, karma) and not spam
            v.valid_user = v.valid_thing and valid_user(v, sr, karma) and not is_self_link
            if organic:
                v.organic = organic

        v._commit()

        up_change, down_change = score_changes(amount, oldamount)

        update_score(obj, up_change, down_change, v.valid_thing, old_valid_thing)

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change - down_change)

        # update the sr's valid vote count
        if is_new and v.valid_thing and kind == "link":
            if sub._id != obj.author_id:
                incr_counts([sr])

        return v
Esempio n. 2
0
    def vote(cls, sub, obj, dir, ip, spam = False, organic = False):
        from admintools import valid_user, valid_thing, update_score
        from r2.lib.count import incr_counts

        # An account can only perform 1 voting operation at a time.
        with g.make_lock('account_%s_voting' % sub._id):
            # If downvoting ensure that the user has enough karma, it
            # will raise an exception if not.
            if dir == False:
                sub.check_downvote()

            # Do the voting.
            sr = obj.subreddit_slow
            kind = obj.__class__.__name__.lower()
            karma = sub.karma(kind, sr)

            #check for old vote
            rel = cls.rel(sub, obj)
            oldvote = list(rel._query(rel.c._thing1_id == sub._id,
                                      rel.c._thing2_id == obj._id,
                                      data = True))

            amount = 1 if dir is True else 0 if dir is None else -1

            is_new = False
            #old vote
            if len(oldvote):
                v = oldvote[0]
                oldamount = int(v._name)
                v._name = str(amount)

                #these still need to be recalculated
                old_valid_thing = getattr(v, 'valid_thing', True)
                v.valid_thing = (valid_thing(v, karma)
                                 and v.valid_thing
                                 and not spam)
                v.valid_user = (v.valid_user
                                and v.valid_thing
                                and valid_user(v, sr, karma))
            #new vote
            else:
                is_new = True
                oldamount = 0
                v = rel(sub, obj, str(amount))
                v.author_id = obj.author_id
                v.ip = ip
                old_valid_thing = v.valid_thing = (valid_thing(v, karma) and
                                                   not spam)
                v.valid_user = v.valid_thing and valid_user(v, sr, karma)
                if organic:
                    v.organic = organic

            v._commit()

            # Record that this account has made a downvote and
            # immediately release the lock since both the downvote
            # count and the vote have been updated.
            up_change, down_change = score_changes(amount, oldamount)
            if down_change:
                sub.incr_downvote(down_change)

        # Continue by updating karmas.
        update_score(obj, up_change, down_change,
                     v.valid_thing, old_valid_thing)

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change - down_change)

        #update the sr's valid vote count
        if is_new and v.valid_thing and kind == 'link':
            if sub._id != obj.author_id:
                incr_counts([sr])

        return v
Esempio n. 3
0
    def vote(cls, sub, obj, dir, ip, organic = False, cheater = False,
             timer=None):
        from admintools import valid_user, valid_thing, update_score
        from r2.lib.count import incr_sr_count
        from r2.lib.db import queries

        if timer is None:
            timer = SimpleSillyStub()

        sr = obj.subreddit_slow
        kind = obj.__class__.__name__.lower()
        karma = sub.karma(kind, sr)

        is_self_link = (kind == 'link'
                        and getattr(obj,'is_self',False))

        #check for old vote
        rel = cls.rel(sub, obj)
        oldvote = rel._fast_query(sub, obj, ['-1', '0', '1']).values()
        oldvote = filter(None, oldvote)

        timer.intermediate("pg_read_vote")

        amount = 1 if dir is True else 0 if dir is None else -1

        is_new = False
        #old vote
        if len(oldvote):
            v = oldvote[0]
            oldamount = int(v._name)
            v._name = str(amount)

            #these still need to be recalculated
            old_valid_thing = getattr(v, 'valid_thing', False)
            v.valid_thing = (valid_thing(v, karma, cheater = cheater)
                             and getattr(v,'valid_thing', False))
            v.valid_user = (getattr(v, 'valid_user', False)                   
                            and v.valid_thing
                            and valid_user(v, sr, karma))
        #new vote
        else:
            is_new = True
            oldamount = 0
            v = rel(sub, obj, str(amount))
            v.ip = ip
            old_valid_thing = v.valid_thing = valid_thing(v, karma, cheater = cheater)
            v.valid_user = (v.valid_thing and valid_user(v, sr, karma)
                            and not is_self_link)
            if organic:
                v.organic = organic

        v._commit()

        timer.intermediate("pg_write_vote")

        up_change, down_change = score_changes(amount, oldamount)

        if not (is_new and obj.author_id == sub._id and amount == 1):
            # we don't do this if it's the author's initial automatic
            # vote, because we checked it in with _ups == 1
            update_score(obj, up_change, down_change,
                         v, old_valid_thing)
            timer.intermediate("pg_update_score")

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change - down_change)
            timer.intermediate("pg_incr_karma")

        #update the sr's valid vote count
        if is_new and v.valid_thing and kind == 'link':
            if sub._id != obj.author_id:
                incr_sr_count(sr)
            timer.intermediate("incr_sr_counts")

        # now write it out to Cassandra. We'll write it out to both
        # this way for a while
        VotesByAccount.copy_from(v)
        timer.intermediate("cassavotes")

        queries.changed(v._thing2, True)
        timer.intermediate("changed")

        return v
Esempio n. 4
0
    def vote(cls, sub, obj, dir, ip, organic=False, cheater=False):
        from admintools import valid_user, valid_thing, update_score
        from r2.lib.count import incr_sr_count
        from r2.lib.db import queries

        sr = obj.subreddit_slow
        kind = obj.__class__.__name__.lower()
        karma = sub.karma(kind, sr)

        is_self_link = (kind == 'link' and getattr(obj, 'is_self', False))

        #check for old vote
        rel = cls.rel(sub, obj)
        oldvote = rel._fast_query(sub, obj, ['-1', '0', '1']).values()
        oldvote = filter(None, oldvote)

        amount = 1 if dir is True else 0 if dir is None else -1

        is_new = False
        #old vote
        if len(oldvote):
            v = oldvote[0]
            oldamount = int(v._name)
            v._name = str(amount)

            #these still need to be recalculated
            old_valid_thing = getattr(v, 'valid_thing', False)
            v.valid_thing = (valid_thing(v, karma, cheater=cheater)
                             and getattr(v, 'valid_thing', False))
            v.valid_user = (getattr(v, 'valid_user', False) and v.valid_thing
                            and valid_user(v, sr, karma))
        #new vote
        else:
            is_new = True
            oldamount = 0
            v = rel(sub, obj, str(amount))
            v.ip = ip
            old_valid_thing = v.valid_thing = valid_thing(v,
                                                          karma,
                                                          cheater=cheater)
            v.valid_user = (v.valid_thing and valid_user(v, sr, karma)
                            and not is_self_link)
            if organic:
                v.organic = organic

        v._commit()

        up_change, down_change = score_changes(amount, oldamount)

        if not (is_new and obj.author_id == sub._id and amount == 1):
            # we don't do this if it's the author's initial automatic
            # vote, because we checked it in with _ups == 1
            update_score(obj, up_change, down_change, v, old_valid_thing)

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change - down_change)

        #update the sr's valid vote count
        if is_new and v.valid_thing and kind == 'link':
            if sub._id != obj.author_id:
                incr_sr_count(sr)

        # now write it out to Cassandra. We'll write it out to both
        # this way for a while
        CassandraVote._copy_from(v)

        queries.changed(v._thing2, True)

        return v
Esempio n. 5
0
    def vote(cls, sub, obj, dir, ip, organic=False, cheater=False):
        from admintools import valid_user, valid_thing, update_score
        from r2.lib.count import incr_sr_count
        from r2.lib.db import queries

        sr = obj.subreddit_slow
        kind = obj.__class__.__name__.lower()
        karma = sub.karma(kind, sr)

        is_self_link = kind == "link" and getattr(obj, "is_self", False)

        # check for old vote
        rel = cls.rel(sub, obj)
        oldvote = rel._fast_query(sub, obj, ["-1", "0", "1"]).values()
        oldvote = filter(None, oldvote)

        amount = 1 if dir is True else 0 if dir is None else -1

        is_new = False
        # old vote
        if len(oldvote):
            v = oldvote[0]
            oldamount = int(v._name)
            v._name = str(amount)

            # these still need to be recalculated
            old_valid_thing = getattr(v, "valid_thing", False)
            v.valid_thing = valid_thing(v, karma, cheater=cheater) and getattr(v, "valid_thing", False)
            v.valid_user = v.valid_user and v.valid_thing and valid_user(v, sr, karma)
        # new vote
        else:
            is_new = True
            oldamount = 0
            v = rel(sub, obj, str(amount))
            v.ip = ip
            old_valid_thing = v.valid_thing = valid_thing(v, karma, cheater=cheater)
            v.valid_user = v.valid_thing and valid_user(v, sr, karma) and not is_self_link
            if organic:
                v.organic = organic

        v._commit()

        v._fast_query_timestamp_touch(sub)

        up_change, down_change = score_changes(amount, oldamount)

        if not (is_new and obj.author_id == sub._id and amount == 1):
            # we don't do this if it's the author's initial automatic
            # vote, because we checked it in with _ups == 1
            update_score(obj, up_change, down_change, v.valid_thing, old_valid_thing)

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change - down_change)

        # update the sr's valid vote count
        if is_new and v.valid_thing and kind == "link":
            if sub._id != obj.author_id:
                incr_sr_count(sr)

        # now write it out to Cassandra. We'll write it out to both
        # this way for a while
        voter = v._thing1
        votee = v._thing2
        cvc = CassandraVote._rel(Account, votee.__class__)
        try:
            cv = cvc._fast_query(voter._id36, votee._id36)
        except tdb_cassandra.NotFound:
            cv = cvc(thing1_id=voter._id36, thing2_id=votee._id36)
        cv.name = v._name
        cv.valid_user, cv.valid_thing = v.valid_user, v.valid_thing
        cv.ip = v.ip
        if getattr(v, "organic", False) or hasattr(cv, "organic"):
            cv.organic = getattr(v, "organic", False)
        cv._commit()

        queries.changed(votee, True)

        return v
Esempio n. 6
0
    def vote(cls, sub, obj, dir, ip, organic=False, cheater=False):
        from admintools import valid_user, valid_thing, update_score
        from r2.lib.count import incr_counts
        from r2.lib.db import queries

        sr = obj.subreddit_slow
        kind = obj.__class__.__name__.lower()
        karma = sub.karma(kind, sr)

        is_self_link = (kind == 'link' and hasattr(obj, 'is_self')
                        and obj.is_self)

        #check for old vote
        rel = cls.rel(sub, obj)
        oldvote = rel._fast_query(sub, obj, ['-1', '0', '1']).values()
        oldvote = filter(None, oldvote)

        amount = 1 if dir is True else 0 if dir is None else -1

        is_new = False
        #old vote
        if len(oldvote):
            v = oldvote[0]
            oldamount = int(v._name)
            v._name = str(amount)

            #these still need to be recalculated
            old_valid_thing = v.valid_thing
            v.valid_thing = (valid_thing(v, karma, cheater=cheater)
                             and v.valid_thing)
            v.valid_user = (v.valid_user and v.valid_thing
                            and valid_user(v, sr, karma))
        #new vote
        else:
            is_new = True
            oldamount = 0
            v = rel(sub, obj, str(amount))
            v.author_id = obj.author_id
            v.sr_id = sr._id
            v.ip = ip
            old_valid_thing = v.valid_thing = \
                              valid_thing(v, karma, cheater = cheater)
            v.valid_user = (v.valid_thing and valid_user(v, sr, karma)
                            and not is_self_link)
            if organic:
                v.organic = organic

        v._commit()

        v._fast_query_timestamp_touch(sub)

        up_change, down_change = score_changes(amount, oldamount)

        if not (is_new and obj.author_id == sub._id and amount == 1):
            # we don't do this if it's the author's initial automatic
            # vote, because we checked it in with _ups == 1
            update_score(obj, up_change, down_change, v.valid_thing,
                         old_valid_thing)

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change - down_change)

        #update the sr's valid vote count
        if is_new and v.valid_thing and kind == 'link':
            if sub._id != obj.author_id:
                incr_counts([sr])

        # now write it out to Cassandra. We'll write it out to both
        # this way for a while
        voter = v._thing1
        votee = v._thing2
        cvc = CassandraVote._rel(Account, votee.__class__)
        try:
            cv = cvc._fast_query(voter._id36, votee._id36)
        except tdb_cassandra.NotFound:
            cv = cvc(thing1_id=voter._id36, thing2_id=votee._id36)
        cv.name = v._name
        cv.valid_user, cv.valid_thing = v.valid_user, v.valid_thing
        cv.ip = v.ip
        if getattr(v, 'organic', False) or hasattr(cv, 'organic'):
            cv.organic = getattr(v, 'organic', False)
        cv._commit()

        queries.changed(votee, True)

        return v
Esempio n. 7
0
File: vote.py Progetto: cmak/reddit
    def vote(cls, sub, obj, dir, ip, spam = False, organic = False):
        from admintools import valid_user, valid_thing, update_score
        sr = obj.subreddit_slow
        kind = obj.__class__.__name__.lower()
        karma = sub.karma(kind, sr)
        
        #check for old vote
        rel = cls.rel(sub, obj)
        oldvote = list(rel._query(rel.c._thing1_id == sub._id,
                                  rel.c._thing2_id == obj._id,
                                  data = True))
        
        amount = 1 if dir is True else 0 if dir is None else -1

        is_new = False
        #old vote
        if len(oldvote):
            v = oldvote[0]
            oldamount = int(v._name)
            v._name = str(amount)

            #these still need to be recalculated
            old_valid_thing = v.valid_thing
            v.valid_thing = (v.valid_thing
                             and (not spam)
                             and valid_thing(v, karma))
            v.valid_user = (v.valid_user
                            and v.valid_thing
                            and valid_user(v, sr, karma))
        #new vote
        else:
            is_new = True
            oldamount = 0
            v = rel(sub, obj, str(amount))
            v.author_id = obj.author_id
            v.ip = ip
            old_valid_thing = v.valid_thing = ((not spam)
                                               and valid_thing(v, karma))
            v.valid_user = v.valid_thing and valid_user(v, sr, karma)
            if organic:
                v.organic = organic

        v._commit()

        up_change, down_change = score_changes(amount, oldamount)

        update_score(obj, up_change, down_change,
                     v.valid_thing, old_valid_thing)

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change - down_change)

        #update the sr's valid vote count
#         if is_new and v.valid_thing and kind == 'link':
#             if sub._id != obj.author_id:
#                 sr._incr('valid_votes', 1)

        #expire the sr
        if kind == 'link' and v.valid_thing:
            expire_hot(sr)
Esempio n. 8
0
    def vote(cls, sub, obj, dir, ip, spam=False, organic=False):
        from admintools import valid_user, valid_thing, update_score
        from r2.lib.count import incr_counts

        # An account can only perform 1 voting operation at a time.
        with g.make_lock('account_%s_voting' % sub._id) as lock:
            kind = obj.__class__.__name__.lower()

            lock.log('voting checkpoint A')

            # If downvoting ensure that the user has enough karma, it
            # will raise an exception if not.
            if dir == False:
                sub.check_downvote(kind)

            lock.log('voting checkpoint B')

            # Do the voting.
            sr = obj.subreddit_slow
            karma = sub.karma(kind, sr)

            lock.log('voting checkpoint C')

            #check for old vote
            rel = cls.rel(sub, obj)
            oldvote = list(
                rel._query(rel.c._thing1_id == sub._id,
                           rel.c._thing2_id == obj._id,
                           data=True))

            amount = 1 if dir is True else 0 if dir is None else -1
            # Users have a vote_multiplier which effects how much their votes are worth
            if isinstance(sub, Account):
                amount *= sub.vote_multiplier

            lock.log('voting checkpoint D')

            is_new = False
            #old vote
            if len(oldvote):
                v = oldvote[0]
                oldamount = int(v._name)
                v._name = str(amount)

                #these still need to be recalculated
                old_valid_thing = getattr(v, 'valid_thing', True)
                v.valid_thing = (valid_thing(v, karma) and v.valid_thing
                                 and not spam)
                v.valid_user = (v.valid_user and v.valid_thing
                                and valid_user(v, sr, karma))
            #new vote
            else:
                is_new = True
                oldamount = 0
                v = rel(sub, obj, str(amount))
                v.author_id = obj.author_id
                v.ip = ip
                old_valid_thing = v.valid_thing = (valid_thing(v, karma)
                                                   and not spam)
                v.valid_user = v.valid_thing and valid_user(v, sr, karma)
                if organic:
                    v.organic = organic

            lock.log('voting checkpoint E')
            v._commit()
            lock.log('voting checkpoint F')

            # Record that this account has made a downvote.
            up_change, down_change = score_changes(amount, oldamount)
            if down_change:
                sub.incr_downvote(down_change, kind)

            lock.log('voting checkpoint G')

        # Release the lock since both the downvote count and the vote count
        # have been updated, and then continue by updating karmas.
        update_score(obj, up_change, down_change, v.valid_thing,
                     old_valid_thing)

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change, down_change)

        #update the sr's valid vote count
        if is_new and v.valid_thing and kind == 'link':
            if sub._id != obj.author_id:
                incr_counts([sr])

        return v
Esempio n. 9
0
    def vote(cls, sub, obj, dir, ip, organic = False, cheater = False):
        from admintools import valid_user, valid_thing, update_score
        from r2.lib.count import incr_counts
        from r2.lib.db import queries

        sr = obj.subreddit_slow
        kind = obj.__class__.__name__.lower()
        karma = sub.karma(kind, sr)

        is_self_link = (kind == 'link'
                        and hasattr(obj,'is_self')
                        and obj.is_self)

        #check for old vote
        rel = cls.rel(sub, obj)
        oldvote = rel._fast_query(sub, obj, ['-1', '0', '1']).values()
        oldvote = filter(None, oldvote)

        amount = 1 if dir is True else 0 if dir is None else -1

        is_new = False
        #old vote
        if len(oldvote):
            v = oldvote[0]
            oldamount = int(v._name)
            v._name = str(amount)

            #these still need to be recalculated
            old_valid_thing = v.valid_thing
            v.valid_thing = (valid_thing(v, karma, cheater = cheater)
                             and v.valid_thing)
            v.valid_user = (v.valid_user
                            and v.valid_thing
                            and valid_user(v, sr, karma))
        #new vote
        else:
            is_new = True
            oldamount = 0
            v = rel(sub, obj, str(amount))
            v.author_id = obj.author_id
            v.sr_id = sr._id
            v.ip = ip
            old_valid_thing = v.valid_thing = \
                              valid_thing(v, karma, cheater = cheater)
            v.valid_user = (v.valid_thing and valid_user(v, sr, karma)
                            and not is_self_link)
            if organic:
                v.organic = organic

        v._commit()
        g.cache.delete(queries.prequeued_vote_key(sub, obj))

        v._fast_query_timestamp_touch(sub)

        up_change, down_change = score_changes(amount, oldamount)

        if not (is_new and obj.author_id == sub._id and amount == 1):
            # we don't do this if it's the author's initial automatic
            # vote, because we checked it in with _ups == 1
            update_score(obj, up_change, down_change,
                         v.valid_thing, old_valid_thing)

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change - down_change)

        #update the sr's valid vote count
        if is_new and v.valid_thing and kind == 'link':
            if sub._id != obj.author_id:
                incr_counts([sr])

        return v
Esempio n. 10
0
    def vote(cls, sub, obj, dir, ip, organic = False):
        from admintools import valid_user, valid_thing, update_score
        from r2.lib.count import incr_counts

        sr = obj.subdigg_slow
        kind = obj.__class__.__name__.lower()
        karma = sub.karma(kind, sr)

        is_self_link = (kind == 'link'
                        and hasattr(obj,'is_self')
                        and obj.is_self)
        
        #check for old vote
        rel = cls.rel(sub, obj)
        oldvote = rel._fast_query(sub, obj, ['-1', '0', '1']).values()
        oldvote = filter(None, oldvote)

        amount = 1 if dir is True else 0 if dir is None else -1

        is_new = False
        #old vote
        if len(oldvote):
            v = oldvote[0]
            oldamount = int(v._name)
            v._name = str(amount)

            #these still need to be recalculated
            old_valid_thing = v.valid_thing
            v.valid_thing = (valid_thing(v, karma)
                             and v.valid_thing)
            v.valid_user = (v.valid_user
                            and v.valid_thing
                            and valid_user(v, sr, karma))
        #new vote
        else:
            is_new = True
            oldamount = 0
            v = rel(sub, obj, str(amount))
            v.author_id = obj.author_id
            v.sr_id = sr._id
            v.ip = ip
            old_valid_thing = v.valid_thing = (valid_thing(v, karma))
            v.valid_user = (v.valid_thing and valid_user(v, sr, karma)
                            and not is_self_link)
            if organic:
                v.organic = organic

        v._commit()

        up_change, down_change = score_changes(amount, oldamount)

        update_score(obj, up_change, down_change,
                     v.valid_thing, old_valid_thing)

        if v.valid_user:
            author = Account._byID(obj.author_id, data=True)
            author.incr_karma(kind, sr, up_change - down_change)

        #update the sr's valid vote count
        if is_new and v.valid_thing and kind == 'link':
            if sub._id != obj.author_id:
                incr_counts([sr])

        return v