Exemple #1
0
def populate(num_srs=10, num_users=1000, num_links=100, num_comments=20, num_votes=50):
    try:
        a = Account._by_name(g.system_user)
    except NotFound:
        a = register(g.system_user, "password")

    srs = []
    for i in range(num_srs):
        name = "reddit_test%d" % i
        try:
            sr = Subreddit._new(name=name, title="everything about #%d" % i, ip="0.0.0.0", author_id=a._id)
            sr._downs = 10
            sr.lang = "en"
            sr._commit()
        except SubredditExists:
            sr = Subreddit._by_name(name)
        srs.append(sr)

    accounts = []
    for i in range(num_users):
        name_ext = "".join([random.choice(string.letters) for x in range(int(random.uniform(1, 10)))])
        name = "test_" + name_ext
        try:
            a = register(name, name)
        except AccountExists:
            a = Account._by_name(name)
        accounts.append(a)

    for i in range(num_links):
        id = random.uniform(1, 100)
        title = url = "http://google.com/?q=" + str(id)
        user = random.choice(accounts)
        sr = random.choice(srs)
        l = Link._submit(title, url, user, sr, "127.0.0.1")
        queries.new_link(l)

        comments = [None]
        for i in range(int(random.betavariate(2, 8) * 5 * num_comments)):
            user = random.choice(accounts)
            body = " ".join([random_word(1, 10) for x in range(int(200 * random.betavariate(2, 6)))])
            parent = random.choice(comments)
            (c, inbox_rel) = Comment._new(user, l, parent, body, "127.0.0.1")
            queries.new_comment(c, inbox_rel)
            comments.append(c)
            for i in range(int(random.betavariate(2, 8) * 10)):
                another_user = random.choice(accounts)
                v = Vote.vote(another_user, c, True, "127.0.0.1")
                queries.new_vote(v)

        like = random.randint(50, 100)
        for i in range(int(random.betavariate(2, 8) * 5 * num_votes)):
            user = random.choice(accounts)
            v = Vote.vote(user, l, random.randint(0, 100) <= like, "127.0.0.1")
            queries.new_vote(v)

    queries.worker.join()
    def load_comments(link, comments, parent_comment=None):
        for comment_data in comments:
            comment_author = Account._by_name(comment_data['author'])
            (c, inbox_rel) = Comment._new(comment_author, link, parent_comment, comment_data['body'], comment_data['ip'])
            queries.new_comment(c, inbox_rel)

            for i in range(int(random.betavariate(2, 8) * 10)):
                another_user = random.choice(accounts)
                v = Vote.vote(another_user, c, True, '127.0.0.1')
                queries.new_vote(v)
            
            if comment_data.has_key('comments'):
                load_comments(link, comment_data['comments'], c)
Exemple #3
0
    def commit(self):
        """Apply the vote's effects and persist it."""
        if self.previous_vote and self == self.previous_vote:
            return

        self.apply_effects()
        VotesByAccount.write_vote(self)

        # Always update the search index if the thing has fewer than 20 votes.
        # When the thing has more votes queue an update less often.
        if self.thing.num_votes < 20 or self.thing.num_votes % 10 == 0:
            self.thing.update_search_index(boost_only=True)

        from r2.lib.db.queries import new_vote
        new_vote(self)

        if self.event_data:
            g.events.vote_event(self)

        g.stats.simple_event('vote.total')
Exemple #4
0
    def commit(self):
        """Apply the vote's effects and persist it."""
        if self.previous_vote and self == self.previous_vote:
            return

        self.apply_effects()
        VotesByAccount.write_vote(self)

        # Always update the search index if the thing has fewer than 20 votes.
        # When the thing has more votes queue an update less often.
        if self.thing.num_votes < 20 or self.thing.num_votes % 10 == 0:
            self.thing.update_search_index(boost_only=True)

        from r2.lib.db.queries import new_vote
        new_vote(self)

        if self.event_data:
            g.events.vote_event(self)

        g.stats.simple_event('vote.total')
