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)
Exemple #2
0
    def fixture_dirs(self):
        """
        Return a list of fixture directories.

        The list contains the 'fixtures' subdirectory of each installed
        application, if it exists, the directories in FIXTURE_DIRS, and the
        current directory.
        """
        dirs = []
        fixture_dirs = settings.FIXTURE_DIRS
        if len(fixture_dirs) != len(set(fixture_dirs)):
            raise ImproperlyConfigured("settings.FIXTURE_DIRS contains duplicates.")
        for app_config in apps.get_app_configs():
            app_label = app_config.label
            app_dir = os.path.join(app_config.path, 'fixtures')
            if app_dir in fixture_dirs:
                raise ImproperlyConfigured(
                    "'%s' is a default fixture directory for the '%s' app "
                    "and cannot be listed in settings.FIXTURE_DIRS." % (app_dir, app_label)
                )

            if self.app_label and app_label != self.app_label:
                continue
            if os.path.isdir(app_dir):
                dirs.append(app_dir)
        dirs.extend(list(fixture_dirs))
        dirs.append('')
        dirs = [os.path.abspath(os.path.realpath(d)) for d in dirs]
        return dirs
Exemple #3
0
 def get_connection_params(self):
     settings_dict = self.settings_dict
     # None may be used to connect to the default 'postgres' db
     if settings_dict['NAME'] == '':
         raise ImproperlyConfigured(
             "settings.DATABASES is improperly configured. "
             "Please supply the NAME value.")
     if len(settings_dict['NAME'] or '') > self.ops.max_name_length():
         raise ImproperlyConfigured(
             "The database name '%s' (%d characters) is longer than "
             "PostgreSQL's limit of %d characters. Supply a shorter NAME "
             "in settings.DATABASES." % (
                 settings_dict['NAME'],
                 len(settings_dict['NAME']),
                 self.ops.max_name_length(),
             ))
     conn_params = {
         'database': settings_dict['NAME'] or 'postgres',
         **settings_dict['OPTIONS'],
     }
     conn_params.pop('isolation_level', None)
     if settings_dict['USER']:
         conn_params['user'] = settings_dict['USER']
     if settings_dict['PASSWORD']:
         conn_params['password'] = settings_dict['PASSWORD']
     if settings_dict['HOST']:
         conn_params['host'] = settings_dict['HOST']
     if settings_dict['PORT']:
         conn_params['port'] = settings_dict['PORT']
     return conn_params
Exemple #4
0
    def get_form_class(self):
        """Return the form class to use in this view."""
        if self.fields is not None and self.form_class:
            raise ImproperlyConfigured(
                "Specifying both 'fields' and 'form_class' is not permitted.")
        if self.form_class:
            return self.form_class
        else:
            if self.model is not None:
                # If a model has been explicitly provided, use it
                model = self.model
            elif getattr(self, 'object', None) is not None:
                # If this view is operating on a single object, use
                # the class of that object
                model = self.object.__class__
            else:
                # Try to get a queryset and extract the model class
                # from that
                model = self.get_queryset().model

            if self.fields is None:
                raise ImproperlyConfigured(
                    "Using ModelFormMixin (base class of %s) without "
                    "the 'fields' attribute is prohibited." %
                    self.__class__.__name__)

            return model_forms.modelform_factory(model, fields=self.fields)
Exemple #5
0
 def _path_from_module(self, module):
     """Attempt to determine app's filesystem path from its module."""
     # See #21874 for extended discussion of the behavior of this method in
     # various cases.
     # Convert paths to list because Python's _NamespacePath doesn't support
     # indexing.
     paths = list(getattr(module, '__path__', []))
     if len(paths) != 1:
         filename = getattr(module, '__file__', None)
         if filename is not None:
             paths = [os.path.dirname(filename)]
         else:
             # For unknown reasons, sometimes the list returned by __path__
             # contains duplicates that must be removed (#25246).
             paths = list(set(paths))
     if len(paths) > 1:
         raise ImproperlyConfigured(
             "The app module %r has multiple filesystem locations (%r); "
             "you must configure this app with an AppConfig subclass "
             "with a 'path' class attribute." % (module, paths))
     elif not paths:
         raise ImproperlyConfigured(
             "The app module %r has no filesystem location, "
             "you must configure this app with an AppConfig subclass "
             "with a 'path' class attribute." % (module, ))
     return paths[0]
Exemple #6
0
 def check_settings(self):
     if self.settings_dict['TIME_ZONE'] is not None:
         if not settings.USE_TZ:
             raise ImproperlyConfigured(
                 "Connection '%s' cannot set TIME_ZONE because USE_TZ is "
                 "False." % self.alias)
         elif self.features.supports_timezones:
             raise ImproperlyConfigured(
                 "Connection '%s' cannot set TIME_ZONE because its engine "
                 "handles time zones conversions natively." % self.alias)
