def module_registered(state): app = state.app db = state.options.get('db') db_name = state.options.get('db_name') sessions_db_name = state.options.get('sessions_db_name') loop = state.options.get('loop') if not db_name: raise RuntimeError( "This blueprint expects you to provide database access! " "Use: app.blueprint(bp, db_name=...)") if _has_user_service(db_name): raise RuntimeError( "This blueprint already registered with this db_name! " "Use other db_name: app.blueprint(bp, db_name=...)") if not db: raise RuntimeError( "This blueprint expects you to provide database access! " "Use: app.blueprint(bp, db=...)") try: get_session_service(sessions_db_name) except (TypeError, ValueError): raise RuntimeError( "This blueprint expects `sessions` blueprint! Register it before" "and pass the `sessions_db_name` options") initdb, dropdb, manager, model = make_models(db, db_name, loop) # noqa service = UserService(app, db_name, initdb, dropdb, manager, model) service.sessions_db_name = sessions_db_name _register_user_service(db_name, service) _set_default_user_service(db_name)
async def set_request_user(self, request, response, user): from sessions import get_session_service sessions = get_session_service(self.sessions_db_name) username = user.username if user and user.is_authenticated() else '' await sessions.update_request_session_data(request, response, { self.sessions_key: username })
async def get_request_user(self, request): from sessions import get_session_service sessions = get_session_service(self.sessions_db_name) data = await sessions.get_request_session_data(request) if self.sessions_key in data: username = data[self.sessions_key] if username: return await self.get_user(username) return AnonymousUser()
def make_test_app(): clear_session_services() app = Sanic(__name__) database = PostgresqlDatabase(database=DB) app.blueprint(bp, db=database, db_name=DB_NAME, loop=None) service = get_session_service() service.dropdb() service.initdb() database.close() database.allow_sync = False app.db = database return app
def recreatedb(loop=None): # loop if not loop: loop = async_loop.new_event_loop() asyncio.set_event_loop(loop) # init db service = get_user_service() service.dropdb() service.initdb() service = get_session_service() service.dropdb() service.initdb() service = get_entry_service(BLOG_ENTRY_DB_NAME) service.dropdb() service.initdb() service = get_entry_service(COMMENT_ENTRY_DB_NAME) service.dropdb() service.initdb() service = get_entry_service(FILES_ENTRY_DB_NAME) service.dropdb() service.initdb()
def get_session_value(app, key): session = get_session_service() app.db.allow_sync = True obj = session._model.get(key=key) return obj
async def handler(request): session = get_session_service() response = json({'status': 'ok'}) data = {'x': 1} await session.set_request_session_data(request, response, data) return response
async def handler(request): session = get_session_service() data = await session.get_request_session_data(request) return json({'data': data})
async def set_session_data(): nonlocal uid session = get_session_service() await session.set_session_data(uid, {'x': 1, 'y': '2'})
async def set_and_update(): session = get_session_service() await session.set_session_data(uid, {'x': 1, 'y': 2}) await session.update_session_data(uid, {'x': 2, 'z': 2}) return await session.get_session_data(uid)