from pyramid.config import Configurator
from pyramid.authentication import AuthTktAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.session import SignedCookieSessionFactory
from pyramid_mailer import mailer_factory_from_settings

from .models.users import groupfinder


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    # session factory
    my_session_factory = SignedCookieSessionFactory('IbljK8dqIDURgz')
    authentication_policy = AuthTktAuthenticationPolicy('UzvRazicail6Ef', 
                            callback=groupfinder, hashalg='sha512', timeout=36000)
    authorization_policy = ACLAuthorizationPolicy()
    
    with Configurator(settings=settings,
                    root_factory='tcs_site.security.RootFactory',
                    authentication_policy=authentication_policy,
                    authorization_policy=authorization_policy) as config:
        config.registry['mailer'] = mailer_factory_from_settings(settings)
        config.include('pyramid_jinja2')
        config.include('.models')
        config.include('.routes')
        config.set_session_factory(my_session_factory)
        config.scan()
    return config.make_wsgi_app()
