示例#1
0
    def setUp(self):
        self.now = datetime.datetime.utcnow().replace(tzinfo=utc)
        self.timedelta = datetime.timedelta(15)
        author = User.objects.get(pk=1)

        for count in range(1, 11):
            post = Post(title="Test Post %d Title" % count,
                        text="foo", author=author)

            if count < 6:
                pubdate = self.now - self.timedelta * count
                post.published_date = pubdate

            post.save()
示例#2
0
    def handle(self, *args, **options):
        # now do the things that you want with your models here
        password = "******"
        exp_posts = 50
        exp_users = 20
        avg_votes_per_post = 5
        avg_comments_per_post = 3

        # Post.objects.all().delete()

        rootExists = User.objects.filter(username="******").count()
        if (rootExists < 1):
            self.stdout.write("Creating root user")
            User.objects.create_superuser("root", "*****@*****.**", password)

        num_posts = Post.objects.all().count()
        num_users = User.objects.all().count()
        num_votes = Vote.objects.all().count()
        num_comments = Comment.objects.all().count()

        faker = Faker()

        self.stdout.write("Default password: "******"Number of posts: " + str(num_posts))
        self.stdout.write("Number of votes: " + str(num_votes))
        self.stdout.write("Number of comments: " + str(num_comments))
        self.stdout.write("Number of users: " + str(num_users))

        if (num_users < exp_users):
            self.stdout.write("Creating " + str(exp_users - num_users) +
                              " Users")
            for x in range(num_users + 1, exp_users + 1):
                username = faker.name().replace(" ", ".")
                email = username + "@test.com"
                User.objects.create_superuser(username, email, password)
                self.stdout.write("   " + username)
            num_users = User.objects.all().count()

        if (num_posts < exp_posts):
            self.stdout.write("Creating " + str(exp_posts - num_posts) +
                              " Posts")
            for x in range(num_posts + 1, exp_posts + 1):
                day_offset = randint(1, 364)
                pub_date = timezone.now() - datetime.timedelta(days=day_offset)
                u = self.randomUser()
                title = faker.text()
                self.stdout.write("   [" + u.username + "] " + title)
                rec = Post()
                rec.author = u
                rec.title = title
                rec.text = faker.text()
                rec.created_date = pub_date
                rec.published_date = pub_date
                rec.save()
            num_posts = Post.objects.all().count()

        self.stdout.write("Voting...")
        while (num_votes < avg_votes_per_post * num_posts):
            user = self.randomUser()
            post = self.randomPost()
            voted = Vote.objects.all().filter(post=post, author=user).count()
            if (post.author != user and voted == 0):
                rec = Vote()
                rec.author = user
                rec.post = post
                rec.created_date = self.randomDate(post.created_date,
                                                   timezone.now())
                rec.save()
                num_votes = Vote.objects.all().count()
                self.stdout.write(
                    str(num_votes) + ": " + user.username + " voted for [" +
                    post.title + "]")

        self.stdout.write("Commenting...")
        while (num_comments < avg_comments_per_post * num_posts):
            user = self.randomUser()
            post = self.randomPost()
            rec = Comment()
            rec.author = user
            rec.post = post
            rec.contents = faker.text()
            rec.created_date = self.randomDate(post.created_date,
                                               timezone.now())
            rec.save()
            num_comments = Comment.objects.all().count()
            self.stdout.write(
                str(num_comments) + ": " + user.username + " commented on [" +
                post.title + "]")