import os
import sys
import transaction

from sqlalchemy import engine_from_config

from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )

from pyramid.scripts.common import parse_vars

from ..models import (
    DBSession,
    Base,
    User,
    Site,
    SiteAPIKey,
)


def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri> [var=value]\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)


def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)

    with transaction.manager:
        DBSession.add(User(email='test@example.com', password='test', admin=True))
        DBSession.add(Site(name='asd', key='80d621df066348e5938a469730ae0cab'))
        DBSession.add(SiteAPIKey(site_id=1, key='GIKfxIcIHPbM0uX9PrQ1To29Pb2on0pa'))
import os
import sys
import transaction

from sqlalchemy import engine_from_config

from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )

from pyramid.scripts.common import parse_vars

from ..models import (
    DBSession,
    Base,
    User,
    Site,
    SiteAPIKey,
)


def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri> [var=value]\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)


def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)

    with transaction.manager:
        user = User(email='test@example.com', password='test', admin=True)
        DBSession.add(user)
        site = Site(name='asd', key='80d621df066348e5938a469730ae0cab')
        DBSession.add(site)
        DBSession.add(SiteAPIKey(site_id=1, key='GIKfxIcIHPbM0uX9PrQ1To29Pb2on0pa'))
        site.users.append(user)

Make sure we add the user to the site as well
Make sure we add the user to the site as well
