Exemple #1
0
def make_users(usr_qty):
    logger.info('Making users')
    users = []
    for i in range(usr_qty):
        name = lorem.get_word(2)
        email = lorem.get_word(2)
        email = email.split(' ')
        email = '@'.join(email)
        email = email + '.com'
        pasw = str(uuid.uuid4())
        user = User(username=name, email=email, password=pasw)
        users.append(user)
    User.objects.bulk_create(users)
    logger.info('Making users complete')
Exemple #2
0
def generate_post() -> dict:
    """
    Generate random post
    @return: dict with title, short_description, image,
             full_description, user, posted
    """
    dict_post = {
        'title': '',
        'short_description': '',
        'image': '',
        'full_description': '',
        'user': 1,
        'posted': False
    }
    fake = Faker(['en_US'])
    dict_post['title'] = get_word(count=1)
    dict_post['short_description'] = get_sentence(count=1,
                                                  word_range=(4, 8),
                                                  sep=' ')
    dict_post['image'] = fake.image_url()
    dict_post['full_description'] = get_paragraph(count=3,
                                                  comma=(0, 2),
                                                  word_range=(4, 8),
                                                  sentence_range=(5, 10),
                                                  sep=os.linesep)
    dict_post['user'] = random.randint(1, 2)
    dict_post['posted'] = random.choice([True, False])
    return dict_post
Exemple #3
0
    def setup_market_db(cls):
        """ create bid for a deal and accept """
        market = Market.objects.create(
            title=get_word()[0:20],
            location=cls.location,
        )
        market.users.add(cls.demo)
        market.users.add(cls.demo1)
        market.users.add(cls.random_object(User))
        market.save()

        market = Market.objects.create(
            title=get_word()[0:20],
            location=cls.location,
        )
        market.users.add(cls.demo)
        market.users.add(cls.demo1)
        market.users.add(cls.random_object(User))
        market.users.add(cls.random_object(User))
        market.save()

        market = Market.objects.create(
            title=get_word()[0:20],
            location=cls.location,
        )
        market.users.add(cls.demo)
        market.users.add(cls.demo1)
        market.users.add(cls.random_object(User))
        market.users.add(cls.random_object(User))
        market.users.add(cls.random_object(User))
        market.save()
        cls.market = market

        market = Market.objects.create(
            title=get_word()[0:20],
            location=cls.location,
        )
        market.users.add(cls.random_object(User))
        market.users.add(cls.random_object(User))
        market.users.add(cls.random_object(User))
        market.save()
Exemple #4
0
    def test_get_word(self):
        """Test :func:`lorem.get_word`."""
        with self.mock_pool:
            with self.mock_randint:
                word = lorem.get_word(count=3)
        self.assertEqual(word, 'lorem ipsum lorem')

        with self.mock_pool:
            with self.mock_randint:
                word = lorem.get_word(count=(3, 5))
        self.assertEqual(word, 'lorem ipsum lorem')

        with self.mock_pool:
            with self.mock_randint:
                word = lorem.get_word(count=3, func='capitalize')
        self.assertEqual(word, 'Lorem Ipsum Lorem')

        with self.mock_pool:
            with self.mock_randint:
                word = lorem.get_word(count=3, func=lambda s: s.upper())
        self.assertEqual(word, 'LOREM IPSUM LOREM')
Exemple #5
0
def crud(conn, op, delay, wc=MAX_WORDS_PER_ITER):
    """
    Generates 'wc' number of random words from lorem-ipsum and
    performs CRUD operation specified by 'op' for each word with default delay after operation
    """
    words = lorem.get_word(count=wc, sep=',',
                           func=lambda x: x.upper()).split(',')
    with conn.cursor() as curs:
        for w in words:
            if op == dbops['INSERT']:
                sql = 'INSERT INTO pgfailover.benchtab (ts, status) VALUES (CURRENT_TIMESTAMP, \'%s\');'
            elif op == dbops['READ']:
                sql = 'SELECT ts, status FROM pgfailover.benchtab WHERE status like \'%s\''
            curs.execute(sql % w)
            conn.commit()
            rec = '{:s} {:s}'
            log.debug(rec.format(opsdb[op], w))
            if delay > 0:
                time.sleep(delay)
Exemple #6
0
    def setup_location_db(cls):
        """ set up location db """
        combis = [(cls.demo, 'Demos Treffpunkt'), (cls.demo1, 'Demo1 Zuhause')]
        for user, title in combis:
            cls.location = Location.objects.create(
                user=user,
                title=title,
                lon=randint(-180, 180),
                lat=randint(-90, 90),
                description=cls.sentences_string(),
            )

        for i in range(cls.LOCATION_COUNT):
            Location.objects.create(
                title=get_word()[0:20],
                user=cls.random_object(User),
                lon=randint(-180, 180),
                lat=randint(-90, 90),
                description=cls.sentences_string(),
            )
            cls.print_steps(i, 100, 'locations')
