# -*- 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, 'flask.Blueprint', 'Blueprint', False, 'from flask import Blueprint, redirect\n'), (13, 'flaskbb.utils.helpers.render_template', 'render_template', False, 'from flaskbb.utils.helpers import render_template\n'), (29, 'flaskbb.utils.helpers.render_template', 'render_template', False, 'from flaskbb.utils.helpers import render_template\n'), (39, 'flaskbb.utils.helpers.render_template', 'render_template', False, 'from flaskbb.utils.helpers import render_template\n'), (26, 'flaskbb.extensions.db.session.add', 'db.session.add', False, 'from flaskbb.extensions import db\n'), (27, 'flaskbb.extensions.db.session.commit', 'db.session.commit', False, 'from flaskbb.extensions import db\n'), (28, 'flask.redirect', 'redirect', False, 'from flask import Blueprint, redirect\n'), (36, 'flaskbb.extensions.db.session.delete', 'db.session.delete', False, 'from flaskbb.extensions import db\n'), (37, 'flaskbb.extensions.db.session.commit', 'db.session.commit', False, 'from flaskbb.extensions import db\n'), (38, 'flask.redirect', 'redirect', False, 'from flask import Blueprint, redirect\n')]