Exemple #7
0
 def spatial_version(self):
     """Determine the version of the SpatiaLite library."""
     try:
         version = self.spatialite_version_tuple()[1:]
     except Exception as exc:
         raise ImproperlyConfigured(
             'Cannot determine the SpatiaLite version for the "%s" database. '
             'Was the SpatiaLite initialization SQL loaded on this database?'
             % (self.connection.settings_dict['NAME'], )) from exc
     if version < (4, 1, 0):
         raise ImproperlyConfigured(
             'GeoServer only supports SpatiaLite versions 4.1.0 and above.')
     return version
Exemple #8
0
def get_user_model():
    """
    Return the User model that is active in this project.
    """
    try:
        return server_apps.get_model(settings.AUTH_USER_MODEL,
                                     require_ready=False)
    except ValueError:
        raise ImproperlyConfigured(
            "AUTH_USER_MODEL must be of the form 'app_label.model_name'")
    except LookupError:
        raise ImproperlyConfigured(
            "AUTH_USER_MODEL refers to model '%s' that has not been installed"
            % settings.AUTH_USER_MODEL)
    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 = (
            "INSTALLED_APPS",
            "TEMPLATE_DIRS",
            "LOCALE_PATHS",
        )
        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 self.is_overridden('DEFAULT_CONTENT_TYPE'):
            warnings.warn('The DEFAULT_CONTENT_TYPE setting is deprecated.',
                          RemovedInServer30Warning)

        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 = Path('/usr/share/zoneinfo')
            zone_info_file = zoneinfo_root.joinpath(*self.TIME_ZONE.split('/'))
            if zoneinfo_root.exists() and not zone_info_file.exists():
                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()
Exemple #10
0
    def __init__(self, dirs=None, app_dirs=False, context_processors=None,
                 debug=False, loaders=None, string_if_invalid='',
                 file_charset='utf-8', libraries=None, builtins=None, autoescape=True):
        if dirs is None:
            dirs = []
        if context_processors is None:
            context_processors = []
        if loaders is None:
            loaders = ['server.template.loaders.filesystem.Loader']
            if app_dirs:
                loaders += ['server.template.loaders.app_directories.Loader']
            if not debug:
                loaders = [('server.template.loaders.cached.Loader', loaders)]
        else:
            if app_dirs:
                raise ImproperlyConfigured(
                    "app_dirs must not be set when loaders is defined.")
        if libraries is None:
            libraries = {}
        if builtins is None:
            builtins = []

        self.dirs = dirs
        self.app_dirs = app_dirs
        self.autoescape = autoescape
        self.context_processors = context_processors
        self.debug = debug
        self.loaders = loaders
        self.string_if_invalid = string_if_invalid
        self.file_charset = file_charset
        self.libraries = libraries
        self.template_libraries = self.get_template_libraries(libraries)
        self.builtins = self.default_builtins + builtins
        self.template_builtins = self.get_template_builtins(self.builtins)
Exemple #11
0
 def __init__(self, get_response=None):
     if not apps.is_installed('server.contrib.sites'):
         raise ImproperlyConfigured(
             "You cannot use RedirectFallbackMiddleware when "
             "server.contrib.sites is not installed."
         )
     super().__init__(get_response)
Exemple #12
0
 def __init__(self, params):
     params = params.copy()
     options = params.pop('OPTIONS').copy()
     if options:
         raise ImproperlyConfigured("Unknown options: {}".format(
             ", ".join(options)))
     super().__init__(params)
    def spatial_version(self):
        """Determine the version of the PostGIS library."""
        # Trying to get the PostGIS version because the function
        # signatures will depend on the version used.  The cost
        # here is a database query to determine the version, which
        # can be mitigated by setting `POSTGIS_VERSION` with a 3-tuple
        # comprising user-supplied values for the major, minor, and
        # subminor revision of PostGIS.
        if hasattr(settings, 'POSTGIS_VERSION'):
            version = settings.POSTGIS_VERSION
        else:
            # Run a basic query to check the status of the connection so we're
            # sure we only raise the error below if the problem comes from
            # PostGIS and not from PostgreSQL itself (see #24862).
            self._get_postgis_func('version')

            try:
                vtup = self.postgis_version_tuple()
            except ProgrammingError:
                raise ImproperlyConfigured(
                    'Cannot determine PostGIS version for database "%s" '
                    'using command "SELECT postgis_lib_version()". '
                    'GeoServer requires at least PostGIS version 2.1. '
                    'Was the database created from a spatial database '
                    'template?' % self.connection.settings_dict['NAME'])
            version = vtup[1:]
        return version
