Beispiel #1
0
    def add_post(author, title, description, slug):
        """Add New Post"""
        p = Post()
        p.author = User.objects.get(username=author)
        p.title = title
        p.slug = p.set_slug(title)
        p.description = description
        p.category = [Category.objects.get(slug=slug)]
        p.save()

        print('New Post added.')
Beispiel #2
0
 def __post():
     post = Post(title=self.text.title(),
                 summary=text.text(3),
                 body=text.text(6),
                 author=user)
     category = Category(name=text.word())
     category.save()
     post.category = category
     tag = Tag(name=text.word())
     tag.save()
     post.tags.append(tag)
     post.save()
     return post
Beispiel #3
0
def generate_post(count=390, locale="en"):
    text = Text(locale)
    box_summary_box = [1, 2, 3]
    box_article_box = [7, 8, 9]
    author_count = int(User.query.count() / 4)
    tag_count = int(Tag.query.count() / 4)
    authors = list()
    tags = list()

    for _ in range(count):
        timestamp = datetime(year=randint(1995, 2017),
                             month=randint(1, 12),
                             day=randint(1, 28),
                             hour=randint(0, 23),
                             minute=randint(0, 59),
                             second=randint(0, 59),
                             microsecond=randint(0, 999999))
        if _ % author_count == 0:
            authors = User.query.order_by(
                sql_expression_rand()).limit(author_count).all()
        if _ % tag_count == 0:
            tags = Tag.query.order_by(
                sql_expression_rand()).limit(tag_count).all()
        new = Post(author=authors[_ % author_count],
                   title=text.title(),
                   summary=text.text(choice(box_summary_box)),
                   body=text.text(choice(box_article_box)),
                   timestamp=timestamp,
                   last_edit=timestamp,
                   count=randint(0, 10000))
        post_tags = [tag for no, tag in enumerate(tags) if no < _ % 4]
        new.tags.extend(post_tags)
        if _ % 3 == 0:
            category = Category.query.order_by(sql_expression_rand()).first()
            new.category = category
        db.session.add(new)
    db.session.commit()