def __init__(self, config): """Set up the UserService REST client library with the location to communicate with. :param config: The mongodb config. E.g:: cfg = dict( dbname="everyone", port=27017, host="127.0.0.1", ) """ self.log = get_log("{}.USMBMetadataProvider".format(__name__)) from pp.user.model import db cfg = dict( db_name=config.get("dbname", "pptestdb"), port=int(config.get("port", 27017)), host=config.get("host", "127.0.0.1"), ) self.log.debug("MongoDB config<{:s}>".format(cfg)) db.init(cfg)
def mongodb(request): """Set up a mongo connection reset and ready to roll. """ from pp.user.model import db as mongo log = get_log('mongodb') dbname = "testingdb-{}".format(uuid.uuid4().hex) mongo.init(dict(dbname=dbname)) db = mongo.db() db.hard_reset() log.info('database ready for testing "{}"'.format(dbname)) def db_teardown(x=None): db.hard_reset() log.warn('teardown database for testing "{}"'.format(dbname)) request.addfinalizer(db_teardown) return db
def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ log = logging.getLogger("{}.main".format(__name__)) config = Configurator(settings=settings) cfg = dict( dbname=settings.get("mongodb.dbname", "ppusertestdb"), port=int(settings.get("mongodb.port", 27017)), host=settings.get("mongodb.host", "127.0.0.1"), ) log.info("MongoDB config<{:s}>".format(cfg)) db.init(cfg) # Custom 404 json response handler. This returns a useful JSON # response in the body of the 404. # XXX this is conflicting #config.add_view(restfulhelpers.notfound_404_view, context=HTTPNotFound) not_found = restfulhelpers.xyz_handler(httplib.NOT_FOUND) config.add_view(not_found, context='pyramid.exceptions.NotFound') bad_request = restfulhelpers.xyz_handler(httplib.BAD_REQUEST) config.add_view( bad_request, context='pyramid.httpexceptions.HTTPBadRequest' ) # Maps to the status page: config.add_route('home', '/') # sysadmin actions config.add_route('dump', '/usiverse/dump/') config.add_route('load', '/usiverse/load/') # User management config.add_route('the_users', '/users') config.add_route('the_users-1', '/users/') config.add_route('user-auth', '/access/auth/{username}/') config.add_route('user-auth-1', '/access/auth/{username}') # recover an access secret token for the given access token: config.add_route('user-secret', '/access/secret/{access_token}/') config.add_route('user-secret-1', '/access/secret/{access_token}/') config.add_route('user', '/user/{username}') config.add_route('user-1', '/user/{username}/') # Pick up the views which set up the views automatically: # config.scan("pp.user.service", ignore="pp.user.service.tests") # Make the pyramid app I'll then wrap in other middleware: app = config.make_wsgi_app() # Add in the configured pp_auth magic. app = pp_auth_middleware(settings, app) # RESTful helper class to handle PUT, DELETE over POST requests: app = restfulhelpers.HttpMethodOverrideMiddleware(app) # Should be last to catch all errors of below wsgi apps. This # returns useful JSON response in the body of the 500: app = restfulhelpers.JSONErrorHandler(app) return app