Skip to content

Commit 63695da

Browse files
committed
Refactor version and method data retrieval in serializers
- Updated `LocationAreaDetailSerializer` and `PokemonDetailSerializer` to order version and method objects by ID. - Created mappings from version and method IDs to their serialized data for improved clarity and efficiency in data access.
1 parent 7cd8d22 commit 63695da

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

pokemon_v2/serializers.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,11 +1192,16 @@ def get_method_rates(self, obj):
11921192
)
11931193
def get_encounters(self, obj):
11941194
# get versions for later use
1195-
version_objects = Version.objects.all()
1195+
version_objects = Version.objects.all().order_by("id")
11961196
version_data = VersionSummarySerializer(
11971197
version_objects, many=True, context=self.context
11981198
).data
11991199

1200+
# Create a mapping from version ID to serialized data
1201+
version_data_map = {
1202+
version_objects[i].id: version_data[i] for i in range(len(version_objects))
1203+
}
1204+
12001205
# all encounters associated with location area
12011206
all_encounters = Encounter.objects.filter(location_area=obj).order_by("pokemon")
12021207
encounters_list = []
@@ -1218,7 +1223,7 @@ def get_encounters(self, obj):
12181223
# each pokemon has multiple versions it could be encountered in
12191224
for ver in poke_encounters.values("version").distinct():
12201225
version_detail = OrderedDict()
1221-
version_detail["version"] = version_data[ver["version"] - 1]
1226+
version_detail["version"] = version_data_map[ver["version"]]
12221227
version_detail["max_chance"] = 0
12231228
version_detail["encounter_details"] = []
12241229

@@ -4705,15 +4710,26 @@ def get_pokemon_cries(self, obj):
47054710
}
47064711
)
47074712
def get_pokemon_moves(self, obj):
4708-
version_objects = VersionGroup.objects.all()
4713+
version_objects = VersionGroup.objects.all().order_by("id")
47094714
version_data = VersionGroupSummarySerializer(
47104715
version_objects, many=True, context=self.context
47114716
).data
4712-
method_objects = MoveLearnMethod.objects.all()
4717+
4718+
# Create a mapping from version group ID to serialized data
4719+
version_data_map = {
4720+
version_objects[i].id: version_data[i] for i in range(len(version_objects))
4721+
}
4722+
4723+
method_objects = MoveLearnMethod.objects.all().order_by("id")
47134724
method_data = MoveLearnMethodSummarySerializer(
47144725
method_objects, many=True, context=self.context
47154726
).data
47164727

4728+
# Create a mapping from method ID to serialized data
4729+
method_data_map = {
4730+
method_objects[i].id: method_data[i] for i in range(len(method_objects))
4731+
}
4732+
47174733
# Get moves related to this pokemon and pull out unique Move IDs.
47184734
# Note that it's important to order by the same column we're using to
47194735
# determine if the entries are unique. Otherwise distinct() will
@@ -4742,11 +4758,11 @@ def get_pokemon_moves(self, obj):
47424758
version_detail = OrderedDict()
47434759

47444760
version_detail["level_learned_at"] = move["level"]
4745-
version_detail["version_group"] = version_data[
4746-
move["version_group"] - 1
4761+
version_detail["version_group"] = version_data_map[
4762+
move["version_group"]
47474763
]
4748-
version_detail["move_learn_method"] = method_data[
4749-
move["move_learn_method"] - 1
4764+
version_detail["move_learn_method"] = method_data_map[
4765+
move["move_learn_method"]
47504766
]
47514767
version_detail["order"] = move["order"]
47524768

0 commit comments

Comments
 (0)