from sqlalchemy import Column, String, Boolean, Integer, ForeignKey
from pyramid.config import Configurator
from sqlalchemy.orm import relationship, sessionmaker, scoped_session
from pyramid.session import SignedCookieSessionFactory
from pyramid.security import Allow, forget, remember
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.httpexceptions import HTTPFound
from pyramid_sacrud.security import permissions, PYRAMID_SACRUD_HOME
from sqlalchemy.ext.declarative import declarative_base

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


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String(30))

    def __repr__(self):
        return self.name


class Group(Base):
    __tablename__ = 'groups'
    id = Column(Integer, primary_key=True)
    name = Column(String(30))

    def __repr__(self):
        return self.name


class Good(Base):
    __tablename__ = 'goods'
    id = Column(Integer, primary_key=True)
    name = Column(String(30))
    group_id = Column(Integer, ForeignKey('groups.id'))
    group = relationship('Group', backref='goods')

    visible = Column(Boolean, default=False)
    archive = Column(Boolean)

    def __repr__(self):
        return self.name


def sacrud_settings(config):
    config.include('pyramid_sacrud', route_prefix='admin')
    config.registry.settings['pyramid_sacrud.models'] = (
        ('Catalouge', [Group, Good]),
        ('Auth system', [User])
    )


def database_settings(config):
    from sqlalchemy import engine_from_config
    settings = config.registry.settings
    engine = engine_from_config(settings, 'sqlalchemy.')
    Base.metadata.bind = engine
    Base.metadata.drop_all()
    Base.metadata.create_all()


def add_fixtures():
    for user_name in ('admin', 'moderator', 'user1', 'user2'):
        DBSession.add(User(name=user_name))
    for group_name in ('Electronics', 'Fashion', 'Home & Garden', 'Motors'):
        group = Group(name=group_name)
        DBSession.add(group)
        if group_name == 'Electronics':
            DBSession.add(Good(name='iPhone', group=group))
            DBSession.add(Good(name='Fridge', group=group))
            DBSession.add(Good(name='YotaPhone', group=group))
        elif group_name == 'Fashion':
            DBSession.add(Good(name='Jeans', group=group))
        elif group_name == 'Home & Garden':
            DBSession.add(Good(name='Rake', group=group))
        elif group_name == 'Motors':
            DBSession.add(Good(name='Chevrolet Cavalier', group=group))
            DBSession.add(Good(name='LADA Granta', group=group))
    DBSession.commit()


class Root(object):
    __acl__ = [(Allow, 'admin', perm) for perm in permissions]

    def __init__(self, request):
        self.request = request


def login(request):
    headers = remember(request, 'admin')
    return HTTPFound(location=request.route_url(PYRAMID_SACRUD_HOME),
                     headers=headers)


def logout(request):
    headers = forget(request)
    return HTTPFound(location=request.route_url(PYRAMID_SACRUD_HOME),
                     headers=headers)


def main(global_settings, **settings):
    my_session_factory = SignedCookieSessionFactory('itsaseekreet')
    config = Configurator(
        settings=settings,
        session_factory=my_session_factory,
    )
    if 'auth' in settings:
        authn_policy = AuthTktAuthenticationPolicy(
            'sosecret',
            callback=lambda u, r: ['admin', ],
        )
        config.set_authentication_policy(authn_policy)
        config.set_authorization_policy(ACLAuthorizationPolicy())
        config.set_root_factory(Root)

        config.add_route('login', '/login')
        config.add_route('logout', '/logout')
        config.add_view(login, route_name='login')
        config.add_view(logout, route_name='logout')

    config.include(database_settings)
    config.include(sacrud_settings)


    if 'fixtures' in settings:
        add_fixtures()
    return config.make_wsgi_app()


if __name__ == '__main__':
    settings = {
        'sqlalchemy.url': 'sqlite:///example.sqlite',
        'fixtures': True,
    }
    app = main({}, **settings)

    from wsgiref.simple_server import make_server
    server = make_server('0.0.0.0', 6543, app)
    server.serve_forever()

@@ -4006,17 +4006,16 @@
 tings)%0A%0A
-%0A
     if '
@@ -4254,24 +4254,142 @@
 *settings)%0A%0A
+    try:%0A        from waitress import serve%0A        serve(app, host='0.0.0.0', port=6543)%0A    except ImportError:%0A    
     from wsg
@@ -4426,24 +4426,28 @@
 _server%0A    
+ 
+   
 server = mak
@@ -4477,16 +4477,20 @@
 3, app)%0A
+    
     serv

use waitress in example if it's possible