Beispiel #1
0
    def _apply_geo_filters(self, request, queryset):
        # Strictly speaking these are not queries that should be possible with a GeoReport v2
        # core implementation, but as they do not require extra data in the models, it's worth it
        # to have them available "for free".
        bbox = request.query_params.get('bbox')
        if bbox:
            bbox = parse_bbox(bbox)
            (long1, lat1), (long2, lat2) = bbox
            queryset = queryset.filter(lat__range=(lat1, lat2))
            queryset = queryset.filter(long__range=(long1, long2))

        lat = request.query_params.get('lat')
        lon = request.query_params.get('long')
        radius = request.query_params.get('radius')
        if lat and lon and radius:
            try:
                lat = float(lat)
                lon = float(lon)
                radius = float(radius)
            except ValueError:
                raise APIException('lat/lon/radius must all be valid decimal numbers')
            if not determine_gissiness():
                raise APIException('this installation is not capable of lat/lon/radius queries')
            from django.contrib.gis.db.models.functions import Distance
            from django.contrib.gis.geos import Point
            from django.contrib.gis.measure import D
            point = Point(x=lon, y=lat, srid=4326)
            queryset = queryset.annotate(distance=Distance('location', point))
            queryset = queryset.filter(location__distance_lte=(point, D(m=radius)))
        return queryset
Beispiel #2
0
    def _apply_geo_filters(self, request, queryset):
        # Strictly speaking these are not queries that should be possible with a GeoReport v2
        # core implementation, but as they do not require extra data in the models, it's worth it
        # to have them available "for free".
        bbox = request.query_params.get('bbox')
        if bbox:
            bbox = parse_bbox(bbox)
            (long1, lat1), (long2, lat2) = bbox
            queryset = queryset.filter(lat__range=(lat1, lat2))
            queryset = queryset.filter(long__range=(long1, long2))

        lat = request.query_params.get('lat')
        lon = request.query_params.get('long')
        radius = request.query_params.get('radius')
        if lat and lon and radius:
            try:
                lat = float(lat)
                lon = float(lon)
                radius = float(radius)
            except ValueError:
                raise APIException('lat/lon/radius must all be valid decimal numbers')
            if not determine_gissiness():
                raise APIException('this installation is not capable of lat/lon/radius queries')
            from django.contrib.gis.db.models.functions import Distance
            from django.contrib.gis.geos import Point
            from django.contrib.gis.measure import D
            point = Point(x=lon, y=lat, srid=4326)
            queryset = queryset.annotate(distance=Distance('location', point))
            queryset = queryset.filter(location__distance_lte=(point, D(m=radius)))
        return queryset
Beispiel #3
0
def test_gissiness():
    """
    Test that determine_gissiness() correctly determines gissiness for known databases.
    :return:
    """
    engine = settings.DATABASES['default']['ENGINE']
    if engine not in GISSY:
        pytest.skip('I have no idea whether %s should be gissy' % engine)
    assert GISSY[engine] == determine_gissiness()
Beispiel #4
0
def test_GeoPointField_heritage():
    assert issubclass(GeoPointField, Field)  # Well that's a given.
    assert issubclass(
        GeoPointField,
        GeometryField) == determine_gissiness()  # Only geometrical if gissy!
Beispiel #5
0
 def ready(self):
     if not determine_gissiness():  # pragma: no cover
         raise ImproperlyConfigured('the geometry extension requires a GIS-enabled database')
Beispiel #6
0
 def ready(self):
     if not determine_gissiness():  # pragma: no cover
         raise ImproperlyConfigured(
             'the geometry extension requires a GIS-enabled database')
import pytest

from issues.gis import determine_gissiness
from issues.models import Issue
from issues.tests.schemata import LIST_OF_ISSUES_SCHEMA
from issues.tests.utils import ISSUE_LIST_ENDPOINT, get_data_from_response, verify_issue

GISSY = determine_gissiness()


def test_get_requests(testing_issues, mf_api_client):
    content = get_data_from_response(mf_api_client.get(ISSUE_LIST_ENDPOINT),
                                     schema=LIST_OF_ISSUES_SCHEMA)
    assert len(content) == Issue.objects.count()


