Merge pull request #1065 from JVMartyns/feat/pokemon_partial_name_query_filter

Feat/Pokemon partial name filter
This commit is contained in:
Alessandro Pezzè 2024-05-03 19:30:53 +02:00 committed by GitHub
commit 0ecbf73565
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 66 additions and 30 deletions

View file

@ -13,6 +13,7 @@ middleware here, or combine a Django application with an application of another
framework.
"""
import os
from django.core.wsgi import get_wsgi_application

View file

@ -1242,9 +1242,11 @@ def _build_locations():
id=int(info[0]),
location_id=int(info[1]),
game_index=int(info[2]),
name="{}-{}".format(location.name, info[3])
if info[3]
else "{}-{}".format(location.name, "area"),
name=(
"{}-{}".format(location.name, info[3])
if info[3]
else "{}-{}".format(location.name, "area")
),
)
build_generic((LocationArea,), "location_areas.csv", csv_record_to_objects)

View file

@ -4,6 +4,7 @@ from rest_framework.response import Response
from rest_framework.views import APIView
from django.shortcuts import get_object_or_404
from django.http import Http404
from django.db.models import Q
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiParameter
from drf_spectacular.types import OpenApiTypes
@ -40,6 +41,15 @@ class NameOrIdRetrieval:
idPattern = re.compile(r"^-?[0-9]+$")
namePattern = re.compile(r"^[0-9A-Za-z\-\+]+$")
def get_queryset(self):
queryset = super().get_queryset()
filter = self.request.GET.get("q", "")
if filter:
queryset = queryset.filter(Q(name__icontains=filter))
return queryset
@extend_schema(
parameters=[
OpenApiParameter(

View file

@ -1679,42 +1679,46 @@ class APIData:
showdown = {
"front_default": showdown_path % pokemon.id if front_default else None,
"front_female": showdown_path % f"female/{pokemon.id}"
if front_female
else None,
"front_shiny": showdown_path % f"shiny/{pokemon.id}"
if front_shiny
else None,
"front_shiny_female": showdown_path % f"shiny/female/{pokemon.id}"
if front_shiny_female
else None,
"back_default": showdown_path % f"back/{pokemon.id}"
if back_default
else None,
"back_female": showdown_path % f"back/female/{pokemon.id}"
if back_female
else None,
"back_shiny": showdown_path % f"back/shiny/{pokemon.id}"
if back_shiny
else None,
"back_shiny_female": showdown_path % f"back/shiny/female/{pokemon.id}"
if back_shiny_female
else None,
"front_female": (
showdown_path % f"female/{pokemon.id}" if front_female else None
),
"front_shiny": (
showdown_path % f"shiny/{pokemon.id}" if front_shiny else None
),
"front_shiny_female": (
showdown_path % f"shiny/female/{pokemon.id}"
if front_shiny_female
else None
),
"back_default": (
showdown_path % f"back/{pokemon.id}" if back_default else None
),
"back_female": (
showdown_path % f"back/female/{pokemon.id}" if back_female else None
),
"back_shiny": (
showdown_path % f"back/shiny/{pokemon.id}" if back_shiny else None
),
"back_shiny_female": (
showdown_path % f"back/shiny/female/{pokemon.id}"
if back_shiny_female
else None
),
}
sprites = {
"front_default": sprite_path % pokemon.id if front_default else None,
"front_female": sprite_path % pokemon.id if front_female else None,
"front_shiny": sprite_path % pokemon.id if front_shiny else None,
"front_shiny_female": sprite_path % pokemon.id
if front_shiny_female
else None,
"front_shiny_female": (
sprite_path % pokemon.id if front_shiny_female else None
),
"back_default": sprite_path % pokemon.id if back_default else None,
"back_female": sprite_path % pokemon.id if back_female else None,
"back_shiny": sprite_path % pokemon.id if back_shiny else None,
"back_shiny_female": sprite_path % pokemon.id
if back_shiny_female
else None,
"back_shiny_female": (
sprite_path % pokemon.id if back_shiny_female else None
),
}
pokemon_sprites = PokemonSprites.objects.create(
@ -5099,6 +5103,25 @@ class APITests(APIData, APITestCase):
"{}".format(cries_data["legacy"]),
)
# test search pokemon using search query param `q=partial_name`
response = self.client.get(
"{}/pokemon/?q={}".format(API_V2, pokemon.name[:2]),
HTTP_HOST="testserver",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 1)
self.assertEqual(response.data["results"][0]["name"], pokemon.name)
response = self.client.get(
"{}/pokemon/?q={}".format(API_V2, pokemon.name[-3:]),
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], 1)
self.assertEqual(response.data["results"][0]["name"], pokemon.name)
def test_pokemon_form_api(self):
pokemon_species = self.setup_pokemon_species_data()
pokemon = self.setup_pokemon_data(pokemon_species=pokemon_species)