Exemple #7
0
def make_posts(post_qty):
    '''
    post_qty: int
    returns: Created model instances
    https://docs.djangoproject.com/en/3.1/ref/models/querysets/#bulk-create
    Using bulk_create. Had to include slug, created and modified to make work.
    bulk_create doesn't call models save method or call pre and post save signals. Duh!
    '''
    logger.info('Making posts')
    new_posts = []  # container for new posts
    for i in range(post_qty):
        title = lorem.get_sentence(1)
        slug = slugify(title)
        num = str(uuid.uuid4().hex)
        unique_slug = f'{slug}-{num}'
        overview = lorem.get_sentence(3)
        created = timezone.now()
        modified = timezone.now()
        content = lorem.get_paragraph(10)
        author = lorem.get_word()
        featured = random.getrandbits(1)
        new_post = Post(title=title,
                        slug=unique_slug,
                        overview=overview,
                        date=created,
                        modified=modified,
                        content=content,
                        author=author,
                        featured=featured)
        new_posts.append(new_post)  # add new posts to container
    Post.objects.bulk_create(new_posts)  # profit
    search_vectors = SearchVector('title', weight='A') + \
                    SearchVector('overview', weight='B') + \
                    SearchVector('content', weight='C')
    Post.objects.update(search_vector=search_vectors)
    logger.info('Making posts complete')
Exemple #8
0
    def prepare(
        self,
        file_path: Path = Path(''),
        content_variant: str = '',
        font_family: str = '',
        font_size: str = '',
        font_style: str = '',
        font_weight: str = '',
        text_decoration_line: str = '',
        font_color: str = '',
        background_color: str = '',
        layout: Layout = Layout.center,
        content_source: str = '',
    ):

        style: str = ''
        if len(font_family) > 0: style += 'font-family: ' + font_family + '; '
        if len(font_size) > 0: style += 'font-size: ' + font_size + '; '
        if len(font_style) > 0: style += 'font-style: ' + font_style + '; '
        if len(font_weight) > 0: style += 'font-weight: ' + font_weight + '; '
        if len(text_decoration_line) > 0:
            style += 'text-decoration-line: ' + text_decoration_line + '; '
        if len(font_color) > 0: style += 'color: ' + font_color + '; '
        if len(background_color) > 0:
            style += 'background: ' + background_color + '; '

        if font_color[1:] == background_color[1:]:
            return

        # Generate content
        words: [str] = ['']
        sentences: [str] = ['']
        paragraphs: [str] = ['']
        usernames: [str] = ['']
        background_images: [str] = []

        # Get words, sentences, paragraphs and usernames
        if content_variant != 'images_only':
            if content_source == 'lorem':
                for _ in range(10):
                    words.append(lorem.get_word())
                    sentences.append(lorem.get_sentence())
                    paragraphs.append(lorem.get_paragraph())
                    usernames.append(self.gen_username())

            elif content_source == 'bible':
                for _ in range(10):
                    words.append(random.choice(self.word_list))
                    sentences.append(random.choice(self.bible_list))
                    temp_paragraph = ''
                    for _ in range(random.randint(2, 5)):
                        temp_paragraph += random.choice(self.bible_list) + ' '
                    paragraphs.append(temp_paragraph)
                    usernames.append(self.gen_username())

            elif content_source == 'random':
                for _ in range(10):
                    words.append(self.gen_random_word())
                    sentences.append(self.gen_random_sentence())
                    paragraphs.append(self.gen_random_paragraph())
                    usernames.append(self.gen_username())

            # Remove firsts
            words.pop(0)
            sentences.pop(0)
            paragraphs.pop(0)
            usernames.pop(0)

        else:
            words = ['', '', '', '', '', '', '', '', '', '']
            sentences = ['', '', '', '', '', '', '', '', '', '']
            paragraphs = ['', '', '', '', '', '', '', '', '', '']
            usernames = ['', '', '', '', '', '', '', '', '', '']

        # Get images
        if content_variant == 'with_images' or content_variant == 'images_only':
            background_images = self.get_images()

        # Generate and save document
        self.generate_file(path=file_path,
                           words=words,
                           sentences=sentences,
                           paragraphs=paragraphs,
                           usernames=usernames,
                           background_images=background_images,
                           style=style,
                           layout=layout)
def make_topic():
    return Topic(title=get_word())
Exemple #10
0
def make_thread():
    return Thread(title=get_word(), is_vegan=randint(0, 1) == 1)