-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathserializers.py
198 lines (157 loc) · 5.51 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
186
187
188
189
190
191
192
193
194
195
196
197
198
from __future__ import absolute_import
from builtins import object
from rest_framework import serializers
from game import messages
from game.theme import get_theme, get_themes_url
from .models import Workspace, Level, Episode, LevelDecor, LevelBlock, Block
class WorkspaceSerializer(serializers.ModelSerializer):
class Meta(object):
model = Workspace
class LevelListSerializer(serializers.HyperlinkedModelSerializer):
title = serializers.SerializerMethodField()
class Meta(object):
model = Level
fields = (
"url",
"__str__",
"episode",
"name",
"title",
"default",
"blocklyEnabled",
"pythonEnabled",
)
def get_title(self, obj):
if obj.default:
title = getattr(messages, "title_level" + obj.name)()
return title
else:
return "Custom Level"
class LevelDetailSerializer(serializers.HyperlinkedModelSerializer):
title = serializers.SerializerMethodField()
description = serializers.SerializerMethodField()
hint = serializers.SerializerMethodField()
levelblock_set = serializers.HyperlinkedIdentityField(
view_name="levelblock-for-level", read_only=True
)
map = serializers.HyperlinkedIdentityField(
view_name="map-for-level", read_only=True
)
mode = serializers.HyperlinkedIdentityField(
view_name="mode-for-level", read_only=True
)
class Meta(object):
model = Level
fields = (
"__str__",
"episode",
"name",
"title",
"description",
"hint",
"next_level",
"default",
"levelblock_set",
"map",
"mode",
"blocklyEnabled",
"pythonEnabled",
"pythonViewEnabled",
)
def get_title(self, obj):
if obj.default:
title = getattr(messages, "title_level" + obj.name)()
return title
else:
return "Custom Level"
def get_description(self, obj):
if obj.default:
description = getattr(messages, "description_level" + obj.name)()
return description
else:
return "Can you find the shortest route?"
def get_hint(self, obj):
if obj.default:
hint = getattr(messages, "hint_level" + obj.name)()
return hint
else:
return "Think back to earlier levels. What did you learn?"
def get_leveldecor_set(self, obj):
leveldecors = LevelDecor.objects.filter(level__id=obj.id)
serializer = LevelDecorSerializer(
leveldecors,
many=True,
context={"request": self.context.get("request", None)},
)
return serializer.data
class LevelModeSerializer(serializers.HyperlinkedModelSerializer):
class Meta(object):
model = Level
fields = ("blocklyEnabled", "pythonEnabled", "pythonViewEnabled")
class LevelMapListSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name="map-for-level", read_only=True
)
class Meta(object):
model = Level
fields = ("url",)
class LevelMapDetailSerializer(serializers.HyperlinkedModelSerializer):
leveldecor_set = serializers.SerializerMethodField()
theme = serializers.SerializerMethodField()
class Meta(object):
model = Level
fields = (
"origin",
"destinations",
"path",
"traffic_lights",
"max_fuel",
"theme",
"leveldecor_set",
)
def get_leveldecor_set(self, obj):
leveldecors = LevelDecor.objects.filter(level__id=obj.id)
serializer = LevelDecorSerializer(
leveldecors,
many=True,
context={"request": self.context.get("request", None)},
)
return serializer.data
def get_theme(self, obj):
pk = get_theme(obj.theme_name).pk
return get_themes_url(pk, self.context.get("request", None))
class EpisodeListSerializer(serializers.HyperlinkedModelSerializer):
name = serializers.SerializerMethodField()
class Meta(object):
model = Episode
fields = ("url", "__str__", "name")
def get_name(self, obj):
return messages.get_episode_title(obj.id)
class EpisodeDetailSerializer(serializers.HyperlinkedModelSerializer):
level_set = serializers.SerializerMethodField()
level_set_url = serializers.HyperlinkedIdentityField(
view_name="level-for-episode", read_only=True
)
name = serializers.SerializerMethodField()
class Meta(object):
model = Episode
depth = 1
fields = ("url", "__str__", "name", "level_set", "level_set_url")
def get_level_set(self, obj):
levels = Level.objects.filter(episode__id=obj.id)
serializer = LevelListSerializer(
levels, many=True, context={"request": self.context.get("request", None)}
)
return serializer.data
def get_name(self, obj):
return messages.get_episode_title(obj.id)
class LevelBlockSerializer(serializers.ModelSerializer):
class Meta(object):
model = LevelBlock
class LevelDecorSerializer(serializers.HyperlinkedModelSerializer):
class Meta(object):
model = LevelDecor
class BlockSerializer(serializers.HyperlinkedModelSerializer):
class Meta(object):
model = Block
fields = ("url", "id", "type")