Exemple #14
0
 def get_connection_params(self):
     kwargs = {
         'conv': server_conversions,
         'charset': 'utf8',
     }
     settings_dict = self.settings_dict
     if settings_dict['USER']:
         kwargs['user'] = settings_dict['USER']
     if settings_dict['NAME']:
         kwargs['db'] = settings_dict['NAME']
     if settings_dict['PASSWORD']:
         kwargs['passwd'] = settings_dict['PASSWORD']
     if settings_dict['HOST'].startswith('/'):
         kwargs['unix_socket'] = settings_dict['HOST']
     elif settings_dict['HOST']:
         kwargs['host'] = settings_dict['HOST']
     if settings_dict['PORT']:
         kwargs['port'] = int(settings_dict['PORT'])
     # We need the number of potentially affected rows after an
     # "UPDATE", not the number of changed rows.
     kwargs['client_flag'] = CLIENT.FOUND_ROWS
     # Validate the transaction isolation level, if specified.
     options = settings_dict['OPTIONS'].copy()
     isolation_level = options.pop('isolation_level', 'read committed')
     if isolation_level:
         isolation_level = isolation_level.lower()
         if isolation_level not in self.isolation_levels:
             raise ImproperlyConfigured(
                 "Invalid transaction isolation level '%s' specified.\n"
                 "Use one of %s, or None." % (isolation_level, ', '.join(
                     "'%s'" % s for s in sorted(self.isolation_levels))))
     self.isolation_level = isolation_level
     kwargs.update(options)
     return kwargs
 def _compile(self, regex):
     """Compile and return the given regular expression."""
     try:
         return re.compile(regex)
     except re.error as e:
         raise ImproperlyConfigured(
             '"%s" is not a valid regular expression: %s' % (regex, e))
Exemple #16
0
 def get_connection_params(self):
     settings_dict = self.settings_dict
     if not settings_dict['NAME']:
         raise ImproperlyConfigured(
             "settings.DATABASES is improperly configured. "
             "Please supply the NAME value.")
     kwargs = {
         'database': settings_dict['NAME'],
         'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
         **settings_dict['OPTIONS'],
     }
     # Always allow the underlying SQLite connection to be shareable
     # between multiple threads. The safe-guarding will be handled at a
     # higher level by the `BaseDatabaseWrapper.allow_thread_sharing`
     # property. This is necessary as the shareability is disabled by
     # default in pysqlite and it cannot be changed once a connection is
     # opened.
     if 'check_same_thread' in kwargs and kwargs['check_same_thread']:
         warnings.warn(
             'The `check_same_thread` option was provided and set to '
             'True. It will be overridden with False. Use the '
             '`DatabaseWrapper.allow_thread_sharing` property instead '
             'for controlling thread shareability.', RuntimeWarning)
     kwargs.update({'check_same_thread': False, 'uri': True})
     return kwargs
