Skip to content

Add GeoDjango support #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pathlib
import sys

from django.core.exceptions import ImproperlyConfigured

test_apps = [
"admin_changelist",
"admin_checks",
Expand Down Expand Up @@ -157,6 +159,15 @@
]
),
]

try:
from django.contrib.gis.gdal import GDAL_VERSION # noqa: F401
except ImproperlyConfigured:
# GDAL not installed.
pass
else:
test_apps.append("gis_tests")

runtests = pathlib.Path(__file__).parent.resolve() / "runtests.py"
run_tests_cmd = f"python3 {runtests} %s --settings mongodb_settings -v 2"

Expand Down
56 changes: 56 additions & 0 deletions .github/workflows/test-python-atlas-geo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Python Tests on Atlas

on:
pull_request:
paths:
- '**.py'
- '!setup.py'
- '.github/workflows/test-python-atlas.yml'
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

defaults:
run:
shell: bash -eux {0}

jobs:
build:
name: Django Test Suite
runs-on: ubuntu-latest
steps:
- name: Checkout django-mongodb-backend
uses: actions/checkout@v4
with:
persist-credentials: false
- name: install django-mongodb-backend
run: |
pip3 install --upgrade pip
pip3 install -e .
- name: Checkout Django
uses: actions/checkout@v4
with:
repository: 'mongodb-forks/django'
ref: 'mongogis'
path: 'django_repo'
persist-credentials: false
- name: Install system packages for Django's Python test dependencies
run: |
sudo apt-get update
sudo apt-get install gdal-bin libmemcached-dev
- name: Install Django and its Python test dependencies
run: |
cd django_repo/tests/
pip3 install -e ..
pip3 install -r requirements/py3.txt
- name: Copy the test settings file
run: cp .github/workflows/mongodb_settings.py django_repo/tests/
- name: Copy the test runner file
run: cp .github/workflows/runtests.py django_repo/tests/runtests_.py
- name: Start local Atlas
working-directory: .
run: bash .github/workflows/start_local_atlas.sh mongodb/mongodb-atlas-local:7
- name: Run tests
run: python3 django_repo/tests/runtests.py --settings mongodb_settings -v 2
Comment on lines +21 to +56

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium test

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}
2 changes: 1 addition & 1 deletion .github/workflows/test-python-atlas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: 'mongodb-forks/django'
ref: 'mongodb-5.2.x'
ref: 'mongogis'
path: 'django_repo'
persist-credentials: false
- name: Install system packages for Django's Python test dependencies
Expand Down
57 changes: 57 additions & 0 deletions .github/workflows/test-python-geo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Python Tests with GeoDjango

on:
pull_request:
paths:
- '**.py'
- '!setup.py'
- '.github/workflows/test-python.yml'
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

defaults:
run:
shell: bash -eux {0}

jobs:
build:
name: Django Test Suite
runs-on: ubuntu-latest
steps:
- name: Checkout django-mongodb-backend
uses: actions/checkout@v4
with:
persist-credentials: false
- name: install django-mongodb-backend
run: |
pip3 install --upgrade pip
pip3 install -e .
- name: Checkout Django
uses: actions/checkout@v4
with:
repository: 'mongodb-forks/django'
ref: 'mongogis'
path: 'django_repo'
persist-credentials: false
- name: Install system packages for Django's Python test dependencies
run: |
sudo apt-get update
sudo apt-get install gdal-bin libmemcached-dev
- name: Install Django and its Python test dependencies
run: |
cd django_repo/tests/
pip3 install -e ..
pip3 install -r requirements/py3.txt
- name: Copy the test settings file
run: cp .github/workflows/mongodb_settings.py django_repo/tests/
- name: Copy the test runner file
run: cp .github/workflows/runtests.py django_repo/tests/runtests_.py
- name: Start MongoDB
uses: supercharge/mongodb-github-action@90004df786821b6308fb02299e5835d0dae05d0d # 1.12.0
with:
mongodb-version: 6.0
- name: Run tests
run: python3 django_repo/tests/runtests_.py
Comment on lines +21 to +57

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium test

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}
2 changes: 1 addition & 1 deletion .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: 'mongodb-forks/django'
ref: 'mongodb-5.2.x'
ref: 'mongogis'
path: 'django_repo'
persist-credentials: false
- name: Install system packages for Django's Python test dependencies
Expand Down
18 changes: 16 additions & 2 deletions django_mongodb_backend/features.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from django.core.exceptions import ImproperlyConfigured
from django.db.backends.base.features import BaseDatabaseFeatures
from django.utils.functional import cached_property
from pymongo.errors import OperationFailure

try:
from .gis.features import GISFeatures
except ImproperlyConfigured:
# GIS libraries not installed
class GISFeatures:
pass

class DatabaseFeatures(BaseDatabaseFeatures):

