forked from openwisp/openwisp-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[change] Separated the serializer classes from views openwisp#414
Closes openwisp#414
- Loading branch information
1 parent
a2fc418
commit 7806457
Showing
2 changed files
with
46 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django.urls import reverse | ||
from rest_framework.serializers import IntegerField, SerializerMethodField | ||
from rest_framework_gis import serializers as gis_serializers | ||
from swapper import load_model | ||
|
||
from openwisp_utils.api.serializers import ValidatedModelSerializer | ||
|
||
Device = load_model('config', 'Device') | ||
Location = load_model('geo', 'Location') | ||
DeviceLocation = load_model('geo', 'DeviceLocation') | ||
|
||
|
||
class LocationSerializer(gis_serializers.GeoFeatureModelSerializer): | ||
class Meta: | ||
model = Location | ||
geo_field = 'geometry' | ||
fields = ('name', 'geometry') | ||
read_only_fields = ('name',) | ||
|
||
|
||
class LocationDeviceSerializer(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__' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters