コード例 #1
0
ファイル: database.py プロジェクト: eevee/spline
def create_user(parser, args):
    engine = engine_from_config(vars(args), 'sqlalchemy.')
    session.configure(bind=engine)

    with transaction.manager:
        all_groups = {
            group.name: group
            for group in session.query(Group)
        }

    username = input('username: '******'email: ').strip()
    password = getpass.getpass('password: '******'confirm password: '******'t match!")
        sys.exit(1)

    group_names = []
    if all_groups:
        print()
        print("available groups: {}".format(', '.join(all_groups)))
        group_names = input('comma-separated list of groups to add to: ').strip().split(',')

    with transaction.manager:
        pwhash = bcrypt.hashpw(
            password.encode('utf8'),
            bcrypt.gensalt(14),
        ).decode('ascii')

        # TODO would be neat to have a password field that hashes on assignment
        # and does the right thing with equality check
        user = User(name=username, email=email, password=pwhash, groups=[])
        for group_name in group_names:
            user.groups.append(all_groups[group_name])
        session.add(user)
        session.flush()
        userid = user.id

    print()
    print("created user {} with id {}".format(username, userid))
コード例 #2
0
ファイル: app.py プロジェクト: eevee/spline
def main(global_settings, **settings):
    # TODO should allow end user to configure logging, of course; this is just
    # a nice default
    coloredlogs.install(level=logging.INFO)

    # TODO this doesn't actually work as a paste entry point, because the ini
    # values need converting  :S
    datadir = Path(settings['spline.datadir'])

    # Built-in core settings we have to have.  These are part of the app
    # propert and it makes zero sense for either a deployer or a developer to
    # ever change them.
    # TODO is this where this kind of thing should go?  seems, y'know, clunky
    settings.update({
        'pyramid.default_locale_name': u'en',
        'mako.directories': ['spline:templates'],
        'mako.module_directory': str(datadir / '_mako_cache'),
        'mako.strict_undefined': True,

        # TODO i am not thrilled that only a string works here
        # TODO: pyramid_scss should learn to do asset specs in imports as well,
        # but scss needs import hooking for that to work
        'scss.asset_path': 'spline:assets/scss\n'
        'spline:assets/vendor/archetype/scss',

        # These are reversed in debug mode
        # TODO scss should really be built and served directly in production
        'scss.compress': True,
        'scss.cache': True,
    })

    # Prefill some paths for stuff stored on disk.
    # TODO this is pretty grody  :)
    # TODO this should be a little more organized and enforce that the
    # directories exist and are writable by us -- or at least that the datadir
    # itself is?
    settings.update({
        'spline.search.whoosh.path': str(datadir / 'whoosh-index'),
        'spline.wiki.root': str(datadir / 'wiki'),

        # TODO this is all going away, i think.  lol CHANGEME.  what is that
        # even for?
        'session.type': 'file',
        'session.data_dir': str(datadir / 'sessions/data'),
        'session.lock_dir': str(datadir / 'sessions/lock'),
        'session.secret': 'CHANGEME',
        'session.cookie_on_exception': True,
    })

    debug = settings.get('spline.debug')
    if debug:
        settings.update({
            # TODO maybe i want to turn these on...  why didn't i?
            'pyramid.debug_authorization': True,
            'pyramid.debug_notfound': True,
            'pyramid.debug_routematch': True,
            'pyramid.reload_templates': True,
            'scss.compress': False,
            'scss.cache': False,
        })

    engine = engine_from_config(settings, 'sqlalchemy.')
    session.configure(bind=engine)

    session_factory = pyramid_beaker.session_factory_from_settings(settings)

    config = Configurator(settings=settings)
    if debug:
        # TODO only if importable?
        config.include('pyramid_debugtoolbar')
    config.include('pyramid_tm')

    config.include('pyramid_mako')

    from spline.feature.filestore import IStorage, FilesystemStorage
    filestore_dir = (datadir / 'filestore').resolve()
    config.registry.registerUtility(FilesystemStorage(filestore_dir), IStorage)
    config.add_static_view('filestore', str(filestore_dir))

    # Sessions
    config.include(pyramid_beaker)
    config.set_session_factory(session_factory)
    config.add_tween('spline.web.auth.csrf_tween_factory')

    # Auth
    config.set_authentication_policy(DatabaseAuthenticationPolicy())
    config.set_authorization_policy(RoleAuthorizationPolicy())
    config.add_request_method(authenticated_userid, 'user', reify=True)

    # Events
    config.add_subscriber(inject_template_vars, BeforeRender)

    # Static assets
    config.add_static_view('static', 'spline:assets', cache_max_age=3600)

    # Load core stuff first
    config.include(core_plugin_includeme)

    # Plugin loading
    # TODO this could totally be inside the app proper, as long as i know how
    # to restart myself.  mamayo problem?
    # TODO there are really two steps here, of scanning and then importing.
    # but i doubt the web app itself wants to scan just to import, right?
    # TODO now there's both a plugin dict here AND a "registry" in the Plugin
    # class itself (which is still in spline_comic btw) -- which is correct?
    # TODO SIGH should i just give up and use setuptools f*****g entry points
    # TODO or actually maybe i should be taking advantage of zope things here
    # -- you can have named implementations of an interface after all!
    config.registry.spline_plugins = {}
    config.add_directive('register_spline_plugin',
                         config_register_spline_plugin)
    for plugins in settings.get('spline.plugins', ()):
        plugin, route_prefix = plugins.split(':', 1)
        config.include(plugin, route_prefix=route_prefix)

    # Sass compilation
    # TODO this has to appear after the includes, or pyramid_scss won't see any
    # additional paths added.  needs a real api for doing that
    config.include('pyramid_scss')
    config.add_route('pyscss', '/css/{css_path:[^/]+}.css')
    config.add_view(route_name='pyscss',
                    view='pyramid_scss.controller.get_scss',
                    renderer='scss',
                    request_method='GET')

    return config.make_wsgi_app()
コード例 #3
0
ファイル: database.py プロジェクト: eevee/spline
def init_db(parser, args):
    settings = vars(args)

    # Logging
    # TODO this should probably go in the main entry point
    coloredlogs.install(level=logging.INFO)
    # TODO what is this for, it was debug-only, is there any reason we wouldn't want it
    #logging.basicConfig()
    #logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

    # Plugin loading
    plugin_list = import_plugins(settings.get('spline.plugins', ()))

    engine = engine_from_config(settings, 'sqlalchemy.')
    session.configure(bind=engine)

    Base.metadata.create_all(engine, checkfirst=True)

    comic_title = settings.get('spline.comic_name')
    chapter_title = settings.get('spline.chapter_name')

    with transaction.manager:
        try:
            g = session.query(Group).filter_by(id=1).one()
        except:
            g = Group(id=1, name='admin')
            session.add(g)
        try:
            gp0 = session.query(GroupPermission).filter_by(id=1).one()
        except:
            gp0 = GroupPermission(id=1, scope='core', permission='admin')
            gp0.group = g
            session.add(gp0)
        # Only needed if the comic plugin is loaded
        if 'spline_comic' in plugin_list:
            from spline_comic.models import Comic, ComicChapter
            try:
                gp1 = session.query(GroupPermission).filter_by(id=1).one()
            except:
                gp1 = GroupPermission(id=1, scope='comic', permission='admin')
                gp1.group = g
                session.add(gp1)
            try:
                comic = session.query(Comic).filter_by(id=1).one()
            except:
                comic = Comic(id=1, title=comic_title, config_timezone='GMT')
                session.add(comic)
            try:
                chap = session.query(ComicChapter).filter_by(id=1).one()
            except:
                chap = ComicChapter(id=1, title=chapter_title, left=0, right=0)
                chap.comic = comic
                session.add(chap)
        # Only needed if the wiki is loaded
        if 'spline_wiki' in plugin_list:
            try:
                gp2 = session.query(GroupPermission).filter_by(id=2).one()
            except:
                gp2 = GroupPermission(id=2, scope='wiki', permission='edit')
                gp2.group = g
                session.add(gp2)
コード例 #4
0
ファイル: app.py プロジェクト: eevee/spline
def main(global_settings, **settings):
    # TODO should allow end user to configure logging, of course; this is just
    # a nice default
    coloredlogs.install(level=logging.INFO)

    # TODO this doesn't actually work as a paste entry point, because the ini
    # values need converting  :S
    datadir = Path(settings['spline.datadir'])

    # Built-in core settings we have to have.  These are part of the app
    # propert and it makes zero sense for either a deployer or a developer to
    # ever change them.
    # TODO is this where this kind of thing should go?  seems, y'know, clunky
    settings.update({
        'pyramid.default_locale_name': u'en',
        'mako.directories': ['spline:templates'],
        'mako.module_directory': str(datadir / '_mako_cache'),
        'mako.strict_undefined': True,

        # TODO i am not thrilled that only a string works here
        # TODO: pyramid_scss should learn to do asset specs in imports as well,
        # but scss needs import hooking for that to work
        'scss.asset_path':
            'spline:assets/scss\n'
            'spline:assets/vendor/archetype/scss',

        # These are reversed in debug mode
        # TODO scss should really be built and served directly in production
        'scss.compress': True,
        'scss.cache': True,
    })

    # Prefill some paths for stuff stored on disk.
    # TODO this is pretty grody  :)
    # TODO this should be a little more organized and enforce that the
    # directories exist and are writable by us -- or at least that the datadir
    # itself is?
    settings.update({
        'spline.search.whoosh.path': str(datadir / 'whoosh-index'),
        'spline.wiki.root': str(datadir / 'wiki'),

        # TODO this is all going away, i think.  lol CHANGEME.  what is that
        # even for?
        'session.type': 'file',
        'session.data_dir': str(datadir / 'sessions/data'),
        'session.lock_dir': str(datadir / 'sessions/lock'),
        'session.secret': 'CHANGEME',
        'session.cookie_on_exception': True,
    })

    debug = settings.get('spline.debug')
    if debug:
        settings.update({
            # TODO maybe i want to turn these on...  why didn't i?
            'pyramid.debug_authorization': True,
            'pyramid.debug_notfound': True,
            'pyramid.debug_routematch': True,

            'pyramid.reload_templates': True,
            'scss.compress': False,
            'scss.cache': False,
        })

    engine = engine_from_config(settings, 'sqlalchemy.')
    session.configure(bind=engine)

    session_factory = pyramid_beaker.session_factory_from_settings(settings)

    config = Configurator(settings=settings)
    if debug:
        # TODO only if importable?
        config.include('pyramid_debugtoolbar')
    config.include('pyramid_tm')

    config.include('pyramid_mako')

    from spline.feature.filestore import IStorage, FilesystemStorage
    filestore_dir = (datadir / 'filestore').resolve()
    config.registry.registerUtility(FilesystemStorage(filestore_dir), IStorage)
    config.add_static_view('filestore', str(filestore_dir))

    # Sessions
    config.include(pyramid_beaker)
    config.set_session_factory(session_factory)
    config.add_tween('spline.web.auth.csrf_tween_factory')

    # Auth
    config.set_authentication_policy(DatabaseAuthenticationPolicy())
    config.set_authorization_policy(RoleAuthorizationPolicy())
    config.add_request_method(authenticated_userid, 'user', reify=True)

    # Events
    config.add_subscriber(inject_template_vars, BeforeRender)

    # Static assets
    config.add_static_view('static', 'spline:assets', cache_max_age=3600)

    # Load core stuff first
    config.include(core_plugin_includeme)

    # Plugin loading
    # TODO this could totally be inside the app proper, as long as i know how
    # to restart myself.  mamayo problem?
    # TODO there are really two steps here, of scanning and then importing.
    # but i doubt the web app itself wants to scan just to import, right?
    # TODO now there's both a plugin dict here AND a "registry" in the Plugin
    # class itself (which is still in spline_comic btw) -- which is correct?
    # TODO SIGH should i just give up and use setuptools f*****g entry points
    # TODO or actually maybe i should be taking advantage of zope things here
    # -- you can have named implementations of an interface after all!
    config.registry.spline_plugins = {}
    config.add_directive(
        'register_spline_plugin', config_register_spline_plugin)
    for plugins in settings.get('spline.plugins', ()):
        plugin, route_prefix = plugins.split(':', 1)
        config.include(plugin, route_prefix=route_prefix)

    # Sass compilation
    # TODO this has to appear after the includes, or pyramid_scss won't see any
    # additional paths added.  needs a real api for doing that
    config.include('pyramid_scss')
    config.add_route('pyscss', '/css/{css_path:[^/]+}.css')
    config.add_view(route_name='pyscss', view='pyramid_scss.controller.get_scss', renderer='scss', request_method='GET')

    return config.make_wsgi_app()
コード例 #5
0
ファイル: initializedb.py プロジェクト: Epithumia/spline
def initdb(**settings):

    engine = engine_from_config(settings, 'sqlalchemy.')
    session.configure(bind=engine)

    config = Configurator(settings=settings)

    # Logging
    config.include('spline.lib.logging')

    # Plugin loading
    debug = settings.get('spline.debug')
    if debug:
        logging.basicConfig()
        logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
    plugin_list = []
    try:
        for plugins in settings.get('spline.plugins', ()):
            plugin, route_prefix = plugins.split(':', 1)
            importlib.import_module(plugin)
            plugin_list.append(plugin)
    except TypeError:
        pass
    Base.metadata.create_all(engine, checkfirst=True)

    adm_name = settings.get('spline.admin_name')
    adm_pw = settings.get('spline.admin_pw')
    adm_email = settings.get('spline.admin_email')
    comic_title = settings.get('spline.comic_name')
    chapter_title = settings.get('spline.chapter_name')
    p = adm_pw.encode('utf8')
    pw = hashpw(p, gensalt(14))

    with transaction.manager:
        try:
            u = session.query(User).filter_by(id=1).one()
        except:
            u = User(id=1, email=adm_email, name=adm_name, password=pw.decode('ascii'))
            session.add(u)
        try:
            g = session.query(Group).filter_by(id=1).one()
        except:
            g = Group(id=1, name='admin')
            g.users.append(u)
            session.add(g)
        try:
            gp0 = session.query(GroupPermission).filter_by(id=1).one()
        except:
            gp0 = GroupPermission(id=1, scope='core', permission='admin')
            gp0.group = g
            session.add(gp0)
        # Only needed if the comic plugin is loaded
        if 'spline_comic' in plugin_list:
            from spline_comic.models import Comic, ComicChapter
            try:
                gp1 = session.query(GroupPermission).filter_by(id=1).one()
            except:
                gp1 = GroupPermission(id=1, scope='comic', permission='admin')
                gp1.group = g
                session.add(gp1)
            try:
                comic = session.query(Comic).filter_by(id=1).one()
            except:
                comic = Comic(id=1, title=comic_title, config_timezone='GMT')
                session.add(comic)
            try:
                chap = session.query(ComicChapter).filter_by(id=1).one()
            except:
                chap = ComicChapter(id=1, title=chapter_title, left=0, right=0)
                chap.comic = comic
                session.add(chap)
        # Only needed if the wiki is loaded
        if 'spline_wiki' in plugin_list:
            try:
                gp2 = session.query(GroupPermission).filter_by(id=2).one()
            except:
                gp2 = GroupPermission(id=2, scope='wiki', permission='edit')
                gp2.group = g
                session.add(gp2)