class DatabaseFeatures(GISFeatures, BaseDatabaseFeatures):
minimum_database_version = (6, 0)
allow_sliced_subqueries_with_in = False
allows_multiple_constraints_on_same_fields = False
Expand Down Expand Up @@ -135,7 +143,7 @@ def django_test_expected_failures(self):
expected_failures.update(self._django_test_expected_failures_no_transactions)
return expected_failures

django_test_skips = {
_django_test_skips = {
"Database defaults aren't supported by MongoDB.": {
# bson.errors.InvalidDocument: cannot encode object:
# <django.db.models.expressions.DatabaseDefault
Expand Down Expand Up @@ -588,6 +596,12 @@ def django_test_expected_failures(self):
},
}

@cached_property
def django_test_skips(self):
skips = super().django_test_skips
skips.update(self._django_test_skips)
return skips

@cached_property
def is_mongodb_6_3(self):
return self.connection.get_database_version() >= (6, 3)
Expand Down
9 changes: 9 additions & 0 deletions django_mongodb_backend/gis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.core.exceptions import ImproperlyConfigured

try:
from .lookups import register_lookups
except ImproperlyConfigured:
# GIS libraries not installed
pass
else:
register_lookups()
21 changes: 21 additions & 0 deletions django_mongodb_backend/gis/adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import collections


class Adapter(collections.UserDict):
def __init__(self, obj, geography=False):
"""
Initialize on the spatial object.
"""
if obj.__class__.__name__ == "GeometryCollection":
self.data = {
"type": obj.__class__.__name__,
"geometries": [self.get_data(x) for x in obj],
}
else:
self.data = self.get_data(obj)

def get_data(self, obj):
return {
"type": obj.__class__.__name__,
"coordinates": obj.coords,
}
65 changes: 65 additions & 0 deletions django_mongodb_backend/gis/features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
from django.utils.functional import cached_property


class GISFeatures(BaseSpatialFeatures):
has_spatialrefsys_table = False
supports_transform = False

@cached_property
def django_test_expected_failures(self):
expected_failures = super().django_test_expected_failures
expected_failures.update(
{
# SRIDs aren't populated: AssertionError: 4326 != None
# self.assertEqual(4326, nullcity.point.srid)
"gis_tests.geoapp.tests.GeoModelTest.test_proxy",
# MongoDB does not support the within lookup
"gis_tests.relatedapp.tests.RelatedGeoModelTest.test06_f_expressions",
"gis_tests.geoapp.tests.GeoModelTest.test_gis_query_as_string",
# 'Adapter' object has no attribute 'srid'
"gis_tests.geoapp.test_expressions.GeoExpressionsTests.test_geometry_value_annotation",
# Object of type ObjectId is not JSON serializable
"gis_tests.geoapp.test_serializers.GeoJSONSerializerTests.test_fields_option",
"gis_tests.geoapp.test_serializers.GeoJSONSerializerTests.test_geometry_field_option",
"gis_tests.geoapp.test_serializers.GeoJSONSerializerTests.test_serialization_base",
"gis_tests.geoapp.test_serializers.GeoJSONSerializerTests.test_srid_option",
# No lookups are supported (yet?)
"gis_tests.geoapp.tests.GeoLookupTest.test_gis_lookups_with_complex_expressions",
}
)
return expected_failures

@cached_property
def django_test_skips(self):
skips = super().django_test_skips
skips.update(
{
"inspectdb not supported.": {
"gis_tests.inspectapp.tests.InspectDbTests",
},
"Raw SQL not supported": {
"gis_tests.geoapp.tests.GeoModelTest.test_raw_sql_query",
},
"MongoDB doesn't support the SRID(s) used in this test.": {
# Error messages:
# - Can't extract geo keys
# - Longitude/latitude is out of bounds
"gis_tests.geoapp.test_expressions.GeoExpressionsTests.test_update_from_other_field",
"gis_tests.layermap.tests.LayerMapTest.test_encoded_name",
# SouthTexasCity fixture objects use SRID 2278 which is ignored
# by the patched version of loaddata in the Django fork.
"gis_tests.distapp.tests.DistanceTest.test_init",
},
"ImproperlyConfigured isn't raised when using RasterField": {
# Normally RasterField.db_type() raises an error, but MongoDB
# migrations don't need to call it, so the check doesn't happen.
"gis_tests.gis_migrations.test_operations.NoRasterSupportTests",
},
"MongoDB doesn't support redundant spatial indexes.": {
# Error: Index already exists with a different name
"gis_tests.geoapp.test_indexes.SchemaIndexesTests.test_index_name",
},
},
)
return skips
10 changes: 10 additions & 0 deletions django_mongodb_backend/gis/lookups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.contrib.gis.db.models.lookups import GISLookup
from django.db import NotSupportedError


def gis_lookup(self, compiler, connection): # noqa: ARG001
raise NotSupportedError(f"MongoDB does not support the {self.lookup_name} lookup.")


def register_lookups():
GISLookup.as_mql = gis_lookup
Loading