-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializers.py
185 lines (125 loc) · 6.21 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from saintsophia.abstract.serializers import DynamicDepthSerializer, GenericSerializer
from rest_framework_gis.serializers import GeoFeatureModelSerializer
from rest_framework.serializers import SerializerMethodField
from . import models
from saintsophia.utils import get_fields, DEFAULT_FIELDS
from .models import *
from django.db.models import Q
class LanguageSerializer(DynamicDepthSerializer):
class Meta:
model = Language
fields = get_fields(Language, exclude=DEFAULT_FIELDS) + ['id']
class WritingSystemSerializer(DynamicDepthSerializer):
class Meta:
model = WritingSystem
fields = get_fields(WritingSystem, exclude=DEFAULT_FIELDS) + ['id']
class GenreSerializer(DynamicDepthSerializer):
class Meta:
model = Genre
fields = get_fields(Genre, exclude=DEFAULT_FIELDS) + ['id']
class HistoricalPersonSerializer(DynamicDepthSerializer):
class Meta:
model = HistoricalPerson
fields = get_fields(HistoricalPerson, exclude=DEFAULT_FIELDS) + ['id']
class PanelSerializer(DynamicDepthSerializer):
list_of_languages = SerializerMethodField()
number_of_inscriptions = SerializerMethodField()
class Meta:
model = Panel
fields = get_fields(Panel, exclude=DEFAULT_FIELDS)+ ['id', 'list_of_languages', 'number_of_inscriptions']
def get_list_of_languages(self, obj):
inscriptions_on_panel = obj.inscriptions.all()
languages = []
for inscription in inscriptions_on_panel:
if inscription.language is not None:
languages.append(inscription.language.text)
return set(languages)
def get_number_of_inscriptions(self, obj):
inscriptions_on_panel = obj.inscriptions.all()
return len(inscriptions_on_panel)
class PanelGeoSerializer(GeoFeatureModelSerializer):
attached_photograph = SerializerMethodField()
attached_topography = SerializerMethodField()
attached_RTI = SerializerMethodField()
attached_3Dmesh = SerializerMethodField()
class Meta:
model = Panel
fields = get_fields(Panel, exclude=DEFAULT_FIELDS)+ ['id', 'attached_photograph', 'attached_topography', 'attached_RTI', 'attached_3Dmesh']
geo_field = 'geometry'
depth = 1
def get_attached_photograph(self, obj):
return obj.images.filter(published=True).filter(type_of_image__text="Orthophoto").values()
def get_attached_topography(self, obj):
return obj.images.filter(published=True).filter(type_of_image__text="Topography").values()
def get_attached_RTI(self, obj):
return obj.rti.filter(published=True).values()
def get_attached_3Dmesh(self, obj):
return obj.mesh.filter(published=True).values()
class PanelMetadataSerializer(DynamicDepthSerializer):
number_of_inscriptions = SerializerMethodField()
number_of_languages = SerializerMethodField()
list_of_languages = SerializerMethodField()
class Meta:
model = Panel
fields = get_fields(Panel, exclude=DEFAULT_FIELDS +['geometry',
'spatial_position',
'spatial_direction'])+ ['id',
'number_of_inscriptions',
'number_of_languages',
'list_of_languages']
def get_number_of_inscriptions(self, obj):
return obj.inscriptions.count()
def get_number_of_languages(self, obj):
inscriptions_on_panel = obj.inscriptions.all()
languages = []
for inscription in inscriptions_on_panel:
if inscription.language is not None:
languages.append(inscription.language.text)
return len(set(languages))
def get_list_of_languages(self, obj):
inscriptions_on_panel = obj.inscriptions.all()
languages = []
for inscription in inscriptions_on_panel:
if inscription.language is not None:
languages.append(inscription.language.text)
return set(languages)
class PanelCoordinatesSerializer(GeoFeatureModelSerializer):
floor = SerializerMethodField()
number_of_inscriptions = SerializerMethodField()
class Meta:
model = Panel
fields = ['id', 'title', 'floor', 'number_of_inscriptions', 'published', 'data_available']
geo_field = 'geometry'
depth = 1
def get_floor(self, obj):
# the floor at which each panel can be found is the first character of the title
return obj.title[0]
def get_number_of_inscriptions(self, obj):
return obj.inscriptions.count()
class InscriptionSerializer(DynamicDepthSerializer):
inscription_iiif_url = SerializerMethodField()
class Meta:
model = Inscription
fields = get_fields(Inscription, exclude=DEFAULT_FIELDS)+ ['id', 'inscription_iiif_url']
def get_inscription_iiif_url(self, obj):
images = obj.panel.images.filter(type_of_image=1).values() # 1 is orthophotos
url = ""
if len(images) > 0:
url = f"https://img.dh.gu.se/saintsophia/static/{images[0]['iiif_file']}/{obj.position_on_surface}/"
return url
class InscriptionTagsSerializer(DynamicDepthSerializer):
class Meta:
model = Tag
fields = get_fields(Tag, exclude=DEFAULT_FIELDS)+ ['id']
class TIFFImageSerializer(DynamicDepthSerializer):
class Meta:
model = Image
fields = get_fields(Image, exclude=DEFAULT_FIELDS)+ ['id']
class ObjectRTISerializer(DynamicDepthSerializer):
class Meta:
model = ObjectRTI
fields = get_fields(ObjectRTI, exclude=DEFAULT_FIELDS)+ ['id']
class ObjectMesh3DSerializer(DynamicDepthSerializer):
class Meta:
model = ObjectMesh3D
fields = get_fields(ObjectMesh3D, exclude=DEFAULT_FIELDS)+ ['id']