Beispiel #1
0
class ServiceFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Service

    title = factory.LazyAttribute(lambda x: faker.sentence(nb_words=3))
    description = factory.LazyAttribute(lambda x: faker.sentence(nb_words=20))
    author = factory.Iterator(User.objects.all())
    category = factory.Iterator(Category.objects.all())
    date = factory.Sequence(
        lambda n: datetime.datetime.now() + datetime.timedelta(days=n))
    seeds_price = factory.Faker('pyint')
    photos = factory.RelatedFactory(ServicePhotoFactory, 'service')
Beispiel #2
0
def seed_comments():
    comments_count = db.session.query(func.count(Comment.id)).scalar()
    comments_to_seed = 31
    comments_to_seed -= comments_count
    sys.stdout.write('[+] Seeding %d comments\n' % comments_to_seed)
    comments = []

    user_ids = [user[0] for user in User.query.with_entities(User.id).all()]
    article_ids = [
        article[0] for article in Article.query.with_entities(Article.id)
    ]
    # equivalent:
    # user_ids = [user[0] for user in db.session.query(User.id).all()]
    # article_ids = [article[0] for article in db.session.query(Article.id).all()]

    for i in range(comments_count, comments_to_seed):
        user_id = random.choice(user_ids)
        article_id = random.choice(article_ids)
        comments.append(
            Comment(article_id=article_id,
                    user_id=user_id,
                    content=faker.sentence()))

    db.session.add_all(comments)
    db.session.commit()
Beispiel #3
0
def seed_products():
    products_count = Product.objects.count()
    products_to_seed = 35

    sys.stdout.write('[+] Seeding %d products\n' % (products_to_seed - products_count))

    dir = os.path.join(os.getcwd(), 'static', 'images', 'products')

    if not os.path.exists(dir):
        os.makedirs(dir)

    for i in range(products_count, products_to_seed):
        name = faker.sentence()
        # slug = faker.slug()
        # description = faker.paragraph(nb_sentences=3, variable_nb_sentences=10)
        description = faker.text()
        tags = Tag.objects.get_random_tag()
        categories = Category.objects.gent_random_category()
        price = round(random.uniform(150, 3000), 2)
        start_date = datetime.date(year=2016, month=1, day=1)
        random_date = faker.date_between(start_date=start_date, end_date='+4y')

        publish_on = random_date
        # publish_on = faker.date_time_between('-3y', '+1y')
        product = Product.objects.create(name=name, description=description, price=price,
                                         publish_on=publish_on, stock=faker.random_int(min=0, max=400))
        product.tags.add(tags)
        product.categories.add(categories)

        file_name = "".join(random.choice(ascii_lowercase) for i in range(16))
        file_path = os.path.join(dir, file_name)
        ProductImage.objects.create(file_name=file_name, original_name='adults.png',
                                    file_length=faker.random.randint(400, 10000),
                                    product=product,
                                    file_path=file_path.replace(os.getcwd(), '').replace('\\', '/'))
Beispiel #4
0
def issue_factory():
    return {
        "title": faker.sentence(),
        "body": faker.text(),
        "milestone": random.randint(1, 5),
        "labels": faker.words(),
        "assignees": [faker.user_name() for _ in range(3)]
    }
Beispiel #5
0
class SourceFactory(DjangoModelFactory):
    name = factory.Sequence(lambda x: '{}{}'.format(faker.name(), x))
    long_title = factory.Sequence(lambda x: '{}{}'.format(faker.sentence(), x))
    icon = factory.SelfAttribute('name')

    user = factory.SubFactory(ShareUserFactory, source=None)

    class Meta:
        model = models.Source
Beispiel #6
0
def seed_comments():
    comments_count = Comment.objects.count()
    comments_to_seed = 31
    sys.stdout.write('[+] Seeding %d comments\n' %
                     (comments_to_seed - comments_count))
    for i in range(comments_count, comments_to_seed):
        if Comment.objects.count() > 1 and faker.boolean():
            Comment.objects.create(
                article=Article.objects.order_by('?').first(),
                user=AppUser.objects.order_by('?').first(),
                content=faker.sentence())
Beispiel #7
0
def seed_replies():
    replies_count = Comment.objects.exclude(
        parent_comment__isnull=True).count()
    replies_to_seed = 33
    sys.stdout.write('[+] Seeding %d replies\n' %
                     (replies_to_seed - replies_count))
    for i in range(replies_count, replies_to_seed):
        Comment.objects.create(
            article=Article.objects.order_by('?').first(),
            user=AppUser.objects.order_by('?').first(),
            content=faker.sentence(),
            parent_comment=Comment.objects.order_by('?').first())
Beispiel #8
0
def seed_comments():
    comments_count = Comment.objects.count()
    comments_to_seed = 31
    sys.stdout.write('[+] Seeding %d comments\n' % (comments_to_seed - comments_count))

    if bool(random.getrandbits(1)):
        rating = faker.random_int(min=1, max=5)
    else:
        rating = None

    for i in range(comments_count, comments_to_seed):
        Comment.objects.create(content=faker.sentence(), user_id=AppUser.objects.order_by('?').only('id').first().id,
                               rating=rating,
                               product=Product.objects.order_by('?').only('id').first())
Beispiel #9
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 #10
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 #11
0
    # add_all 一次性添加多个对象
    session.add_all(faker_users)

    # 生成 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
