From 1a0e0fd28e953136d63752363d4782e3883a833b Mon Sep 17 00:00:00 2001
From: Noah Seger <nosamanuel@gmail.com>
Date: Tue, 14 Feb 2012 17:33:13 -0600
Subject: [PATCH] Use router by default to get LayerMapping DB
---
django/contrib/gis/tests/layermap/routers.py | 12 ++++++++++++
django/contrib/gis/tests/layermap/tests.py | 19 +++++++++++++++++++
django/contrib/gis/utils/layermapping.py | 11 +++++++----
3 files changed, 38 insertions(+), 4 deletions(-)
create mode 100644 django/contrib/gis/tests/layermap/routers.py
diff --git a/django/contrib/gis/tests/layermap/routers.py b/django/contrib/gis/tests/layermap/routers.py
new file mode 100644
index 0000000..3f071ea
|
-
|
+
|
|
| | 1 | |
| | 2 | |
| | 3 | |
| | 4 | |
| | 5 | |
| | 6 | |
| | 7 | |
| | 8 | |
| | 9 | |
| | 10 | |
| | 11 | |
| | 12 | |
diff --git a/django/contrib/gis/tests/layermap/tests.py b/django/contrib/gis/tests/layermap/tests.py
index ef0e15e..cc5d2ca 100644
|
a
|
b
|
|
| 6 | 6 | |
| 7 | 7 | from django.utils.unittest import TestCase |
| 8 | 8 | |
| | 9 | |
| 9 | 10 | from django.contrib.gis.gdal import DataSource |
| 10 | 11 | from django.contrib.gis.tests.utils import mysql |
| 11 | 12 | from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey |
| … |
… |
|
| 26 | 27 | NUMS = [1, 2, 1, 19, 1] # Number of polygons for each. |
| 27 | 28 | STATES = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado'] |
| 28 | 29 | |
| | 30 | |
| | 31 | |
| 29 | 32 | class LayerMapTest(TestCase): |
| 30 | 33 | |
| 31 | 34 | def test01_init(self): |
| … |
… |
def test07_invalid_layer(self):
|
| 278 | 281 | lm = LayerMapping(Invalid, invalid_shp, invalid_mapping, |
| 279 | 282 | source_srs=4326) |
| 280 | 283 | lm.save(silent=True) |
| | 284 | |
| | 285 | |
| | 286 | |
| | 287 | |
| | 288 | |
| | 289 | |
| | 290 | |
| | 291 | |
| | 292 | |
| | 293 | |
| | 294 | |
| | 295 | |
| | 296 | |
| | 297 | |
| | 298 | |
| | 299 | |
diff --git a/django/contrib/gis/utils/layermapping.py b/django/contrib/gis/utils/layermapping.py
index 154617f..0d6d59d 100644
|
a
|
b
|
|
| 9 | 9 | import sys |
| 10 | 10 | from decimal import Decimal |
| 11 | 11 | from django.core.exceptions import ObjectDoesNotExist |
| 12 | | from django.db import connections, DEFAULT_DB_ALIAS |
| | 12 | from django.db import connections, |
| 13 | 13 | from django.contrib.gis.db.models import GeometryField |
| 14 | 14 | from django.contrib.gis.gdal import (CoordTransform, DataSource, |
| 15 | 15 | OGRException, OGRGeometry, OGRGeomType, SpatialReference) |
| … |
… |
class LayerMapping(object):
|
| 66 | 66 | def __init__(self, model, data, mapping, layer=0, |
| 67 | 67 | source_srs=None, encoding=None, |
| 68 | 68 | transaction_mode='commit_on_success', |
| 69 | | transform=True, unique=None, using=DEFAULT_DB_ALIAS): |
| | 69 | transform=True, unique=None, using=): |
| 70 | 70 | """ |
| 71 | 71 | A LayerMapping object is initialized using the given Model (not an instance), |
| 72 | 72 | a DataSource (or string path to an OGR-supported data file), and a mapping |
| … |
… |
def __init__(self, model, data, mapping, layer=0,
|
| 80 | 80 | self.ds = data |
| 81 | 81 | self.layer = self.ds[layer] |
| 82 | 82 | |
| 83 | | self.using = using |
| 84 | | self.spatial_backend = connections[using].ops |
| | 83 | if using is not None: |
| | 84 | self.using = using |
| | 85 | else: |
| | 86 | self.using = router.db_for_write(model) |
| | 87 | self.spatial_backend = connections[self.using].ops |
| 85 | 88 | |
| 86 | 89 | # Setting the mapping & model attributes. |
| 87 | 90 | self.mapping = mapping |