def test_get_by_service_request_id(testing_issues, mf_api_client):
    content = get_data_from_response(mf_api_client.get(
        ISSUE_LIST_ENDPOINT, {'service_request_id': '1982hglaqe8pdnpophff'}),
                                     schema=LIST_OF_ISSUES_SCHEMA)
    assert len(content) == 1
    verify_issue(content[0])


def test_get_by_service_request_ids(testing_issues, mf_api_client):
    content = get_data_from_response(mf_api_client.get(
        ISSUE_LIST_ENDPOINT,
        {'service_request_id': '1982hglaqe8pdnpophff,2981hglaqe8pdnpoiuyt'}),
                                     schema=LIST_OF_ISSUES_SCHEMA)
    assert set(c['service_request_id'] for c in content) == {
        '1982hglaqe8pdnpophff', '2981hglaqe8pdnpoiuyt'
Beispiel #8
0
def test_GeoPointField_heritage():
    assert issubclass(GeoPointField, Field)  # Well that's a given.
    assert issubclass(GeoPointField, GeometryField) == determine_gissiness()  # Only geometrical if gissy!
Beispiel #9
0
    def to_python(self, value):
        if (Point and isinstance(value, Point)) or isinstance(
                value, (list, tuple)):
            value = "%s;%s" % (value[0], value[1])

        return self.from_db_value(value, None, None, None)

    def get_prep_value(self, value):
        if not value:
            return None
        if isinstance(value, string_types):
            lat, lng = _string_value_to_coords(value)
        else:
            lat, lng = [float(c) for c in value]
        return "%r;%r" % (lat, lng)


if determine_gissiness():
    from django.contrib.gis.db.models.fields import PointField as _GISPointField

    class GeoPointField(_GISPointField):
        def __init__(self, verbose_name=None, dim=2, **kwargs):
            kwargs['geography'] = True
            kwargs['srid'] = 4326
            super(GeoPointField, self).__init__(verbose_name, dim, **kwargs)
else:

    class GeoPointField(GeoPointFieldFallback):
        pass
Beispiel #10
0
        latlng = _string_value_to_coords(value)
        return latlng

    def to_python(self, value):
        if (Point and isinstance(value, Point)) or isinstance(value, (list, tuple)):
            value = "%s;%s" % (value[0], value[1])

        return self.from_db_value(value, None, None, None)

    def get_prep_value(self, value):
        if not value:
            return None
        if isinstance(value, string_types):
            lat, lng = _string_value_to_coords(value)
        else:
            lat, lng = [float(c) for c in value]
        return "%r;%r" % (lat, lng)


if determine_gissiness():
    from django.contrib.gis.db.models.fields import PointField as _GISPointField

    class GeoPointField(_GISPointField):
        def __init__(self, verbose_name=None, dim=2, **kwargs):
            kwargs['geography'] = True
            kwargs['srid'] = 4326
            super(GeoPointField, self).__init__(verbose_name, dim, **kwargs)
else:
    class GeoPointField(GeoPointFieldFallback):
        pass
Beispiel #11
0
import pytest

from issues.gis import determine_gissiness
from issues.models import Issue
from issues.tests.schemata import LIST_OF_ISSUES_SCHEMA
from issues.tests.utils import ISSUE_LIST_ENDPOINT, get_data_from_response, verify_issue

GISSY = determine_gissiness()


def test_get_requests(testing_issues, mf_api_client):
    content = get_data_from_response(mf_api_client.get(ISSUE_LIST_ENDPOINT), schema=LIST_OF_ISSUES_SCHEMA)
    assert len(content) == Issue.objects.count()


def test_get_by_service_request_id(testing_issues, mf_api_client):
    content = get_data_from_response(
        mf_api_client.get(ISSUE_LIST_ENDPOINT, {'service_request_id': '1982hglaqe8pdnpophff'}),
        schema=LIST_OF_ISSUES_SCHEMA
    )
    assert len(content) == 1
    verify_issue(content[0])


def test_get_by_service_request_ids(testing_issues, mf_api_client):
    content = get_data_from_response(
        mf_api_client.get(
            ISSUE_LIST_ENDPOINT,
            {'service_request_id': '1982hglaqe8pdnpophff,2981hglaqe8pdnpoiuyt'}
        ),
        schema=LIST_OF_ISSUES_SCHEMA