Ejemplo n.º 1
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('\\', '/'))
Ejemplo n.º 2
0
    def handle(self, *args, **options):

        for x in range(0, 50):
            title = " ".join(faker.words(random.randint(2, 4)))
            views = random.randint(1, 100)
            reviewed = random.randint(0, 1) == 1
            author_name = random.choice(author_names)
            published = faker.date_between(start_date='-7y', end_date='now')
            category_name = random.choice(categories)

            #create the author instance & save to db
            author = Author.objects.get_or_create(name=author_name)

            #create the journal instance
            journal = Journal(title=title,
                              author=Author.objects.get(name=author_name),
                              published=published,
                              views=views,
                              reviewed=reviewed)

            #create the journal instance and save
            journal.save()

            category = Category.objects.get_or_create(name=category_name)

            journal.categories.add(Category.objects.get(name=category_name))

        self.stdout.write(self.style.SUCCESS('Data imported sucessfully'))
Ejemplo n.º 3
0
 def active_from(self):
     faker = Faker._get_faker()
     if self.in_past:
         faked_date = faker.optional_value('date_between',
                                           start_date='-365d',
                                           end_date='-200d')
     elif self.in_future:
         faked_date = faker.date_between(start_date='+2d', end_date='+199d')
     elif self.in_present:
         faked_date = faker.optional_value('date_between',
                                           start_date='-200d',
                                           end_date='-2d')
     else:
         faked_date = faker.optional_value('date_object',
                                           end_datetime='+5y')
     return faked_date
Ejemplo n.º 4
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()
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 def active_until(self):
     faker = Faker._get_faker()
     if self.in_past:
         faked_date = faker.date_between(start_date='-199d', end_date='-2d')
     elif self.in_future:
         faked_date = faker.optional_value('date_between',
                                           start_date='+200d',
                                           end_date='+365d')
     elif self.in_present:
         faked_date = faker.optional_value('date_between',
                                           start_date='+2d',
                                           end_date='+200d')
     else:
         if self.active_from:
             start, end = self.active_from, self.active_from + timedelta(
                 days=365)
             faked_date = faker.optional_value('date_between_dates',
                                               date_start=start,
                                               date_end=end)
         else:
             faked_date = faker.optional_value('date_object',
                                               end_datetime='+5y')
     return faked_date
def birth_date_and_start_date():
    # Link birth date and start date
    start_date = faker.date_between(start_date='-20y', end_date='now')
    delta = datetime.timedelta(days=365 * random.randint(18, 40))  # 18y-40y
    birth_date = start_date - delta
    return {'birth_date': birth_date, 'start_date': start_date}