def write(): " give them a rich text editor for the site " form = ArticleForm() if form.validate_on_submit(): news = Article(title=form.title.data, subtitle=form.subtitle.data, body=form.body.data) db.session.add(news) return {'form': form}
def seed(count): " make some fake articles " import forgery_py as f seed() for i in range(count): a = Article(title=f.lorem_ipsum.title(), subtitle=f.lorem_ipsum.title(), body=f.lorem_ipsum.paragraph(html=True, sentences_quantity=100), author=get_random(User)) db.session.add(a) db.session.commit() click.echo('added {} articles to the database'.format( Article.query.count()))
def seed(count): """ add some fake articles to the database """ f = Faker() if User.query.count() == 0: raise Exception('there are no users in the database') for i in range(count): a = Article(title=f.sentence(), subtitle=f.sentence(), body='\n'.join(f.paragraphs(randint(5, 20))), views=randint(0, 100000), author=get_random(User)) db.session.add(a) db.session.commit() click.echo('added {} articles to the database'.format( Article.query.count()))
def home(): articles = Article.query.order_by(Article.popularity().desc()).all() top = articles[:4] return {'articles': articles, 'top_stories': top}
def most_popular(): """ returns the most popular articles """ sorted_articles = Article.query.order_by(Article.popularity()).all() return jsonify(sorted_articles)
def write(): " give them a rich text editor for the site " form = ArticleForm() if form.validate_on_submit(): news = Article.create(form.data) return {'form': form}
def view_all_articles(): sorted_articles = Article.query.order_by(Article.popularity().desc()).all() articles = [x.read() for x in sorted_articles] return jsonify({'top': articles})