Skip to content

Commit

Permalink
[feature] Add geojson endpoints #360
Browse files Browse the repository at this point in the history
Closes #360
  • Loading branch information
purhan committed Jan 27, 2021
1 parent 670fc63 commit 5a22e0d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
56 changes: 56 additions & 0 deletions openwisp_controller/geo/api/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Count
from django.urls import reverse
from rest_framework import generics
from rest_framework.permissions import BasePermission
from rest_framework.serializers import IntegerField, SerializerMethodField
from rest_framework_gis import serializers as gis_serializers
from rest_framework_gis.pagination import GeoJsonPagination
from swapper import load_model

from openwisp_users.api.mixins import FilterByOrganizationManaged, FilterByParentManaged
from openwisp_utils.api.serializers import ValidatedModelSerializer

Device = load_model('config', 'Device')
Location = load_model('geo', 'Location')
DeviceLocation = load_model('geo', 'DeviceLocation')
Expand All @@ -22,6 +29,28 @@ class Meta:
read_only_fields = ('name',)


class DeviceSerializer(ValidatedModelSerializer):
admin_edit_url = SerializerMethodField('get_admin_edit_url')

def get_admin_edit_url(self, obj):
return self.context['request'].build_absolute_uri(
reverse(f'admin:{obj._meta.app_label}_device_change', args=(obj.id,))
)

class Meta:
model = Device
fields = '__all__'


class GeoJsonLocationSerializer(gis_serializers.GeoFeatureModelSerializer):
device_count = IntegerField()

class Meta:
model = Location
geo_field = 'geometry'
fields = '__all__'


class DeviceLocationView(generics.RetrieveUpdateAPIView):
serializer_class = LocationSerializer
permission_classes = (DevicePermission,)
Expand Down Expand Up @@ -58,4 +87,31 @@ def create_location(self, device):
return location


class GeoJsonLocationList(FilterByOrganizationManaged, generics.ListAPIView):
GeoJsonPagination.page_size = 1000
pagination_class = GeoJsonPagination
queryset = Location.objects.filter(devicelocation__isnull=False).annotate(
device_count=Count('devicelocation')
)
serializer_class = GeoJsonLocationSerializer


class LocationDeviceList(FilterByParentManaged, generics.ListAPIView):
serializer_class = DeviceSerializer
queryset = Device.objects.none()

def get_parent_queryset(self):
qs = Location.objects.filter(pk=self.kwargs['location_pk'])
return qs

def get_queryset(self):
super().get_queryset()
qs = Device.objects.filter(
devicelocation__location_id=self.kwargs['location_pk']
)
return qs


device_location = DeviceLocationView.as_view()
geojson = GeoJsonLocationList.as_view()
location_device_list = LocationDeviceList.as_view()
6 changes: 6 additions & 0 deletions openwisp_controller/geo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ def get_geo_urls(geo_views):
geo_views.device_location,
name='api_device_location',
),
url(r'^api/v1/device/geojson$', geo_views.geojson, name='api_geojson',),
url(
r'^api/v1/location/(?P<location_pk>[^/]+)/device/$',
geo_views.location_device_list,
name='api_location_device_list',
),
]
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ django-x509~=0.9.2
django-taggit~=1.3.0
django-loci~=0.4.0
django-flat-json-widget~=0.1.2
openwisp-users~=0.5.1
# TODO: change this when next version of openwisp_users is released
openwisp-users @ https://github.com/openwisp/openwisp-users/tarball/master
openwisp-utils[rest]~=0.7.1
openwisp-notifications~=0.3
djangorestframework-gis>=0.12.0,<0.17.0
Expand Down

0 comments on commit 5a22e0d

Please sign in to comment.