# -*- coding: utf-8 -*-
from flask import Blueprint, redirect
from flaskbb.utils.helpers import render_template

from .forms import AddForm, DeleteForm
from .models import MyPost
from flaskbb.extensions import db

news = Blueprint("news", __name__, template_folder="templates")


def inject_news_link():
    return render_template("navigation_snippet.html")


@news.route("/")
def index():
    return render_template("index.html", newsposts = MyPost.query.all())


@news.route('/add', methods=['GET', 'POST'])
def add():
    form = AddForm()
    if form.validate_on_submit():
        p = MyPost(name = form.name.data, text = form.text.data)
        db.session.add(p)
        db.session.commit()
        return redirect('/news')
    return render_template('add.html', form=form)

@news.route('/delete', methods=['GET', 'POST'])
def delete():
    form = DeleteForm()
    if form.validate_on_submit():        
        p = MyPost.query.filter(MyPost.name == form.name.data).first()
        db.session.delete(p)
        db.session.commit()        
        return redirect('/news')
    return render_template('delete.html', form=form)



[((9, 7, 9, 63), 'flask.Blueprint', 'Blueprint', (), '', False, 'from flask import Blueprint, redirect\n'), ((13, 11, 13, 53), 'flaskbb.utils.helpers.render_template', 'render_template', ({(13, 27, 13, 52): '"""navigation_snippet.html"""'}, {}), "('navigation_snippet.html')", False, 'from flaskbb.utils.helpers import render_template\n'), ((29, 11, 29, 49), 'flaskbb.utils.helpers.render_template', 'render_template', (), '', False, 'from flaskbb.utils.helpers import render_template\n'), ((39, 11, 39, 52), 'flaskbb.utils.helpers.render_template', 'render_template', (), '', False, 'from flaskbb.utils.helpers import render_template\n'), ((26, 8, 26, 25), 'flaskbb.extensions.db.session.add', 'db.session.add', ({(26, 23, 26, 24): 'p'}, {}), '(p)', False, 'from flaskbb.extensions import db\n'), ((27, 8, 27, 27), 'flaskbb.extensions.db.session.commit', 'db.session.commit', ({}, {}), '()', False, 'from flaskbb.extensions import db\n'), ((28, 15, 28, 32), 'flask.redirect', 'redirect', ({(28, 24, 28, 31): '"""/news"""'}, {}), "('/news')", False, 'from flask import Blueprint, redirect\n'), ((36, 8, 36, 28), 'flaskbb.extensions.db.session.delete', 'db.session.delete', ({(36, 26, 36, 27): 'p'}, {}), '(p)', False, 'from flaskbb.extensions import db\n'), ((37, 8, 37, 27), 'flaskbb.extensions.db.session.commit', 'db.session.commit', ({}, {}), '()', False, 'from flaskbb.extensions import db\n'), ((38, 15, 38, 32), 'flask.redirect', 'redirect', ({(38, 24, 38, 31): '"""/news"""'}, {}), "('/news')", False, 'from flask import Blueprint, redirect\n')]