from noter import app, db, Entry
from flask import url_for, redirect, request, render_template, \
    abort, flash, session
from markdown2 import Markdown

@app.route('/')
def show_entries():
    entries = Entry.query.order_by(Entry.id)
    return render_template('show_entries.html', entries = entries)

@app.route('/add', methods=['POST'])
def add_entry():
    if not session.get('logged_in'):
        abort(401)
    newEntry = Entry(request.form['title'], request.form['text'])
    db.session.add(newEntry)
    db.session.commit()
    return redirect(url_for('show_entries'))

@app.route('/edit/<int:id>', methods=['GET', 'POST'])
def edit_entry(id):
    return show_entries();

@app.route('/delete/<int:id>', methods=['GET'])
def confirm_delete_entry(id):
    if not session.get('logged_in'):
        abort(403)
    entry = Entry.query.filter_by(id=id).first()
    return render_template('delete.html', entry = entry);

@app.route('/delete/<int:id>', methods=['POST'])
def delete_entry(id):
    if not session.get('logged_in'):
        abort(403)
    entry = Entry.query.filter_by(id=id).first()
    db.session.delete(entry)
    db.session.commit()
    return show_entries();

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if (request.form['username'] != app.config['USERNAME']) or \
           (request.form['password'] != app.config['PASSWORD']):
            error = 'Wrong username/password combination'
        else:
            session['logged_in'] = True
            flash('You were logged in.')
            return redirect(url_for('show_entries'))
    return render_template('login.html', error=error)

@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out.')
    return redirect(url_for('show_entries'))

@@ -199,16 +199,31 @@
 tries = 
+entries_render(
 Entry.qu
@@ -244,16 +244,17 @@
 ntry.id)
+)
 %0A    ret
@@ -829,32 +829,47 @@
 03)%0A    entry = 
+entries_render(
 Entry.query.filt
@@ -884,24 +884,25 @@
 =id).first()
+)
 %0A    return 
@@ -1840,28 +1840,238 @@
 ct(url_for('show_entries'))%0A
+%0Adef entries_render(entries):%0A    try:%0A        for e in entries:%0A            e.body = Markdown().convert(e.body)%0A    except TypeError:%0A        entries.body = Markdown().convert(entries.body)%0A%0A    return entries

reimplement markdown