view bitsyblog/factory.py @ 59:0af1f4ae328d

fix a couple of basic things
author egj@socialplanning.org
date Mon, 04 Jan 2010 03:12:04 +0000
parents b97d11e2cf41
children f6a6a4b072e7
line wrap: on
line source

from bitsyauth import BitsyAuth
from bitsyblog import BitsyBlog, BitsierBlog
from getpass import getpass 
from paste.httpexceptions import HTTPExceptionHandler

# accepted configuration keys, e.g. 'bitsyblog.file_dir'
config = set(['file_dir', 
              'date_format', 
              'subject', 
              'n_links', 
              'help_file', 
              'header', 
              'template_directories', 
              'feed_items'])

def factory(global_conf, **app_conf):
    """make bitsyauth app and wrap it in middleware"""
    key_str = 'bitsyblog.%s'
    args = dict([ (key, app_conf[ key_str % key]) for key in config
                  if app_conf.has_key(key_str % key) ])
    app = BitsyBlog(**args)
    secret = app_conf.get('secret', 'secret')
    return BitsyAuth(HTTPExceptionHandler(app), 
                     global_conf,
                     app.passwords,
                     app.newuser,
                     'bitsyblog', 
                     secret)


def bitsierfactory(global_conf, **app_conf):
    """make single-user bitsyblog"""
    key_str = 'bitsyblog.%s'
    args = dict([ (key, app_conf[ key_str % key]) for key in config
                  if app_conf.has_key(key_str % key) ])
    user = app_conf['bitsyblog.user']
    app = BitsierBlog(**args)
    app.user = user
    secret = app_conf.get('secret', 'secret')
    auth = BitsyAuth(HTTPExceptionHandler(app),
                     global_conf,
                     app.passwords,
                     newuser=None,
                     site=app_conf.get('site', 'bitsyblog'),
                     secret=secret)
    if not user in app.users:
        pw = getpass('Enter password for %s: ' % user)
        app.newuser(user, auth.hash(app.user, pw))
    return auth