# -*- coding: utf-8 -*-
from contextlib import closing
import datetime
from flask import Flask
from flask import g
from flask import render_template
import os
import psycopg2


DB_SCHEMA = """
DROP TABLE IF EXISTS entries;
CREATE TABLE entries (
    id SERIAL PRIMARY KEY,
    title VARCHAR (127) NOT NULL,
    text TEXT NOT NULL,
    created TIMESTAMP NOT NULL
)
"""
DB_ENTRY_INSERT = """
INSERT INTO entries (title, text, created) VALUES (%s, %s, %s)
"""
DB_ENTRIES_LIST = """
SELECT id, title, text, created FROM entries ORDER BY created DESC
"""


app = Flask(__name__)

app.config['DATABASE'] = os.environ.get(
    'DATABASE_URL', 'dbname=learning_journal user=cewing'
)


def connect_db():
    """Return a connection to the configured database"""
    return psycopg2.connect(app.config['DATABASE'])


def init_db():
    """Initialize the database using DB_SCHEMA

    WARNING: executing this function will drop existing tables.
    """
    with closing(connect_db()) as db:
        db.cursor().execute(DB_SCHEMA)
        db.commit()


def get_database_connection():
    db = getattr(g, 'db', None)
    if db is None:
        g.db = db = connect_db()
    return db


@app.teardown_request
def teardown_request(exception):
    db = getattr(g, 'db', None)
    if db is not None:
        if exception:
            # if there was a problem, rollback any existing transaction
            db.rollback()
        else:
            # otherwise, commit
            db.commit()
        db.close()


def write_entry(title, text):
    """write an entry with title and text to the database"""
    if not title or not text:
        raise ValueError("Title and text required for writing an entry")
    con = get_database_connection()
    cur = con.cursor()
    now = datetime.datetime.utcnow()
    cur.execute(DB_ENTRY_INSERT, [title, text, now])


def get_all_entries():
    """return a list of all entries as dicts"""
    con = get_database_connection()
    cur = con.cursor()
    cur.execute(DB_ENTRIES_LIST)
    keys = ('id', 'title', 'text', 'created')
    return [dict(zip(keys, row)) for row in cur.fetchall()]


@app.route('/')
def show_entries():
    entries = get_all_entries()
    return render_template('list_entries.html', entries=entries)


if __name__ == '__main__':
    app.run(debug=True)

@@ -110,43 +110,146 @@
 ort 
-g%0Afrom flask import render_template
+abort%0Afrom flask import g%0Afrom flask import redirect%0Afrom flask import render_template%0Afrom flask import request%0Afrom flask import url_for
 %0Aimp
@@ -2340,16 +2340,300 @@
 ries)%0A%0A%0A
+@app.route('/add', methods=%5B'POST'%5D)%0Adef add_entry():%0A    try:%0A        write_entry(request.form%5B'title'%5D, request.form%5B'text'%5D)%0A    except psycopg2.Error:%0A        # this will catch any errors generated by the database%0A        abort(500)%0A    return redirect(url_for('show_entries'))%0A%0A%0A
 if __nam

implement the view that adds a new entry