-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializers.py
165 lines (114 loc) · 4.96 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from diana.abstract.serializers import DynamicDepthSerializer, GenericSerializer
from rest_framework_gis.serializers import GeoFeatureModelSerializer
from rest_framework.serializers import SerializerMethodField
from . import models
from diana.utils import get_fields, DEFAULT_FIELDS
from .models import *
class PlaceSerializer(DynamicDepthSerializer):
class Meta:
model = Place
fields = get_fields(Place, exclude=DEFAULT_FIELDS)+ ['id']
class TIFFImageSerializer(DynamicDepthSerializer):
class Meta:
model = Image
fields = get_fields(Image, exclude=DEFAULT_FIELDS)+ ['id']
class PlaceCoordinatesSerializer(GeoFeatureModelSerializer):
class Meta:
model = Place
fields = ['id', 'name', 'dataset']
geo_field = 'geometry'
depth = 1
class LayerSerializer(DynamicDepthSerializer):
class Meta:
model = Layer
fields = get_fields(Layer, exclude=DEFAULT_FIELDS)+ ['id']
class AuthorSerializer(DynamicDepthSerializer):
class Meta:
model = Author
fields = get_fields(Author, exclude=DEFAULT_FIELDS)+ ['id']
class DatasetSerializer(DynamicDepthSerializer):
class Meta:
model = Dataset
fields = get_fields(Dataset, exclude=DEFAULT_FIELDS)+ ['id']
class Object3DHopSerializer(DynamicDepthSerializer):
class Meta:
model = Object3DHop
fields = get_fields(Object3DHop, exclude=DEFAULT_FIELDS)+ ['id']
class ObjectPointCloudSerializer(DynamicDepthSerializer):
class Meta:
model = ObjectPointCloud
fields = get_fields(ObjectPointCloud, exclude=DEFAULT_FIELDS)+ ['id']
class DocumentSerializer(DynamicDepthSerializer):
type_names = SerializerMethodField()
class Meta:
model = Document
fields = get_fields(Document, exclude=DEFAULT_FIELDS)+ ['id', 'type_names']
def get_type_names(self, obj):
type_names = []
types = obj.type.all()
for type in types:
type_names.append(type.text)
return type_names
class ObservationSerializer(DynamicDepthSerializer):
class Meta:
model = Observation
fields = get_fields(Observation, exclude=DEFAULT_FIELDS)+ ['id']
class SiteSerializer(DynamicDepthSerializer):
class Meta:
model = Site
fields = get_fields(Site, exclude=DEFAULT_FIELDS)+ ['id']
class NecropolisSerializer(DynamicDepthSerializer):
class Meta:
model = Necropolis
fields = get_fields(Necropolis, exclude=DEFAULT_FIELDS)+ ['id']
class TagSerializer(DynamicDepthSerializer):
class Meta:
model = Tag
fields = ['id'] + get_fields(Tag, exclude=DEFAULT_FIELDS)
class PlaceGeoSerializer(GeoFeatureModelSerializer):
# images = TIFFImageSerializer(many=True)
photographs_count = SerializerMethodField()
plans_count = SerializerMethodField()
threedhop_count = SerializerMethodField()
pointcloud_count = SerializerMethodField()
first_photograph_id = SerializerMethodField()
# class Meta:
# model = Place
# fields = get_fields(Place, exclude=DEFAULT_FIELDS)+ ['id', 'photographs_count', 'plans_count', 'threedhop_count', 'pointcloud_count', 'first_photograph_id']
# geo_field = 'geometry'
# # depth = 1
def __init__(self, *args, **kwargs):
# Accept 'depth' from the view context or leave it unset (no depth)
depth = kwargs.pop('depth', None)
super(PlaceGeoSerializer, self).__init__(*args, **kwargs)
# Dynamically set the depth only if it is provided
if depth is not None:
self.Meta.depth = depth
else:
# If no depth is provided, remove it so there's no depth by default
if hasattr(self.Meta, 'depth'):
del self.Meta.depth
class Meta:
model = Place
fields = get_fields(Place, exclude=DEFAULT_FIELDS) + [
'id', 'photographs_count', 'plans_count', 'threedhop_count',
'pointcloud_count', 'first_photograph_id'
]
geo_field = 'geometry'
# No default depth here, we want to start with no depth
def get_photographs_count(self, obj):
return obj.images.filter(type_of_image__text="photograph").count()
def get_plans_count(self, obj):
floor_plans = obj.images.filter(type_of_image__text="floor plan").count()
section_plans = obj.images.filter(type_of_image__text="section").count()
return floor_plans + section_plans
def get_threedhop_count(self, obj):
return obj.object_3Dhop.count()
def get_pointcloud_count(self, obj):
return obj.object_pointcloud.count()
def get_first_photograph_id(self, obj):
try:
object_to_display = obj.images.filter(type_of_image__text="photograph").filter(published=True).values()[0]
except:
object_to_display = []
return object_to_display