Source

whatpylonsproject / pylonssecuredapp / websetup.py

"""Setup the PylonsSecuredApp application"""
import logging

import pylons.test

from pylonssecuredapp.config.environment import load_environment

from pylonssecuredapp.model import meta
from pylonssecuredapp.model.auth import User, Group, Permission

log = logging.getLogger(__name__)


def setup_app(command, conf, vars):
    """Place any commands to setup pylonssecuredapp here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    load_environment(conf.global_conf, conf.local_conf)
    setup_db(meta.engine)


def setup_db(engine):
    """Setup the database using the ``engine``"""
    # Create the tables if they don't already exist
    log.info("Creating tables")
    meta.metadata.drop_all(bind=engine, checkfirst=True)
    meta.metadata.create_all(bind=engine)
    log.info("Successfully setup")
    
    # Creating the rows:
    gustavo = User()
    gustavo.user_name = u'gustavo'
    gustavo.password = u'freedomware'
    meta.Session.add(gustavo)
    
    foo = User()
    foo.user_name = u'foo'
    foo.password = u'whatever'
    meta.Session.add(foo)

    admins = Group()
    admins.group_name = u'admins'
    admins.users.append(gustavo)
    meta.Session.add(admins)

    administrate = Permission()
    administrate.permission_name = u'administrate'
    administrate.groups.append(admins)
    meta.Session.add(administrate)
    
    # Applying changes:
    meta.Session.commit()