=======
Beispiel #12
0
class SchoolFactory(DjangoModelFactory):
    class Meta:
        model = School

    name = lazy_attribute(lambda x: faker.sentence(nb_words=4))
Beispiel #13
0
def run_more():
    faker = Faker('zh-CN')
    driver = webdriver.Firefox()
    driver.set_page_load_timeout(60)
    segmentfaulturs = [
        'https://segmentfault.com/companies?page=' + str(i) for i in range(7)
    ]
    for segmentfaulturl in segmentfaulturls:
        driver.get(segmentfaulturl)
        time.sleep(2)
        html = driver.page_source
        dom_tree = etree.HTML(html)
        # 获取公司的链接
        company_urls = dom_tree.xpath(
            '//div[@class="row"]/div[@class="media"]/div[@class="col-md-9"]/div[@class="media-body"]/h4[@id="media-heading"]/a[1]/@href'
        )
        # 获取公司的名称
        company_names = dom_tree.xpath(
            '//div[@class="row"]/div[@class="media"]/div[@class="col-md-9"]/div[@class="media-body"]/h4[@id="media-heading"]/a/text()'
        )
        # 获取公司图片的url
        company_logos = dom_tree.xpath(
            '//div[@class="row"]/div[@class="media"]/div[@class="col-md-9"]/div[@class="pull-left"]/div[@class="img-warp"]/a[@class="company-logo"]/@style'
        )
        # 获取公司网站
        company_websites = dom_tree.xpath(
            '//div[@class="row"]/div[@class="media"]/div[@class="col-md-9"]/div[@class="media-body"]/a[@class="company-site"]/@href'
        )
        # 获取公司一句话介绍
        company_onewords = dom_tree.xpath(
            '//div[@class="row"]/div[@class="media"]/div[@class="col-md-9"]/div[@class="media-body"]/p[@class="company-desc"]/text()'
        )

        for company_name in company_names:
            print(company_name)

        for company_website in company_websites:
            print(company_website)

        for company_logo in company_logos:
            print(company_logo[company_log.rfind('(') +
                               1:company_logo.rfind(')') - 1])

        for company_oneword in company_onewords:
            print(company_oneword)

        for company_url in company_urls:
            company_url = 'https://segmentfault.com' + company_url
            print(company_url)

        # 获取当前窗口句柄
        now_handle = driver.current_window_handle

        i = 0
        for company_url in company_urls:
            name = company_names[i]
            email = faker.email()
            password = '******'
            logo_img = company_logos[i][company_logos[i].rfind('(') +
                                        1:company_logos[i].rfind(')') - 1]
            if User.query.filter_by(name=name).first():
                # 如果企业存在就不存入数据库
                i += 1
                print('重复')
                continue
            # 把用户加入数据库
            user = User(name=name,
                        email=email,
                        password=password,
                        logo_img=logo_img,
                        role=20)
            db.session.add(user)
            user = User.query.filter_by(name=name).first()
            oneword = faker.sentence(nb_words=25)

            print(name)
            print(logo_img)
            print(website)
            print(oneword)

            description = faker.sentence(nb_words=50)
            # 把公司信息加入数据库
            company = Company(user=user,
                              website=website,
                              oneword=oneword,
                              description=description)
            db.session.add(company)
            # 给公司家入职位信息
            num = User.query.filter_by(name=name).first().id
            ran = random.randint(10, 100)
            for j in range(random.randint(10, 20)):
                job = Job(name=faker.job(),
                          wage=str(ran) + 'K - ' + str(ran + 10) + 'K',
                          location=faker.province(),
                          company=Company.query.filter_by(user_id=num).first(),
                          description=faker.sentence(nb_words=25),
                          requirement=faker.sentence(nb_words=20))
                db.session.add(job)
                i += 1
    driver.close()
    # 向数据库提交
    try:
        db.session.commit()
    except Exception as e:
        print(e)
        db.session.rollback()
Beispiel #14
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()
Beispiel #15
0
            uid = str(uuid.uuid4())
        lv = random.randint(1, 10)
        if lv <= 4:
            level = "debug"
        elif lv <= 8:
            level = "info"
        elif lv <= 9:
            level = "warning"
        else:
            level = "error"

        log_item = {
            "@timestamp":
            str(datetime.datetime.utcnow() + datetime.timedelta(hours=8)),
            "message":
            faker.sentence(nb_words=9),
            "muid":
            uid,
            "level":
            level
        }
        log = json.dumps(log_item)
        print(log)

        fl = random.randint(1, 4)
        if fl == 1:
            filename = "/usr/log/xmppclient-a79874f161b7.log"
        elif fl == 2:
            filename = "/usr/log/querier-a79874f161b7.log"
        elif fl == 3:
            filename = "/usr/log/callback-a79874f161b7.log"
Beispiel #16
0
# -*- coding: utf-8 -*-
# @Time    : 2018/12/3 3:19 PM
# @Author  : Jax.Li
# @FileName: loggenerater.py
# @Software: PyCharm
# @Blog    :https://blog.jaxli.com

import faker
import datetime
import time

if __name__ == '__main__':
    faker = faker.Faker()
    # while True:
    for index in range(10):
        log = "{}    INFO    {}\n".format(str(datetime.datetime.now()), faker.sentence(nb_words=9))
        print(log)
        time.sleep(2)
        with open("/usr/log/python.log", "a") as file:
            file.write(log)