def load_fixtures(num_votes = 10):
    accounts = []
    for name, account_data in load_fixture('accounts').items():
        print "creating account %r" % (name,)
        try:
            a = Account._by_name(name)
        except NotFound:
            a = Account(name=name, password=bcrypt_password(account_data['password']))
            # new accounts keep the profanity filter settings until opting out
        for key, val in account_data.items():
            if key in ('password',):
                continue
            setattr(a, key, val)
        a._commit()

        # clear the caches
        Account._by_name(name, _update = True)
        Account._by_name(name, allow_deleted = True, _update = True)
        accounts.append(a)

    for name, subreddit_data in load_fixture('subreddits').items():
        print "creating subreddit %r" % (name,)
        try:
            sr = Subreddit._by_name(name)
        except NotFound:
            author = Account._by_name(subreddit_data['author'])
            sr = Subreddit._new(name = name, 
                title = subreddit_data['title'], 
                author_id = author._id, 
                ip = subreddit_data['ip'])

        for key, val in subreddit_data.items():
            if key in ('author', 'ip', 'title', 'subscribers'):
                continue
            if val is None or val == '':
                continue
            setattr(sr, key, val)
        sr._downs = 10
        sr._commit()

        for sub_name in subreddit_data.get('subscribers', []):
            subscriber = Account._by_name(sub_name)
            Subreddit.subscribe_defaults(subscriber)
            if sr.add_subscriber(subscriber):
                sr._incr('_ups', 1)
            queries.changed(sr, True)

        for mod_name in subreddit_data.get('moderators', []):
            moderator = Account._by_name(mod_name)
            sr.add_moderator(moderator)

    # defined here so it has access to the 'authors' var
    def load_comments(link, comments, parent_comment=None):
        for comment_data in comments:
            comment_author = Account._by_name(comment_data['author'])
            (c, inbox_rel) = Comment._new(comment_author, link, parent_comment, comment_data['body'], comment_data['ip'])
            queries.new_comment(c, inbox_rel)

            for i in range(int(random.betavariate(2, 8) * 10)):
                another_user = random.choice(accounts)
                v = Vote.vote(another_user, c, True, '127.0.0.1')
                queries.new_vote(v)
            
            if comment_data.has_key('comments'):
                load_comments(link, comment_data['comments'], c)

    for link_label, link_data in load_fixture('links').items():
        print "creating link for %r" % (link_data['title'],)
        author = Account._by_name(link_data['author'])
        sr = Subreddit._by_name(link_data['sr'])
        link = Link._submit(link_data['title'], link_data['url'], author, sr, link_data['ip'])
        for key, val in link_data.items():
            if key in ('title', 'url', 'author', 'sr', 'comments'):
                continue
            if val is None or val == '':
                continue
            setattr(link, key, val)
        link._commit()
        queries.new_link(link)

        like = random.randint(50,100)
        for i in range(int(random.betavariate(2, 8) * 5 * num_votes)):
            user = random.choice(accounts)
            v = Vote.vote(user, link, random.randint(0, 100) <= like, '127.0.0.1')
            queries.new_vote(v)
        if link_data.has_key('comments'):
            load_comments(link, link_data['comments'])

    queries.worker.join()
Exemple #6
0
def populate(num_srs = 10, num_users = 1000, num_links = 100, num_comments = 20, num_votes = 50):
    try:
        a = Account._by_name(g.system_user)
    except NotFound:
        a = register(g.system_user, "password", "127.0.0.1")

    srs = []
    for i in range(num_srs):
        name = "reddit_test%d" % i
        try:
            sr = Subreddit._new(name = name, title = "everything about #%d"%i,
                                ip = '0.0.0.0', author_id = a._id)
            sr._downs = 10
            sr.lang = "en"
            sr._commit()
        except SubredditExists:
            sr = Subreddit._by_name(name)
        srs.append(sr)

    accounts = []
    for i in range(num_users):
        name_ext = ''.join([ random.choice(string.letters)
                             for x
                             in range(int(random.uniform(1, 10))) ])
        name = 'test_' + name_ext
        try:
            a = register(name, name, "127.0.0.1")
        except AccountExists:
            a = Account._by_name(name)
        accounts.append(a)

    for i in range(num_links):
        id = random.uniform(1,100)
        title = url = 'http://google.com/?q=' + str(id)
        user = random.choice(accounts)
        sr = random.choice(srs)
        l = Link._submit(title, url, user, sr, '127.0.0.1')
        queries.new_link(l)

        comments = [ None ]
        for i in range(int(random.betavariate(2, 8) * 5 * num_comments)):
            user = random.choice(accounts)
            body = ' '.join([ random_word(1, 10)
                              for x
                              in range(int(200 * random.betavariate(2, 6))) ])
            parent = random.choice(comments)
            (c, inbox_rel) = Comment._new(user, l, parent, body, '127.0.0.1')
            queries.new_comment(c, inbox_rel)
            comments.append(c)
            for i in range(int(random.betavariate(2, 8) * 10)):
                another_user = random.choice(accounts)
                v = Vote.vote(another_user, c, True, '127.0.0.1')
                queries.new_vote(v)

        like = random.randint(50,100)
        for i in range(int(random.betavariate(2, 8) * 5 * num_votes)):
           user = random.choice(accounts)
           v = Vote.vote(user, l, random.randint(0, 100) <= like, '127.0.0.1')
           queries.new_vote(v)

    queries.worker.join()
