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

Implemented Views #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pokemon/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
from pokemon.models import Pokemon, PokemonTypes


# TODO: Add a serializer for PokemonTypes, and use it as a nested serializer for PokemonSerializer

class PokemonTypeSerializer(serializers.ModelSerializer):
class Meta:
model = PokemonTypes
fields = ('type',)

class PokemonSerializer(serializers.ModelSerializer):
types = PokemonTypeSerializer(many=True)

class Meta:
model = Pokemon
fields = ('id', 'name_english', 'name_japanese', 'name_chinese', 'name_french', 'hp', 'attack', 'defense',
'special_attack', 'special_defense', 'speed', )
'special_attack', 'special_defense', 'speed', 'types')
88 changes: 69 additions & 19 deletions pokemon/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,92 @@ def pokemon_list(request):
"""
pokemon = Pokemon.objects.all()
serializer = PokemonSerializer(pokemon, many=True)
return JsonResponse(serializer.data, safe=False, json_dumps_params={'ensure_ascii': False})
return JsonResponse(serializer.data, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 4})


@csrf_exempt
@api_view(['GET'])
def pokemon_by_id(request, id):
"""
Get Pokemon by ID
"""
# TODO: Implement Endpoint
return HttpResponse(status=501)
try:
pokemon = Pokemon.objects.get(id=id)
serializer = PokemonSerializer(pokemon, many=False)
return JsonResponse(serializer.data, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 4})
except Pokemon.DoesNotExist:
message = {'error': 'Not found'}
return JsonResponse(message, status=404, json_dumps_params={'ensure_ascii': False, 'indent': 4})


@csrf_exempt
@api_view(['GET'])
def pokemon_by_name(request, name):
"""
Get Pokemon by name
"""
# TODO: Implement Endpoint
return HttpResponse(status=501)

# Make the first letter of the name uppercase and the rest lowercase
name = name.capitalize()
try:
pokemon = Pokemon.objects.get(name_english=name)
serializer = PokemonSerializer(pokemon, many=False)
return JsonResponse(serializer.data, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 4})
except Pokemon.DoesNotExist:
message = {'error': 'Not found'}
return JsonResponse(message, status=404, json_dumps_params={'ensure_ascii': False, 'indent': 4})

@csrf_exempt
def pokemon_by_type(request, pokemon_type):
"""
Get Pokemon by type
"""
# TODO: Implement Endpoint
return HttpResponse(status=501)
pokemon_type = pokemon_type.lower() # Convert pokemon_type to lowercase
VALID_POKEMON_TYPES = ['fire', 'water', 'grass', 'electric', 'psychic', 'ghost', 'dark', 'fairy', 'rock', 'ground', 'steel', 'flying', 'fighting', 'bug', 'ice', 'dragon', 'poison', 'normal'] # Add or modify this list based on your data

if pokemon_type not in VALID_POKEMON_TYPES:
return JsonResponse({'error': 'Bad request'}, status=400)

try:
pokemon = Pokemon.objects.filter(types__type__iexact=pokemon_type)
# Check if Database returned nothing
if pokemon.count() == 0:
raise Pokemon.DoesNotExist
serializer = PokemonSerializer(pokemon, many=True)
return JsonResponse(serializer.data, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 4})
except Pokemon.DoesNotExist:
message = {'error': f'There are no Pokemon with type {pokemon_type}'}
return JsonResponse(message, status=400)


@csrf_exempt
def pokemon_by_hp(request):
"""
Get Pokemon by HP
"""
# TODO: Implement Endpoint
return HttpResponse(status=501)
hp_gt = request.GET.get('gt', None) # Get the 'gt' parameter from the request, default value is None
hp_lt = request.GET.get('lt', None) # Get the 'lt' parameter from the request, default value is None

if hp_gt is None and hp_lt is None:
return JsonResponse({'error': 'Invalid Operator. Must be one of ["gt","gte","lt","lte"]'}, status=400)
elif hp_gt is None :
try:
pokemon = Pokemon.objects.filter(hp__lte=hp_lt) # Filter Pokemon with HP less than or equal to the specified value
if pokemon.count() == 0:
raise Pokemon.DoesNotExist
serializer = PokemonSerializer(pokemon, many=True)
return JsonResponse(serializer.data, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 4})
except Pokemon.DoesNotExist:
message = {'error': f'There are no Pokemon with HP less than or equal to {hp_lt}'}
return JsonResponse(message, status=400)
elif hp_lt is None:
try:
pokemon = Pokemon.objects.filter(hp__gte=hp_gt) # Filter Pokemon with HP greater than or equal to the specified value
if pokemon.count() == 0:
raise Pokemon.DoesNotExist
serializer = PokemonSerializer(pokemon, many=True)
return JsonResponse(serializer.data, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 4})
except Pokemon.DoesNotExist:
message = {'error': f'There are no Pokemon with HP greater than or equal to {hp_gt}'}
return JsonResponse(message, status=400)
else:
try:
pokemon = Pokemon.objects.filter(hp__gte=hp_gt, hp__lte=hp_lt) # Filter Pokemon with HP within the specified range
if pokemon.count() == 0:
raise Pokemon.DoesNotExist
serializer = PokemonSerializer(pokemon, many=True)
return JsonResponse(serializer.data, safe=False, json_dumps_params={'ensure_ascii': False, 'indent': 4})
except Pokemon.DoesNotExist:
message = {'error': f'There are no Pokemon with HP greater than or equal to {hp_gt} and less than or equal to {hp_lt}'}
return JsonResponse(message, status=400)