Ticket #9664: layermapping_mysql.diff

File layermapping_mysql.diff, 5.3 KB (added by jbronn, 15 years ago)

Patch that enables support for LayerMapping with MySQL

  • django/contrib/gis/utils/layermapping.py

     
    116116    OGRException, OGRGeometry, OGRGeomType, SpatialReference
    117117from django.contrib.gis.gdal.field import \
    118118    OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString, OFTTime
    119 from django.contrib.gis.models import GeometryColumns, SpatialRefSys
    120119from django.db import models, transaction
    121120from django.contrib.localflavor.us.models import USStateField
    122121
     
    189188
    190189        # Getting the geometry column associated with the model (an
    191190        # exception will be raised if there is no geometry column).
    192         self.geo_col = self.geometry_column()
     191        if SpatialBackend.mysql:
     192            transform = False
     193        else:
     194            self.geo_col = self.geometry_column()
    193195
    194196        # Checking the source spatial reference system, and getting
    195197        # the coordinate transformation object (unless the `transform`
     
    327329
    328330    def check_srs(self, source_srs):
    329331        "Checks the compatibility of the given spatial reference object."
     332        from django.contrib.gis.models import SpatialRefSys
    330333        if isinstance(source_srs, SpatialReference):
    331334            sr = source_srs
    332335        elif isinstance(source_srs, SpatialRefSys):
     
    498501    #### Other model methods ####
    499502    def coord_transform(self):
    500503        "Returns the coordinate transformation object."
     504        from django.contrib.gis.models import SpatialRefSys
    501505        try:
    502506            # Getting the target spatial reference system
    503507            target_srs = SpatialRefSys.objects.get(srid=self.geo_col.srid).srs
     
    509513
    510514    def geometry_column(self):
    511515        "Returns the GeometryColumn model associated with the geographic column."
     516        from django.contrib.gis.models import GeometryColumns
    512517        # Getting the GeometryColumn object.
    513518        try:
    514519            db_table = self.model._meta.db_table
    515520            geo_col = self.geom_field
    516             if SpatialBackend.name == 'oracle':
     521            if SpatialBackend.oracle:
    517522                # Making upper case for Oracle.
    518523                db_table = db_table.upper()
    519524                geo_col = geo_col.upper()
  • django/contrib/gis/tests/__init__.py

     
    2929        elif postgis:
    3030            test_models += ['distapp', 'layermap', 'relatedapp']
    3131        elif mysql:
    32             test_models += ['relatedapp']
     32            test_models += ['relatedapp', 'layermap']
    3333
    3434        test_suite_names += [
    3535            'test_gdal_driver',
  • django/contrib/gis/tests/layermap/tests.py

     
    11import os, unittest
    22from copy import copy
    3 from datetime import date
    43from decimal import Decimal
    54from models import City, County, CountyFeat, Interstate, State, city_mapping, co_mapping, cofeat_mapping, inter_mapping
     5from django.contrib.gis.db.backend import SpatialBackend
    66from django.contrib.gis.utils.layermapping import LayerMapping, LayerMapError, InvalidDecimal, MissingForeignKey
    77from django.contrib.gis.gdal import DataSource
    88
     
    8585            lm = LayerMapping(Interstate, inter_shp, inter_mapping)
    8686            lm.save(silent=True, strict=True)
    8787        except InvalidDecimal:
    88             pass
     88            # No transactions for geoms on MySQL; delete added features.
     89            if SpatialBackend.mysql: Interstate.objects.all().delete()
    8990        else:
    9091            self.fail('Should have failed on strict import with invalid decimal values.')
    9192
     
    151152            self.assertRaises(e, LayerMapping, County, co_shp, co_mapping, transform=False, unique=arg)
    152153
    153154        # No source reference system defined in the shapefile, should raise an error.
    154         self.assertRaises(LayerMapError, LayerMapping, County, co_shp, co_mapping)
     155        if not SpatialBackend.mysql:
     156            self.assertRaises(LayerMapError, LayerMapping, County, co_shp, co_mapping)
    155157
    156158        # Passing in invalid ForeignKey mapping parameters -- must be a dictionary
    157159        # mapping for the model the ForeignKey points to.
     
    227229        lm.save(fid_range=slice(None, 1), silent=True, strict=True) # layer[:1]
    228230
    229231        # Only Pueblo & Honolulu counties should be present because of
    230         # the `unique` keyword.
    231         qs = County.objects.all()
     232        # the `unique` keyword.  Have to set `order_by` on this QuerySet
     233        # or else MySQL will return a different ordering than the other dbs.
     234        qs = County.objects.order_by('name')
    232235        self.assertEqual(2, qs.count())
    233236        hi, co = tuple(qs)
    234237        hi_idx, co_idx = tuple(map(NAMES.index, ('Honolulu', 'Pueblo')))
  • django/contrib/gis/tests/layermap/tests_mysql.py

     
     1from tests import *
Back to Top