# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from sqlalchemy.orm import scoped_session, sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
import os
from pyramid.config import Configurator
from pyramid.view import view_config
from waitress import serve
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
import datetime
from sqlalchemy import create_engine

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()


class Entry(Base):
    __tablename__ = 'entries'

    id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
    title = sa.Column(sa.Unicode(128), nullable=False)
    text = sa.Column(sa.UnicodeText, nullable=False)
    created = sa.Column(sa.DateTime, nullable=False,
                        default=datetime.datetime.utcnow)

    @classmethod
    def write(cls, title=None, text=None, session=None):
        if session is None:
            session = DBSession
        instance = cls(title=title, text=text)
        session.add(instance)
        return instance

    @classmethod
    def all(cls, session=None):
        if session is None:
            session = DBSession
        return session.query(cls).order_by(cls.created.desc()).all()

    def __repr__(self):
        return "<Entry(title='%s', created='%s')>" % (
            self.title, self.created)

DATABASE_URL = os.environ.get(
    'DATABASE_URL',
    'postgresql://meslater@localhost:5432/learning-journal'
)


def init_db():
    engine = create_engine(DATABASE_URL)
    Base.metadata.create_all(engine)


@view_config(route_name='home', renderer='templates/test.jinja2')
def home(request):
    # this sets a breakpoint.  Our shell will pop into a pbd prompt
    # import pdb; pdb.set_trace()
    return {'one': 'two', 'stuff': ['a', 'b', 'c']}


@view_config(route_name='other', renderer='string')
def other(request):
    # import pdb; pdb.set_trace()
    return request.matchdict


def main():
    """Create a configured wsgi app"""
    settings = {}
    debug = os.environ.get('DEBUG', True)
    settings['reload_all'] = debug
    settings['debug_all'] = debug
    if not os.environ.get('TESTING', False):
        engine = sa.create_engine(DATABASE_URL)
        DBSession.configure(bind=engine)
    # configuration setup
    config = Configurator(
        settings=settings
        )
    config.include('pyramid_tm')
    config.include('pyramid_jinja2')
    config.add_route('home', '/')
    # config.add_route('other', '/other/{special_val}')
    config.scan()
    app = config.make_wsgi_app()
    return app

if __name__ == '__main__':
    app = main()
    port = os.environ.get('PORT', 5000)
    serve(app, host='0.0.0.0', port=port)

@@ -1655,18 +1655,18 @@
 mplates/
-te
+li
 st.jinja
@@ -1673,20 +1673,25 @@
 2')%0Adef 
-home
+list_view
 (request
@@ -1701,156 +1701,60 @@
     
-# this sets a breakpoint.  Our shell will pop into a pbd prompt%0A    # import pdb; pdb.set_trace()%0A    return %7B'one': 'two', 'stuff': %5B'a', 'b', 'c'%5D
+entries = Entry.all()%0A    return %7B'entries': entries
 %7D%0A%0A%0A
@@ -1874,25 +1874,21 @@
 urn 
-request.matchdict
+%22Hello World%22
 %0A%0A%0Ad
@@ -2392,26 +2392,24 @@
 e', '/')%0A   
- #
  config.add_

connect view to list.jinja2