Exemple #7
0
def populate(num_srs=10,
             num_users=1000,
             num_links=100,
             num_comments=20,
             num_votes=50):
    try:
        a = Account._by_name(g.system_user)
    except NotFound:
        a = register(g.system_user, "password", "127.0.0.1")

    srs = []
    names = [
        'ask_ns',
        'battery_park_city',
        'harlem',
        'chelsea',
        'chinatown',
        'east_harlem',
        'east_village',
        'financial_district',
        'flatiron',
        'fashion_district',
        'gramercy',
        'greenwich_village',
        'hells_kitchen',
        'little_italy',
        'lower_east_side',
        'meatpacking_district',
        'midtown_east',
        'morningside_heights',
        'murray_hill',
        'noho',
        'nolita',
        'nomad',
        'soho',
        'theater_district',
        'tribeca',
        'upper_east_side',
        'upper_west_side',
        'west_village',
        'gowanus',
        'park_slope',
        'carroll_gardens',
        'cobble_hill',
        'boerum_hill',
        'brooklyn_heights',
        'downtown_brooklyn',
        'prospect_heights',
        'fort_greene',
        'clinton_hill',
        'vinegar_hill',
        'dumbo',
        'williamsburg',
        'greenpoint',
        'long_island_city',
    ]
    for name in names:
        try:
            sr = Subreddit._new(name=name,
                                title="everything about %s" % name,
                                ip='0.0.0.0',
                                author_id=a._id)
            sr._downs = 10
            sr.lang = "en"
            sr._commit()
        except SubredditExists:
            sr = Subreddit._by_name(name)
        srs.append(sr)

    accounts = []
    for i in range(num_users):
        name_ext = ''.join([
            random.choice(string.letters)
            for x in range(int(random.uniform(1, 10)))
        ])
        name = 'test_' + name_ext
        try:
            a = register(name, name, "127.0.0.1")
        except AccountExists:
            a = Account._by_name(name)
        accounts.append(a)

    for i in range(num_links):
        id = random.uniform(1, 100)
        title = url = 'http://google.com/?q=' + str(id)
        user = random.choice(accounts)
        sr = random.choice(srs)
        if False:
            l = Link._submit(title, url, user, sr, '127.0.0.1')
            queries.new_link(l)

        comments = [None]
        for i in range(int(random.betavariate(2, 8) * 5 * num_comments)):
            user = random.choice(accounts)
            body = ' '.join([
                random_word(1, 10)
                for x in range(int(200 * random.betavariate(2, 6)))
            ])
            parent = random.choice(comments)
            (c, inbox_rel) = Comment._new(user, l, parent, body, '127.0.0.1')
            queries.new_comment(c, inbox_rel)
            comments.append(c)
            for i in range(int(random.betavariate(2, 8) * 10)):
                another_user = random.choice(accounts)
                v = Vote.vote(another_user, c, True, '127.0.0.1')
                queries.new_vote(v)

        if False:
            like = random.randint(50, 100)
            for i in range(int(random.betavariate(2, 8) * 5 * num_votes)):
                user = random.choice(accounts)
                v = Vote.vote(user, l,
                              random.randint(0, 100) <= like, '127.0.0.1')
                queries.new_vote(v)

    queries.worker.join()
def populate(num_users = 10, num_links = 10, num_comments = 10, num_votes = 10):
    try:
        a = Account._by_name(g.system_user)
    except NotFound:
        a = register(g.system_user, "password", "127.0.0.1")

    srs = []

    default_spaces = ['Announcements', 'NewSpaces', 'Connector', 'NewUser', 'Help',]
    for name in default_spaces:
        try:
            sr = Subreddit._new(name = name, title = name,
                                ip = '0.0.0.0', author_id = a._id)
            sr._downs = 10
            sr.lang = "en"
            sr._commit()
        except SubredditExists:
            sr = Subreddit._by_name(name)
        srs.append(sr)

    accounts = []
    for i in range(num_users):
        name_ext = ''.join([ random.choice(string.letters)
                             for x
                             in range(int(random.uniform(1, 10))) ])
        name = 'test_' + name_ext
        try:
            a = register(name, name, "127.0.0.1")
        except AccountExists:
            a = Account._by_name(name)
        accounts.append(a)

    for i in range(num_links):
        id = random.uniform(1,100)
        title = url = 'http://google.com/?q=' + str(id)
        user = random.choice(accounts)
        sr = random.choice(srs)
        l = Link._submit(title, url, user, sr, '127.0.0.1')
        queries.new_link(l)

        comments = [ None ]
        for i in range(int(random.betavariate(2, 8) * 5 * num_comments)):
            user = random.choice(accounts)
            body = ' '.join([ random_word(1, 10)
                              for x
                              in range(int(200 * random.betavariate(2, 6))) ])
            parent = random.choice(comments)
            (c, inbox_rel) = Comment._new(user, l, parent, body, '127.0.0.1')
            queries.new_comment(c, inbox_rel)
            comments.append(c)
            for i in range(int(random.betavariate(2, 8) * 10)):
                another_user = random.choice(accounts)
                v = Vote.vote(another_user, c, True, '127.0.0.1')
                queries.new_vote(v)

        like = random.randint(50,100)
        for i in range(int(random.betavariate(2, 8) * 5 * num_votes)):
           user = random.choice(accounts)
           v = Vote.vote(user, l, random.randint(0, 100) <= like, '127.0.0.1')
           queries.new_vote(v)

    queries.worker.join()