Example #1
0
def article(article_id):
    article = Article.query.filter_by(id=article_id).first()
    article_form = ArticleForm()
    if article is not None:
        if request.method == 'GET':
            return render_template('article.html', article=article, article_form=article_form)
        else:
            if logged_in() and admin():
                if request.method == 'PUT':
                    if article_form.validate_on_submit():
                        article.title = request.form['title']
                        article.body = request.form['body']
                        db.session.commit()
                        flash('Article modified successfully', 'success')
                        return redirect('/news/article' + str(article_id))
                    else:
                        flash_errors(article_form)
                        return render_template('article.html', article=article, article_form=article_form)
                elif request.method == 'DELETE':
                    db.session.delete(article)
                    db.session.commit()
                    flash('Article deleted successfully', 'success')
                    return redirect('/news')
    else:
        return render_template('404.html')
Example #2
0
def news_index(page=1):
    articles_query = Article.query.order_by('id desc')
    articles_paginate = articles_query.paginate(page, 5, False)
    articles = articles_paginate.items
    article_form = ArticleForm()
    if request.method == 'GET':
        if articles_paginate.pages != 0 and articles_paginate.pages < page:
            return redirect('/news')
        return render_template('news.html', article_form=article_form, articles=articles)
    else:
        if logged_in() and admin():
            if article_form.validate_on_submit():
                user = current_user()
                new_article = Article(request.form['title'], request.form['body'])
                new_article.user_id = user.id
                user.articles.append(new_article)
                db.session.add(new_article)
                db.session.commit()
                flash('Article posted successfully', 'success')
                return redirect('/news')
            else:
                flash_errors(article_form)
                return render_template('news.html', article_form=article_form, articles=articles)