Add a "pokemon" field for the move endpoint

This field will contain a simple list of pokemon summaries. Each
element on the list represents a pokemon that can learn that move.
This commit is contained in:
Simón Oroño 2021-01-23 13:22:37 -04:00
parent 85541fff96
commit 7c7beb4592
2 changed files with 32 additions and 0 deletions

View file

@ -2192,6 +2192,7 @@ class MoveDetailSerializer(serializers.ModelSerializer):
flavor_text_entries = MoveFlavorTextSerializer(
many=True, read_only=True, source="moveflavortext"
)
pokemon = serializers.SerializerMethodField("get_learned_by_pokemon")
class Meta:
model = Move
@ -2219,8 +2220,26 @@ class MoveDetailSerializer(serializers.ModelSerializer):
"type",
"machines",
"flavor_text_entries",
"pokemon"
)
def get_learned_by_pokemon(self, obj):
pokemon_moves = PokemonMove.objects.filter(move_id=obj).order_by("pokemon_id")
pokemon_list = []
pokemon_ids = pokemon_moves.values("pokemon_id").distinct()
for id in pokemon_ids:
pokemon_object = Pokemon.objects.get(pk=id["pokemon_id"])
pokemon_data = PokemonSummarySerializer(pokemon_object, context=self.context).data
pokemon_list.append(pokemon_data)
return pokemon_list
def get_move_machines(self, obj):
machine_objects = Machine.objects.filter(move=obj)

View file

@ -3897,6 +3897,14 @@ class APITests(APIData, APITestCase):
move_effect_change_effect_text = self.setup_move_effect_change_effect_text_data(
move_effect_change=move_effect_change, effect="efct tx for mv efct chng"
)
pokemon = self.setup_pokemon_data()
version_group = self.setup_version_group_data()
self.setup_pokemon_move_data(
pokemon,
move,
version_group
)
after_move = self.setup_move_data(name="after mv")
before_move = self.setup_move_data(name="before mv")
@ -4141,6 +4149,11 @@ class APITests(APIData, APITestCase):
TEST_HOST, API_V2, move_flavor_text.version_group.pk
),
)
# pokemon
self.assertEqual(
response.data["pokemon"][0]["name"],
pokemon.name
)
# Stat Tests
def test_stat_api(self):