def get_server_kwargs(self): kwargs = { 'no_keep_alive': False, 'xheaders': False, 'ssl_options': None, 'protocol': None, 'decompress_request': False, 'chunk_size': None, 'max_header_size': None, 'idle_connection_timeout': None, 'body_timeout': None, 'max_body_size': self.config.FILE_UPLOAD_MAX_BODY_SIZE, 'max_buffer_size': None, 'trusted_downstream': None } # HTTPS supporting https_config = getattr(self.config, 'HTTPS', None) if self.app.https_enabled and https_config is not None: if not all(['key_file' in https_config, https_config['key_file']]): raise ImproperlyConfigured('Key file not configured') if not all(['crt_file' in https_config, https_config['crt_file']]): raise ImproperlyConfigured('Crt file not configured') import ssl ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_ctx.load_cert_chain(https_config['crt_file'], https_config['key_file']) kwargs.update(ssl_options=ssl_ctx) logger.debug('HTTPS status: ON.') else: logger.warning('HTTPS status: OFF.') return kwargs
def __init__(self, *args, file_path=None, **kwargs): self._fname = None if file_path is not None: self.file_path = file_path else: self.file_path = getattr(settings, 'EMAIL_FILE_PATH', None) # Make sure self.file_path is a string. if not isinstance(self.file_path, str): raise ImproperlyConfigured( 'Path for saving emails is invalid: %r' % self.file_path) self.file_path = os.path.abspath(self.file_path) # Make sure that self.file_path is a directory if it exists. if os.path.exists( self.file_path) and not os.path.isdir(self.file_path): raise ImproperlyConfigured( 'Path for saving email messages exists, but is not a directory: %s' % self.file_path) # Try to create it, if it not exists. elif not os.path.exists(self.file_path): try: os.makedirs(self.file_path) except OSError as err: raise ImproperlyConfigured( 'Could not create directory for saving email messages: %s (%s)' % (self.file_path, err)) # Make sure that self.file_path is writable. if not os.access(self.file_path, os.W_OK): raise ImproperlyConfigured('Could not write to directory: %s' % self.file_path) # Finally, call super(). # Since we're using the console-based backend as a base, # force the stream to be None, so we don't default to stdout kwargs['stream'] = None super().__init__(*args, **kwargs)
def get_template_names(self): """ Return a list of template names to be used for the request. Must return a list. May not be called if render is overridden. """ try: names = super().get_template_names() except ImproperlyConfigured: # If template_name isn't specified, it's not a problem -- # we just start with an empty list. names = [] # If the list is a queryset, we'll invent a template name based on the # app and model name. This name gets put at the end of the template # name list so that user-supplied names override the automatically- # generated ones. if isinstance(self.object_list, Query): names.append("%s%s.html" % (opts.model_name, self.template_name_suffix)) elif not names: raise ImproperlyConfigured( "%(cls)s requires either a 'template_name' attribute " "or a get_queryset() method that returns a QuerySet." % { 'cls': self.__class__.__name__, }) return names
def get_queryset(self): """ Return the list of items for this handler. The return value must be an iterable and may be an instance of `QuerySet` in which case `QuerySet` specific behavior will be enabled. """ if self.queryset is not None: queryset = self.queryset if isinstance(queryset, Query): queryset = queryset.all() elif self.model is not None: queryset = self.model.query.all() else: raise ImproperlyConfigured( "%(cls)s is missing a QuerySet. Define " "%(cls)s.model, %(cls)s.queryset, or override " "%(cls)s.get_queryset()." % {'cls': self.__class__.__name__}) ordering = self.get_ordering() if ordering: if isinstance(ordering, str): ordering = (ordering, ) queryset = sort_query(queryset, *ordering) return queryset
def get_serializer(self, many=False): serializer_class = self.get_serializer_class() if serializer_class is not None: return serializer_class(many=many) raise ImproperlyConfigured( "SerializableMixin requires either a definition of " "'serializer_class' or an implementation of 'get_serializer()'")
def get_service_name(self): if self.service_name is None: raise ImproperlyConfigured( "ServiceContextMixin requires either a definition of " "'service_name' or an implementation of 'get_service_name()'") else: return self.service_name
def get_template_name(self): """Return a template name to be used for the request.""" if self.template_name is None: raise ImproperlyConfigured( "TemplateMixin requires either a definition of " "'template_name' or an implementation of 'get_template_name()'" ) else: return self.template_name
def _get_backends(return_tuples=False): backends = [] for backend_data in settings.PAYMENT_SYSTEM_BACKENDS: path = backend_data['NAME'] options = backend_data.get('OPTIONS', {}) try: backend_class = import_string(path) except ImportError: msg = "The module in NAME could not be imported: %s." \ "Check your PAYMENT_SYSTEM_BACKENDS setting." raise ImproperlyConfigured(msg % path) backend = backend_class(**options) backends.append((path, backend) if return_tuples else backend) if not backends: raise ImproperlyConfigured( 'No payment system backends have been defined. ' 'Does PAYMENT_SYSTEM_BACKENDS contain anything?') return backends
def get_serializer_class(self): if self.serializer_class is None: try: return self.object.__marshmallow__ except AttributeError: raise ImproperlyConfigured( "No serializer class for dumping data. Either provide a serializer_class " "or define schema on the Model.") return super().get_serializer_class()
def _get_backends(return_tuples=False): backends = [] for backend_path in settings.AUTHENTICATION_BACKENDS: backend = load_backend(backend_path) backends.append((backend, backend_path) if return_tuples else backend) if not backends: raise ImproperlyConfigured( 'No authentication backends have been defined. ' 'Does AUTHENTICATION_BACKENDS contain anything?') return backends
def get_hashers(): hashers = [] for hasher_path in settings.PASSWORD_HASHERS: hasher_cls = import_string(hasher_path) hasher = hasher_cls() if not getattr(hasher, 'algorithm'): raise ImproperlyConfigured("hasher doesn't specify an " "algorithm name: %s" % hasher_path) hashers.append(hasher) return hashers
def _parse_callback(self, entry): callback = entry.get('callback') if callback is not None: try: fn = import_string(callback) if fn is not None and not callable(fn): raise ImproperlyConfigured( 'Rate limit callback is not callable') return fn except ImportError: pass
def get_message_moderators(moderator_config): moderators = [] for moderator in moderator_config: try: klass = import_string(moderator['NAME']) except ImportError: msg = "The module in NAME could not be imported: %s." \ "Check your MODERATORS setting." raise ImproperlyConfigured(msg % moderator['NAME']) moderators.append(klass(**moderator.get('OPTIONS', {}))) return moderators
def __init__(self, location=None, base_url=None): location = location or setting('FTP_STORAGE_LOCATION') if location is None: raise ImproperlyConfigured("You must set a location at " "instanciation or at " " settings.FTP_STORAGE_LOCATION'.") self.location = location base_url = base_url or settings.MEDIA_URL self._config = self._decode_location(location) self._base_url = base_url self._connection = None
def __init__(self, settings_module): # update this dict from global settings (but only for ALL_CAPS settings) for setting in dir(global_settings): if setting.isupper(): setattr(self, setting, getattr(global_settings, setting)) # store the settings module in case someone later cares self.SETTINGS_MODULE = settings_module mod = importlib.import_module(self.SETTINGS_MODULE) tuple_settings = () self._explicit_settings = set() for setting in dir(mod): if setting.isupper(): setting_value = getattr(mod, setting) if (setting in tuple_settings and not isinstance(setting_value, (list, tuple))): raise ImproperlyConfigured( "The %s setting must be a list or a tuple. " % setting) setattr(self, setting, setting_value) self._explicit_settings.add(setting) if not self.SECRET_KEY: raise ImproperlyConfigured( "The SECRET_KEY setting must not be empty.") if hasattr(time, 'tzset') and self.TIME_ZONE: # When we can, attempt to validate the timezone. If we can't find # this file, no check happens and it's harmless. zoneinfo_root = '/usr/share/zoneinfo' if (os.path.exists(zoneinfo_root) and not os.path.exists( os.path.join(zoneinfo_root, *(self.TIME_ZONE.split('/'))))): raise ValueError("Incorrect timezone setting: %s" % self.TIME_ZONE) # Move the time zone info into os.environ. See ticket #2315 for why # we don't do this unconditionally (breaks Windows). os.environ['TZ'] = self.TIME_ZONE time.tzset()
def fixture_dirs(self): """Return a list of fixture directories.""" dirs = [] fixture_dirs = settings.FIXTURE_DIRS if len(fixture_dirs) != len(set(fixture_dirs)): raise ImproperlyConfigured( "settings.FIXTURE_DIRS contains duplicates.") dirs.extend(fixture_dirs) dirs.append('') return [os.path.realpath(d) for d in dirs]
def get_password_validators(validator_config): validators = [] for validator in validator_config: try: klass = import_string(validator['NAME']) except ImportError: msg = "The module in NAME could not be imported: %s." \ "Check your AUTH_PASSWORD_VALIDATORS setting." raise ImproperlyConfigured(msg % validator['NAME']) validators.append(klass(**validator.get('OPTIONS', {}))) return validators
def get_success_url(self): """Return the URL to redirect to after processing a valid form.""" if self.success_url: url = self.success_url.format(**self.object.__dict__) else: try: url = self.object.get_absolute_url() except AttributeError: raise ImproperlyConfigured( "No URL to redirect to. Either provide an url or define " "a get_absolute_url method on the Model.") return url
def __init__(self, handler=None, exc_info=None, is_email=False): if handler is not None and not isinstance(handler, RequestHandler): raise ImproperlyConfigured( '`handler` argument must be a `tornado.web.RequestHandler` instance' ) from anthill.framework.apps import app self.app = app self.handler = handler self.is_email = is_email self.exc_info = exc_info or sys.exc_info() self.exc_type, self.exc_value, self.tb = self.exc_info if self.html_template_name is None: raise ImproperlyConfigured('`html_template_name` required') if self.text_template_name is None: raise ImproperlyConfigured('`text_template_name` required') template_loader = Loader(Path(CURRENT_DIR, 'templates')) self._html_template = template_loader.load(self.html_template_name) self._text_template = template_loader.load(self.text_template_name)
def _decode_location(self, location): """Return splitted configuration data from location.""" splitted_url = urlparse.urlparse(location) config = {} if splitted_url.scheme not in ('ftp', 'aftp'): raise ImproperlyConfigured( 'FTPStorage works only with FTP protocol!' ) if splitted_url.hostname == '': raise ImproperlyConfigured('You must at least provide hostname!') if splitted_url.scheme == 'aftp': config['active'] = True else: config['active'] = False config['path'] = splitted_url.path config['host'] = splitted_url.hostname config['user'] = splitted_url.username config['passwd'] = splitted_url.password config['port'] = int(splitted_url.port) return config
async def build_context_from_context_processors( handler: RequestHandler) -> dict: """Build extra context for current handler on every request.""" ctx = {} for ctx_processor in CONTEXT_PROCESSORS: f = import_string(ctx_processor) # Context processor can be either co routine or plain function result = await f(handler) if inspect.iscoroutinefunction(f) else f( handler) if not isinstance(handler, RequestHandler): raise ImproperlyConfigured( 'Context processor `%s` got `%s` object, ' 'but need `RequestHandler`' % (f.__name__, handler.__class__.__name__)) if not isinstance(result, dict): raise ImproperlyConfigured( 'Context processor `%s` must return dict object, ' 'but `%s` returned' % (f.__name__, type(result))) if not result: logging.warning('Empty result for context processor `%s`' % f.__name__) ctx.update(result) return ctx
def _get_storage_path(cls): try: return cls._storage_path except AttributeError: storage_path = getattr(settings, 'SESSION_FILE_PATH', None) or tempfile.gettempdir() # Make sure the storage path is valid. if not os.path.isdir(storage_path): raise ImproperlyConfigured( "The session storage path %r doesn't exist. Please set your" " SESSION_FILE_PATH setting to an existing directory in which" " Anthill can store session data." % storage_path) cls._storage_path = storage_path return storage_path
def _setup(self, name=None): """ Load the settings module pointed to by the environment variable. This is used the first time settings are needed, if the user hasn't configured settings manually. """ settings_module = os.environ.get(ENVIRONMENT_VARIABLE) if not settings_module: desc = ("setting %s" % name) if name else "settings" raise ImproperlyConfigured( "Requested %s, but settings are not configured. " "You must either define the environment variable %s " "or call settings.configure() before accessing settings." % (desc, ENVIRONMENT_VARIABLE)) self._wrapped = Settings(settings_module)
def get_queryset(self): """ Return the queryset that will be used to look up the object. This method is called by the default implementation of get_object() and may not be called if get_object() is overridden. """ if self.queryset is None: if self.model: return self.model.query else: raise ImproperlyConfigured( "%(cls)s is missing a queryset. Define " "%(cls)s.model, %(cls)s.queryset, or override " "%(cls)s.get_queryset()." % {'cls': self.__class__.__name__}) return self.queryset
def get_queryset(self): """ Return the list of items for this handler. The return value must be an instance of `sqlalchemy.orm.Query`. """ if isinstance(self.queryset, Query): queryset = self.queryset elif self.model is not None: queryset = self.model.query else: raise ImproperlyConfigured( "%(cls)s is missing a queryset. Define " "%(cls)s.model, %(cls)s.queryset, or override " "%(cls)s.get_queryset()." % {'cls': self.__class__.__name__}) ordering = self.get_ordering() if ordering: if isinstance(ordering, str): ordering = (ordering, ) queryset = sort_query(queryset, *ordering) return queryset
def __init__(self, server, params, backend): self._backend = backend self._server = server self._params = params self.reverse_key = get_key_func( params.get("REVERSE_KEY_FUNCTION") or "anthill.framework.core.cache.backends.redis.util.default_reverse_key" ) if not self._server: raise ImproperlyConfigured("Missing connections string") if not isinstance(self._server, (list, tuple, set)): self._server = self._server.split(",") self._clients = [None] * len(self._server) self._options = params.get("OPTIONS", {}) self._slave_read_only = self._options.get('SLAVE_READ_ONLY', True) serializer_path = self._options.get( "SERIALIZER", "anthill.framework.core.cache.backends.redis.serializers.pickle.PickleSerializer" ) serializer_cls = import_string(serializer_path) compressor_path = self._options.get( "COMPRESSOR", "anthill.framework.core.cache.backends.redis.compressors.identity.IdentityCompressor" ) compressor_cls = import_string(compressor_path) self._serializer = serializer_cls(options=self._options) self._compressor = compressor_cls(options=self._options) self.connection_factory = pool.get_connection_factory( options=self._options)
def create_client(self, user=None): if self.client_class is None: raise ImproperlyConfigured('Client class is undefined') return self.client_class(user=user)
def __call__(self, resource_name, resource_key, exceeded_callback=None, *args, **kwargs): if exceeded_callback is not None and not callable(exceeded_callback): raise ImproperlyConfigured('Exceeded callback is not callable') def decorator(func): @wraps(func) def wrapper(*f_args, **f_kwargs): if not RATE_LIMIT_ENABLE or not RATE_LIMIT_CONFIG: if RATE_LIMIT_ENABLE and not RATE_LIMIT_CONFIG: logger.warning('Rate limit is not configured.') return func(*f_args, **f_kwargs) if resource_name not in self.config: logger.error('Resource `%s` is not configured.' % resource_name) return rate_requests_max, rate_duration_max = self.config[ resource_name]['rate'] storage_key = self.build_storage_key(resource_name, resource_key) with self.lock: rate_requests = self.storage.get(storage_key) if rate_requests is None: self.storage.set(storage_key, 1, timeout=rate_duration_max) elif rate_requests < rate_requests_max: self.storage.incr(storage_key) else: block = self.config[resource_name]['block'] callback = self.config[resource_name]['callback'] state = dict(rate_storage_key=storage_key, rate_requests_max=rate_requests_max, rate_duration=rate_duration_max, rate_requests=rate_requests, rate_resource_name=resource_name, rate_resource_key=resource_key) if block: if exceeded_callback is None: raise RateLimitException(state) else: kwargs.update(state) exceeded_callback(*args, **kwargs) return elif callback is not None: callback(**state) try: return func(*f_args, **f_kwargs) except Exception: # Fallback first then re-raise exception if rate_requests is not None and rate_requests > 0: self.storage.decr(storage_key) raise return wrapper return decorator
def get_success_url(self): """Return the URL to redirect to after processing a valid form.""" if not self.success_url: raise ImproperlyConfigured( "No URL to redirect to. Provide a success_url.") return str(self.success_url) # success_url may be lazy
def get_success_url(self): if self.success_url: return self.success_url.format(**self.object.__dict__) else: raise ImproperlyConfigured( "No URL to redirect to. Provide a success_url.")