# -*- 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)



[((221, 277), 'flask.Blueprint', 'Blueprint', (['"""news"""', '__name__'], {'template_folder': '"""templates"""'}), "('news', __name__, template_folder='templates')\n", (230, 277), False, 'from flask import Blueprint, redirect\n'), ((315, 357), 'flaskbb.utils.helpers.render_template', 'render_template', (['"""navigation_snippet.html"""'], {}), "('navigation_snippet.html')\n", (330, 357), False, 'from flaskbb.utils.helpers import render_template\n'), ((739, 777), 'flaskbb.utils.helpers.render_template', 'render_template', (['"""add.html"""'], {'form': 'form'}), "('add.html', form=form)\n", (754, 777), False, 'from flaskbb.utils.helpers import render_template\n'), ((1087, 1128), 'flaskbb.utils.helpers.render_template', 'render_template', (['"""delete.html"""'], {'form': 'form'}), "('delete.html', form=form)\n", (1102, 1128), False, 'from flaskbb.utils.helpers import render_template\n'), ((649, 666), 'flaskbb.extensions.db.session.add', 'db.session.add', (['p'], {}), '(p)\n', (663, 666), False, 'from flaskbb.extensions import db\n'), ((675, 694), 'flaskbb.extensions.db.session.commit', 'db.session.commit', ([], {}), '()\n', (692, 694), False, 'from flaskbb.extensions import db\n'), ((710, 727), 'flask.redirect', 'redirect', (['"""/news"""'], {}), "('/news')\n", (718, 727), False, 'from flask import Blueprint, redirect\n'), ((986, 1006), 'flaskbb.extensions.db.session.delete', 'db.session.delete', (['p'], {}), '(p)\n', (1003, 1006), False, 'from flaskbb.extensions import db\n'), ((1015, 1034), 'flaskbb.extensions.db.session.commit', 'db.session.commit', ([], {}), '()\n', (1032, 1034), False, 'from flaskbb.extensions import db\n'), ((1058, 1075), 'flask.redirect', 'redirect', (['"""/news"""'], {}), "('/news')\n", (1066, 1075), False, 'from flask import Blueprint, redirect\n')]