Merge pull request #564 from simonorono/learned_by_pokemon

This commit is contained in:
Alessandro Pezzè 2021-01-27 19:29:11 +01:00 committed by GitHub
commit 2367c4a815
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 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"
)
learned_by_pokemon = serializers.SerializerMethodField()
class Meta:
model = Move
@ -2219,8 +2220,28 @@ class MoveDetailSerializer(serializers.ModelSerializer):
"type",
"machines",
"flavor_text_entries",
"learned_by_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,10 @@ 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 +4145,8 @@ class APITests(APIData, APITestCase):
TEST_HOST, API_V2, move_flavor_text.version_group.pk
),
)
# pokemon
self.assertEqual(response.data["learned_by_pokemon"][0]["name"], pokemon.name)
# Stat Tests
def test_stat_api(self):