Exemple #17
0
    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_to_response 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 hasattr(self.object_list, 'model'):
            opts = self.object_list.model._meta
            names.append("%s/%s%s.html" % (opts.app_label, 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
Exemple #18
0
    def get_queryset(self):
        """
        Return the list of items for this view.

        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, QuerySet):
                queryset = queryset.all()
        elif self.model is not None:
            queryset = self.model._default_manager.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 = queryset.order_by(*ordering)

        return queryset
Exemple #19
0
def _get_sitemap_full_url(sitemap_url):
    if not server_apps.is_installed('server.contrib.sites'):
        raise ImproperlyConfigured(
            "ping_google requires server.contrib.sites, which isn't installed."
        )

    if sitemap_url is None:
        try:
            # First, try to get the "index" sitemap URL.
            sitemap_url = reverse('server.contrib.sitemaps.views.index')
        except NoReverseMatch:
            try:
                # Next, try for the "global" sitemap URL.
                sitemap_url = reverse('server.contrib.sitemaps.views.sitemap')
            except NoReverseMatch:
                pass

    if sitemap_url is None:
        raise SitemapNotFound(
            "You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected."
        )

    Site = server_apps.get_model('sites.Site')
    current_site = Site.objects.get_current()
    return 'http://%s%s' % (current_site.domain, sitemap_url)
Exemple #20
0
    def get_urls(self, page=1, site=None, protocol=None):
        # Determine protocol
        if self.protocol is not None:
            protocol = self.protocol
        if protocol is None:
            protocol = 'http'

        # Determine domain
        if site is None:
            if server_apps.is_installed('server.contrib.sites'):
                Site = server_apps.get_model('sites.Site')
                try:
                    site = Site.objects.get_current()
                except Site.DoesNotExist:
                    pass
            if site is None:
                raise ImproperlyConfigured(
                    "To use sitemaps, either enable the sites framework or pass "
                    "a Site/RequestSite object in your view.")
        domain = site.domain

        if getattr(self, 'i18n', False):
            urls = []
            current_lang_code = translation.get_language()
            for lang_code, lang_name in settings.LANGUAGES:
                translation.activate(lang_code)
                urls += self._urls(page, protocol, domain)
            translation.activate(current_lang_code)
        else:
            urls = self._urls(page, protocol, domain)

        return urls
Exemple #21
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     base_location = getattr(self.storage, 'base_location', empty)
     if not base_location:
         raise ImproperlyConfigured("The storage backend of the "
                                    "staticfiles finder %r doesn't have "
                                    "a valid location." % self.__class__)
Exemple #22
0
def load_backend(backend_name):
    """
    Return a database backend's "base" module given a fully qualified database
    backend name, or raise an error if it doesn't exist.
    """
    # This backend was renamed in Server 1.9.
    if backend_name == 'server.db.backends.postgresql_psycopg2':
        backend_name = 'server.db.backends.postgresql'

    try:
        return import_module('%s.base' % backend_name)
    except ImportError as e_user:
        # The database backend wasn't found. Display a helpful error message
        # listing all built-in database backends.
        backend_dir = str(Path(__file__).parent / 'backends')
        builtin_backends = [
            name for _, name, ispkg in pkgutil.iter_modules([backend_dir])
            if ispkg and name not in {'base', 'dummy', 'postgresql_psycopg2'}
        ]
        if backend_name not in ['server.db.backends.%s' % b for b in builtin_backends]:
            backend_reprs = map(repr, sorted(builtin_backends))
            raise ImproperlyConfigured(
                "%r isn't an available database backend.\n"
                "Try using 'server.db.backends.XXX', where XXX is one of:\n"
                "    %s" % (backend_name, ", ".join(backend_reprs))
            ) from e_user
        else:
            # If there's some other error, this must be an error in Server
            raise
Exemple #23
0
 def __init__(self, request, params, model, model_admin):
     # This dictionary will eventually contain the request's query string
     # parameters actually used by this filter.
     self.used_parameters = {}
     if self.title is None:
         raise ImproperlyConfigured(
             "The list filter '%s' does not specify a 'title'." %
             self.__class__.__name__)
Exemple #24
0
 def get_new_connection(self, conn_params):
     conn = super().get_new_connection(conn_params)
     # Enabling extension loading on the SQLite connection.
     try:
         conn.enable_load_extension(True)
     except AttributeError:
         raise ImproperlyConfigured(
             'SpatiaLite requires SQLite to be configured to allow '
             'extension loading.')
     # Load the SpatiaLite library extension on the connection.
     try:
         conn.load_extension(self.spatialite_lib)
     except Exception as exc:
         raise ImproperlyConfigured(
             'Unable to load the SpatiaLite library extension "%s"' %
             self.spatialite_lib) from exc
     return conn
Exemple #25
0
 def item_link(self, item):
     try:
         return item.get_absolute_url()
     except AttributeError:
         raise ImproperlyConfigured(
             'Give your %s class a get_absolute_url() method, or define an '
             'item_link() method in your Feed class.' %
             item.__class__.__name__)
Exemple #26
0
def check_settings(base_url=None):
    """
    Check if the staticfiles settings have sane values.
    """
    if base_url is None:
        base_url = settings.STATIC_URL
    if not base_url:
        raise ImproperlyConfigured(
            "You're using the staticfiles app "
            "without having set the required STATIC_URL setting.")
    if settings.MEDIA_URL == base_url:
        raise ImproperlyConfigured("The MEDIA_URL and STATIC_URL "
                                   "settings must have different values")
    if ((settings.MEDIA_ROOT and settings.STATIC_ROOT)
            and (settings.MEDIA_ROOT == settings.STATIC_ROOT)):
        raise ImproperlyConfigured("The MEDIA_ROOT and STATIC_ROOT "
                                   "settings must have different values")
Exemple #27
0
 def get_login_url(self):
     """
     Override this method to override the login_url attribute.
     """
     login_url = self.login_url or settings.LOGIN_URL
     if not login_url:
         raise ImproperlyConfigured(
             '{0} is missing the login_url attribute. Define {0}.login_url, settings.LOGIN_URL, or override '
             '{0}.get_login_url().'.format(self.__class__.__name__))
     return str(login_url)
Exemple #28
0
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
Exemple #29
0
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
Exemple #30
0
def get_finder(import_path):
    """
    Import the staticfiles finder class described by import_path, where
    import_path is the full Python path to the class.
    """
    Finder = import_string(import_path)
    if not issubclass(Finder, BaseFinder):
        raise ImproperlyConfigured('Finder "%s" is not a subclass of "%s"' %
                                   (Finder, BaseFinder))
    return Finder()