# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import datetime
import os

from pyramid.config import Configurator
from pyramid.httpexceptions import HTTPFound
from pyramid.view import view_config
import sqlalchemy as sa
from sqlalchemy.exc import DBAPIError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from waitress import serve
from zope.sqlalchemy import ZopeTransactionExtension


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

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(127), nullable=False)
    text = sa.Column(sa.UnicodeText, nullable=False)
    created = sa.Column(
        sa.DateTime, nullable=False, default=datetime.datetime.utcnow
    )

    def __repr__(self):
        return "Entry: {title} created at {date}".format(
            title=self.title, date=self.created
        )

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

    @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


def init_db():
    engine = sa.create_engine(DATABASE_URL, echo=False)
    Base.metadata.create_all(engine)


@view_config(route_name='home', renderer='templates/list.jinja2')
def list_view(request):
    entries = Entry.all()
    return {'entries': entries}


@view_config(route_name='add', request_method='POST')
def add_entry(request):
    title = request.params.get('title')
    text = request.params.get('text')
    Entry.write(title=title, text=text)
    return HTTPFound(request.route_url('home'))


@view_config(context=DBAPIError)
def db_exception(context, request):
    from pyramid.response import Response
    response = Response(context.message)
    response.status_int = 500
    return response


def main():
    """Create a configured wsgi app"""
    settings = {}
    debug = os.environ.get('DEBUG', True)
    settings['reload_all'] = debug
    settings['debug_all'] = debug
    settings['auth.username'] = os.environ.get('AUTH_USERNAME', 'admin')
    settings['auth.password'] = os.environ.get('AUTH_PASSWORD', 'secret')
    if not os.environ.get('TESTING', False):
        # only bind the session if we are not testing
        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('add', '/add')
    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)

@@ -84,16 +84,79 @@
 ort os%0A%0A
+from pyramid.authentication import AuthTktAuthenticationPolicy%0A
 from pyr
@@ -2856,16 +2856,391 @@
 engine)%0A
+    # add a secret value for auth tkt signing%0A    auth_secret = os.environ.get('JOURNAL_AUTH_SECRET', 'itsaseekrit')%0A    # and add a new value to the constructor for our Configurator:%0A    config = Configurator(%0A        settings=settings,%0A        authentication_policy=AuthTktAuthenticationPolicy(%0A            secret=auth_secret,%0A            hashalg='sha512'%0A        ),%0A    )%0A
     # co

Add authentication config