Fixed #24064 -- Prevented database access at compile time in spatialite models.

This commit is contained in:
Andriy Sokolovskiy 2015-01-02 16:37:54 +02:00 committed by Tim Graham
parent 23f1a8dad2
commit 839f431ef5
1 changed files with 17 additions and 6 deletions

View File

@ -2,7 +2,9 @@
The GeometryColumns and SpatialRefSys models for the SpatiaLite backend.
"""
from django.db import connection, models
from django.db.backends.signals import connection_created
from django.contrib.gis.db.backends.base import SpatialRefSysMixin
from django.contrib.gis.db.backends.spatialite.base import DatabaseWrapper
from django.utils.encoding import python_2_unicode_compatible
@ -13,10 +15,6 @@ class SpatialiteGeometryColumns(models.Model):
"""
f_table_name = models.CharField(max_length=256)
f_geometry_column = models.CharField(max_length=256)
if connection.ops.spatial_version[0] >= 4:
type = models.IntegerField(db_column='geometry_type')
else:
type = models.CharField(max_length=30)
coord_dimension = models.IntegerField()
srid = models.IntegerField(primary_key=True)
spatial_index_enabled = models.IntegerField()
@ -57,8 +55,6 @@ class SpatialiteSpatialRefSys(models.Model, SpatialRefSysMixin):
auth_srid = models.IntegerField()
ref_sys_name = models.CharField(max_length=256)
proj4text = models.CharField(max_length=2048)
if connection.ops.spatial_version[0] >= 4:
srtext = models.CharField(max_length=2048)
@property
def wkt(self):
@ -71,3 +67,18 @@ class SpatialiteSpatialRefSys(models.Model, SpatialRefSysMixin):
app_label = 'gis'
db_table = 'spatial_ref_sys'
managed = False
def add_spatial_version_related_fields(sender, **kwargs):
"""
Adds fields after establishing a database connection to prevent database
operations at compile time.
"""
if connection_created.disconnect(add_spatial_version_related_fields, sender=DatabaseWrapper):
spatial_version = connection.ops.spatial_version[0]
if spatial_version >= 4:
SpatialiteSpatialRefSys.add_to_class('srtext', models.CharField(max_length=2048))
SpatialiteGeometryColumns.add_to_class('type', models.IntegerField(db_column='geometry_type'))
else:
SpatialiteGeometryColumns.add_to_class('type', models.CharField(max_length=30))
connection_created.connect(add_spatial_version_related_fields, sender=DatabaseWrapper)