Browse Source

Fixed #29484 -- Removed the need to specify SPATIALITE_LIBRARY_PATH with Spatialite 4.2+.

Thanks Tim Graham for the review.
Claude Paroz 6 years ago
parent
commit
f3836144db

+ 22 - 12
django/contrib/gis/db/backends/spatialite/base.py

@@ -27,13 +27,12 @@ class DatabaseWrapper(SQLiteDatabaseWrapper):
         # (`libspatialite`). If it's not in the system library path (e.g., it
         # cannot be found by `ctypes.util.find_library`), then it may be set
         # manually in the settings via the `SPATIALITE_LIBRARY_PATH` setting.
-        self.spatialite_lib = getattr(settings, 'SPATIALITE_LIBRARY_PATH',
-                                      find_library('spatialite'))
-        if not self.spatialite_lib:
-            raise ImproperlyConfigured('Unable to locate the SpatiaLite library. '
-                                       'Make sure it is in your library path, or set '
-                                       'SPATIALITE_LIBRARY_PATH in your settings.'
-                                       )
+        self.lib_spatialite_paths = [name for name in [
+            getattr(settings, 'SPATIALITE_LIBRARY_PATH', None),
+            'mod_spatialite.so',
+            'mod_spatialite',
+            find_library('spatialite'),
+        ] if name is not None]
         super().__init__(*args, **kwargs)
 
     def get_new_connection(self, conn_params):
@@ -47,12 +46,23 @@ class DatabaseWrapper(SQLiteDatabaseWrapper):
                 'extension loading.'
             )
         # Load the SpatiaLite library extension on the connection.
-        try:
-            conn.load_extension(self.spatialite_lib)
-        except Exception as exc:
+        for path in self.lib_spatialite_paths:
+            try:
+                conn.load_extension(path)
+            except Exception:
+                if getattr(settings, 'SPATIALITE_LIBRARY_PATH', None):
+                    raise ImproperlyConfigured(
+                        'Unable to load the SpatiaLite library extension '
+                        'as specified in your SPATIALITE_LIBRARY_PATH setting.'
+                    )
+                continue
+            else:
+                break
+        else:
             raise ImproperlyConfigured(
-                'Unable to load the SpatiaLite library extension "%s"' % self.spatialite_lib
-            ) from exc
+                'Unable to load the SpatiaLite library extension. '
+                'Library names tried: %s' % ', '.join(self.lib_spatialite_paths)
+            )
         return conn
 
     def prepare_database(self):

+ 0 - 6
docs/ref/contrib/gis/install/spatialite.txt

@@ -21,12 +21,6 @@ In any case, you should always be able to :ref:`install from source
 __ https://www.gaia-gis.it/fossil/libspatialite
 __ https://www.gaia-gis.it/gaia-sins/
 
-.. admonition:: ``SPATIALITE_LIBRARY_PATH`` setting required for SpatiaLite 4.2+
-
-    If you're using SpatiaLite 4.2+, you must put this in your ``settings.py``::
-
-        SPATIALITE_LIBRARY_PATH = 'mod_spatialite'
-
 .. _spatialite_source:
 
 Installing from source