-
Notifications
You must be signed in to change notification settings - Fork 1
/
serializers.py
64 lines (57 loc) · 2.09 KB
/
serializers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from rest_framework import serializers
from geonode.base.models import ResourceBase
from .models import TopicCategoryGDC
class TopicCategoryGDCSerializer(serializers.ModelSerializer):
class Meta:
model = TopicCategoryGDC
fields = '__all__'
class DetailsSerializer(serializers.ModelSerializer):
#categories = TopicCategoryGDCSerializer(read_only=True)
category = serializers.SlugRelatedField(
many=False,
read_only=True,
slug_field='gn_description'
)
regions = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field='name'
)
class Meta:
model = ResourceBase
fields = ['title', 'thumbnail_url' , 'detail_url', 'alternate', 'date', 'date_type', 'raw_data_quality_statement', 'raw_supplemental_information', 'raw_abstract', 'alternate', 'regions', 'category', 'bbox_polygon','ll_bbox_polygon']
class GeoJSONSerializer(serializers.ModelSerializer):
def to_representation(self, obj):
if len(obj.abstract) > 150:
formated_abstract = obj.abstract[:150-3] + '...'
else:
formated_abstract = obj.abstract
if obj.ll_bbox_polygon:
feature = {
'type': 'Feature',
'properties': {
'pk': obj.pk,
'title': obj.title,
'abstract': formated_abstract,
'detail_url': obj.detail_url
},
'geometry': {
'type': 'Polygon',
'coordinates': [obj.ll_bbox_polygon.coords[0]]
},
}
else:
feature = {
'type': 'Feature',
'properties': {
'pk': obj.pk,
'title': obj.title,
'abstract': formated_abstract,
'detail_url': obj.detail_url
},
'geometry': {
'type': 'Polygon',
'coordinates': None
},
}
return feature