Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Pokemon partial name filter #1065

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions config/wsgi.py
Expand Up @@ -13,6 +13,7 @@
framework.

"""

import os
from django.core.wsgi import get_wsgi_application

Expand Down
8 changes: 5 additions & 3 deletions data/v2/build.py
Expand Up @@ -1242,9 +1242,11 @@ def csv_record_to_objects(info):
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)
Expand Down
10 changes: 10 additions & 0 deletions pokemon_v2/api.py
Expand Up @@ -4,6 +4,7 @@
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

Expand Down Expand Up @@ -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(
Expand Down
77 changes: 50 additions & 27 deletions pokemon_v2/tests.py
Expand Up @@ -1679,42 +1679,46 @@ def setup_pokemon_sprites_data(

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(
Expand Down Expand Up @@ -5099,6 +5103,25 @@ def test_pokemon_api(self):
"{}".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)
Expand Down