def __init__(self, settings: Settings) -> None: config = { 'INSTALLED_APPS': settings.get('INSTALLED_APPS', []), 'DATABASES': settings.get('DATABASES', {}), 'AUTH_USER_MODEL': settings.get('AUTH_USER_MODEL', 'auth.User') } if not django_settings.configured: django_settings.configure(**config) django.setup() self.models = {model.__name__: model for model in apps.get_models()}
def render_response(handler: Handler, injector: Injector, settings: Settings, accept: http.Header, ret: ReturnValue) -> http.Response: """ Render a response, using content negotiation to determine an appropriate renderer. Used in the default `BEFORE_REQUEST` configuration. """ if isinstance(ret, http.Response): data, status, headers, content_type = ret if content_type is not None or (status >= 300 and status <= 399): return ret else: data, status, headers, content_type = ret, 200, {}, None if data is None or data == b'': content = b'' content_type = None else: default_renderers = settings.get('RENDERERS', DEFAULT_RENDERERS) renderers = getattr(handler, 'renderers', default_renderers) renderer = negotiate_renderer(accept, renderers) if renderer is None: raise exceptions.NotAcceptable() content = injector.run(renderer.render, {'response_data': data}) if isinstance(content, http.Response): return content content_type = renderer.get_content_type() if not content and status == 200: status = 204 content_type = None return http.Response(content, status, headers, content_type)
def test_misconfigured_jwt_settings() -> None: settings = Settings({ 'JWT': {}, }) token = 'abc' with pytest.raises(exceptions.ConfigurationError): JWT(token=token, settings=settings)
def test_misconfigured_static_files() -> None: router = WerkzeugRouter([]) settings = Settings( {'STATICS': { 'ROOT_DIR': None, 'PACKAGE_DIRS': ['apistar'] }}) statics = WhiteNoiseStaticFiles(router=router, settings=settings) with pytest.raises(exceptions.ConfigurationError): statics.get_url('/apistar/css/base.css')
def get_auth(handler: Handler, injector: Injector, settings: Settings): default_authentication = settings.get('AUTHENTICATION', None) authentication = getattr(handler, 'authentication', default_authentication) if authentication is None: return Unauthenticated() for authenticator in authentication: auth = injector.run(authenticator.authenticate) if auth is not None: return auth return Unauthenticated()
def get_settings(settings: Settings): user_settings = dict(DEFAULTS) user_settings.update(settings.get(SETTINGS_KEY, {})) for module in IMPORT_STRINGS: if module not in user_settings and module not in DEFAULTS: raise ConfigurationError(f'{module} settings is required.') if user_settings['ORM'] == 'sqlalcamy': user_settings[module] = import_from_string(user_settings[module]) return user_settings
def __init__(self, settings: Settings) -> None: """ Configure MongoDB database backend. Args: settings: The application settings dictionary. """ self.config = settings.get('DATABASE', {}) self.url = self.config.get('URL', '') self.database_name = self.config.get('NAME', '') self.client = None
async def check_permissions_async(handler: Handler, injector: Injector, settings: Settings): """ An async variant of 'check_permissions'. """ default_permissions = settings.get('PERMISSIONS', None) permissions = getattr(handler, 'permissions', default_permissions) if permissions is None: return for permission in permissions: if not await injector.run_async(permission.has_permission): raise exceptions.Forbidden()
def check_permissions(handler: Handler, injector: Injector, settings: Settings) -> None: """ Ensure that any configured permissions are met. Used in the default `BEFORE_REQUEST` configuration. """ default_permissions = settings.get('PERMISSIONS', None) permissions = getattr(handler, 'permissions', default_permissions) if permissions is None: return for permission in permissions: if not injector.run(permission.has_permission): raise exceptions.Forbidden()
def __init__(self, settings: Settings) -> None: """ Initialize the Redis connection pool and provide a simple interface to Redis connections. NOTE: The connection pool is established and tested in a blocking fashion. XXX(jeff) Update settings to be more flexible and parse out the URL if needed for the cli. """ logger.debug('Redis::__init__:') self._config = settings.get('REDIS', {}) self._pool = None self._url = self._config.get('URL') self._min_pool = self._config.get('MIN_POOL', 1) self._max_pool = self._config.get('MAX_POOL', 8)
def get_request_data(headers: http.Headers, injector: Injector, settings: Settings): content_type = headers.get('Content-Type') if not content_type: return None media_type, _ = parse_options_header(content_type) parser_mapping = { parser.media_type: parser for parser in settings.get('PARSERS', parsers.DEFAULT_PARSERS) } if media_type not in parser_mapping: raise exceptions.UnsupportedMediaType() parser = parser_mapping[media_type] return injector.run(parser.parse)
def __init__(self, redis: Redis, settings: Settings, asyncpg: AsyncPgBackend, client: Client) -> None: self._config = settings.get('SLACK', {}) self._asyncpg = asyncpg self._client = client self._name = self._config.get('BOT_NAME') self._emoji = ':robot_face:' self._verification = self._config.get('VERIFICATION_TOKEN') self._oauth = { 'client_id': self._config.get('CLIENT_ID'), 'client_secret': self._config.get('CLIENT_SECRET'), 'scope': self._config.get('API_SCOPE'), } self._cw = CryptoWorld(redis, client)
def __init__(self, router: Router, routes: RouteConfig, settings: Settings) -> None: try: url = router.reverse_url('serve_schema') except exceptions.NoReverseMatch: url = None content = {} for route in flatten_routes(routes): if getattr(route.view, 'exclude_from_schema', False): continue content[route.name] = get_link(route) schema_settings = settings.get('SCHEMA', {}) title = schema_settings.get('TITLE', '') description = schema_settings.get('DESCRIPTION', '') url = schema_settings.get('URL', url) super().__init__(title=title, description=description, url=url, content=content)
def __init__(self, settings: Settings) -> None: self._config = settings.get('DATABASE') self._url = self._config.get('URL') self._pool = None
def __init__(self, settings: Settings) -> None: config = settings.get('APISTAR_SHELL', {}) self.imports = config.get('PRE_IMPORTS', []) self.custom_function = config.get('CUSTOM_FUNCTION') self.header = config.get('HEADER', 'Shell')