Beispiel #1
0
def seed_articles():
    roles = db.session.query(Role.id).filter(
        or_(Role.name == 'ROLE_ADMIN', Role.name == 'ROLE_AUTHOR')).all()
    # Not working roles = db.session.query(Role.id).filter(Role.name == v for v in ('ROLE_ADMIN', 'ROLE_AUTHOR')).all()
    roles = [role[0] for role in roles]

    articles_count = db.session.query(func.count(Article.id)).all()[0][0]
    articles_to_seed = 23
    articles_to_seed -= articles_count
    sys.stdout.write('[+] Seeding %d articles\n' % articles_to_seed)
    author_admin_ids = [
        user[0] for user in db.session.query(User.id).filter(
            User.roles.any(UserRole.role_id.in_(roles))).all()
    ]
    tag_ids = [tag[0] for tag in db.session.query(Tag.id).all()]
    category_ids = [
        category[0] for category in db.session.query(Category.id).all()
    ]

    for i in range(articles_count, articles_to_seed):
        title = faker.sentence()
        description = '\n'.join(faker.sentences(2))
        body = faker.text()
        user_id = random.choice(author_admin_ids)

        start_date = datetime.date(year=2017, month=1, day=1)
        random_date = faker.date_between(start_date=start_date, end_date='+4y')
        publish_on = random_date
        a = Article(title=title,
                    body=body,
                    description=description,
                    user_id=user_id,
                    publish_on=publish_on)

        a.tags.append(db.session.query(Tag).order_by(func.random()).first())

        for i in range(0, random.randint(1, 2)):
            a.tags.append(random.choice(tags))

        for i in range(0, random.randint(1, 2)):
            a.categories.append(random.choice(categories))

        db.session.add(a)
        db.session.commit()
Beispiel #2
0
def seed_articles():
    articles_count = Article.objects.count()
    articles_to_seed = 23
    sys.stdout.write('[+] Seeding %d articles\n' %
                     (articles_to_seed - articles_count))
    for i in range(articles_count, articles_to_seed):
        title = faker.sentence()
        description = '\n'.join(faker.sentences(2))
        body = faker.text()
        user = AppUser.objects.order_by('?').first()
        tag = Tag.objects.get_random_tag()
        category = Category.objects.get_random_category()
        start_date = datetime.date(year=2017, month=1, day=1)
        random_date = faker.date_between(start_date=start_date, end_date='+4y')
        publish_on = random_date
        a = Article.objects.create(title=title,
                                   body=body,
                                   description=description,
                                   user=user,
                                   publish_on=publish_on)
        a.tags.add(tag)
        a.categories.add(category)
Beispiel #3
0
    # 生成 5 个分类
    faker_categories = [Category(name=faker.word()) for i in range(5)]
    session.add_all(faker_categories)

    # 生成20个标签
    faker_tags = [Tag(name=faker.word()) for i in range(20)]
    session.add_all(faker_tags)

    # 生成 100 篇文章
    for i in range(100):
        article = Article(
            # sentence() 生成一句话作为标题
            title=faker.sentence(),
            # 文章内容为随机生成的 10-20句话
            content=' '.join(faker.sentences(nb=random.randint(10, 20))),
            # 从生成的用户中随机取一个作为作者
            author=random.choice(faker_users),
            # 从生成的分类中随机取一个作为分类
            category=random.choice(faker_categories)
        )
        # 从生成的标签中随机取 2-5 个作为分类,注意 sample() 函数的用法
        for tag in random.sample(faker_tags, random.randint(2, 5)):
            article.tags.append(tag)
        session.add(article)
    session.commit()
<<<<<<< HEAD
=======


>>>>>>> dev
Beispiel #4
0
        Article.tags.get_through_model(), Category, Tag
    ])

    faker = Factory.create()

    faker_users = [
        User.create(
            username=faker.name(),
            password=faker.word(),
            email=faker.email(),
        ) for i in range(10)
    ]

    faker_categories = [Category.create(name=faker.word()) for i in range(5)]

    faker_tags = [Tag.create(name=faker.word()) for i in range(20)]

    for i in range(100):
        article = Article(title=faker.sentence(),
                          content=' '.join(
                              faker.sentences(nb=random.randint(10, 20))),
                          author=random.choice(faker_users),
                          category=random.choice(faker_categories))

        article.save()

        for tag in random.sample(faker_tags, random.randint(2, 5)):
            article.tags.add(tag)

        article.save()