Esempio n. 1
0
def test_setup(**kwargs):
    from random import choice
    from django.contrib.auth.models import User
    from snapboard.models import Thread, Post, Category, UserProfile
    from snapboard import sampledata

    if not settings.DEBUG:
        return 

    if Thread.objects.all().count() > 0:
        # return, since there seem to already be threads in the database.
        return
    
    # ask for permission to create the test
    msg = """
    You've installed SNAPboard with DEBUG=True, do you want to populate
    the board with random users/threads/posts to test-drive the application?
    (yes/no):
    """
    populate = raw_input(msg).strip()
    while not (populate == "yes" or populate == "no"):
        populate = raw_input("\nPlease type 'yes' or 'no': ").strip()
    if populate == "no":
        return

    # create 10 random users

    users = ('john', 'sally', 'susan', 'amanda', 'bob', 'tully', 'fran')
    for u in users:
        user = User.objects.get_or_create(username=u)
        profile = UserProfile.objects.get_or_create(user=user[0], website='abc.com')
        # user.is_staff = True

    cats = ('Random Topics',
            'Good Deals',
            'Skiing in the Vermont Area',
            'The Best Restaurants')
    for c in cats:
        cat = Category.objects.get_or_create(label=c)

    # create up to 30 posts
    tc = range(1, 50)
    for i in range(0, 35):
        print 'thread ', i, 'created'
        cat= choice(Category.objects.all())
        subj = choice(sampledata.objects.split('\n'))
        thread = Thread(subject=subj, category=cat)
        thread.save()

        for j in range(0, choice(tc)):
            text = '\n\n'.join([sampledata.sample_data() for x in range(0, choice(range(2, 5)))])
            # create a post
            post = Post(
                    user=choice(User.objects.all()),
                    thread=thread,
                    text=text,
                    ip='.'.join([str(choice(range(1,255))) for x in (1,2,3,4)]),
                    )
            # allows setting of arbitrary ip
            post.management_save()
Esempio n. 2
0
def make_random_post_tree(parent_post, amount, thread, ratio=0.5):
    text = '\n\n'.join([sampledata.sample_data()
      for x in range(0, randrange(1, 6))])
    # the post data
    postdata = {
      "user": choice(User.objects.all()),
      "thread": thread,
      "text": text,
      "ip": '.'.join([str(choice(range(2, 254))) for x in xrange(4)]),
    }
    # Create a post in tree.
    if parent_post is None:  # Root node.
        post = Post.add_root(**postdata)
    else:
        post = parent_post.add_child(**postdata)
    # allows setting of arbitrary ip
    post.save()
    posts_remain = amount - 1  # Minus just-created one

    # ... got better ideas?
    # (got non-uniform random distributions?)
    # Anyway, ratio ~= depth/width. Don't delegate too few / too much to
    # each child depending on the ratio.
    fmin = lambda ratio, posts_remain: int(posts_remain * (ratio - 0.5)
      * 2) + 1 if ratio > 0.5 else 1
    fmax = lambda ratio, posts_remain: int(posts_remain * ratio * 2) + 2 \
      if ratio < 0.5 else posts_remain + 1

    while posts_remain > 0:  # Distribute remnants
        next_tree_posts = randrange(fmin(ratio, posts_remain),
          fmax(ratio, posts_remain))
        #print(" D: delegating %d,   %d remain " % (xx, x-xx))
        make_random_post_tree(post, next_tree_posts, thread)
        posts_remain -= next_tree_posts
Esempio n. 3
0
def test_setup(**kwargs):
    from random import choice
    from django.contrib.auth.models import User
    from snapboard.models import Thread, Post, Category
    from snapboard import sampledata

    if not settings.DEBUG:
        return 

    if Thread.objects.all().count() > 0:
        # return, since there seem to already be threads in the database.
        return
    
    # ask for permission to create the test
    msg = """
    You've installed SNAPboard with DEBUG=True, do you want to populate
    the board with random users/threads/posts to test-drive the application?
    (yes/no):
    """
    populate = raw_input(msg).strip()
    while not (populate == "yes" or populate == "no"):
        populate = raw_input("\nPlease type 'yes' or 'no': ").strip()
    if populate == "no":
        return

    # create 10 random users

    users = ('john', 'sally', 'susan', 'amanda', 'bob', 'tully', 'fran')
    for u in users:
        user = User.objects.get_or_create(username=u)
        # user.is_staff = True

    cats = ('Random Topics',
            'Good Deals',
            'Skiing in the Vermont Area',
            'The Best Restaurants')
    for c in cats:
        cat = Category.objects.get_or_create(label=c)

    # create up to 30 posts
    tc = range(1, 50)
    for i in range(0, 35):
        print 'thread ', i, 'created'
        cat= choice(Category.objects.all())
        subj = choice(sampledata.objects.split('\n'))
        thread = Thread(subject=subj, category=cat)
        thread.save()

        for j in range(0, choice(tc)):
            text = '\n\n'.join([sampledata.sample_data() for x in range(0, choice(range(2, 5)))])
            # create a post
            post = Post(
                    user=choice(User.objects.all()),
                    thread=thread,
                    text=text,
                    ip='.'.join([str(choice(range(1,255))) for x in (1,2,3,4)]),
                    )
            # allows setting of arbitrary ip
            post.management_save()