2018-10-20 00:05:10 +00:00
|
|
|
import re
|
2016-06-25 17:33:52 +00:00
|
|
|
from rest_framework import viewsets
|
2016-05-30 13:58:03 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
2015-09-09 04:28:33 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2016-06-04 16:27:36 +00:00
|
|
|
from django.http import Http404
|
2024-04-08 13:27:14 +00:00
|
|
|
from django.db.models import Q
|
2018-10-18 23:28:47 +00:00
|
|
|
|
2018-10-20 00:05:10 +00:00
|
|
|
from .models import *
|
|
|
|
from .serializers import *
|
2014-12-04 11:11:46 +00:00
|
|
|
|
2018-10-20 00:05:10 +00:00
|
|
|
# pylint: disable=no-member, attribute-defined-outside-init
|
2015-03-11 20:07:30 +00:00
|
|
|
|
2015-09-10 23:02:06 +00:00
|
|
|
###########################
|
2016-04-30 12:02:27 +00:00
|
|
|
# BEHAVIOR ABSTRACTIONS #
|
2015-09-10 23:02:06 +00:00
|
|
|
###########################
|
|
|
|
|
2020-02-17 20:58:07 +00:00
|
|
|
|
|
|
|
class ListOrDetailSerialRelation:
|
2015-09-09 04:28:33 +00:00
|
|
|
"""
|
2015-09-10 23:02:06 +00:00
|
|
|
Mixin to allow association with separate serializers
|
|
|
|
for list or detail view.
|
2015-09-09 04:28:33 +00:00
|
|
|
"""
|
2015-09-10 23:02:06 +00:00
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
list_serializer_class = None
|
2015-09-06 01:22:06 +00:00
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
def get_serializer_class(self):
|
2020-02-17 20:58:07 +00:00
|
|
|
if self.action == "list" and self.list_serializer_class is not None:
|
2015-09-10 04:03:18 +00:00
|
|
|
return self.list_serializer_class
|
|
|
|
return self.serializer_class
|
2015-09-06 01:22:06 +00:00
|
|
|
|
2015-09-10 23:02:06 +00:00
|
|
|
|
2020-02-17 20:58:07 +00:00
|
|
|
class NameOrIdRetrieval:
|
2015-09-10 23:02:06 +00:00
|
|
|
"""
|
2016-03-05 09:27:25 +00:00
|
|
|
Mixin to allow retrieval of resources by
|
2015-09-10 23:02:06 +00:00
|
|
|
pk (in this case ID) or by name
|
|
|
|
"""
|
|
|
|
|
2018-10-20 00:05:10 +00:00
|
|
|
idPattern = re.compile(r"^-?[0-9]+$")
|
|
|
|
namePattern = re.compile(r"^[0-9A-Za-z\-\+]+$")
|
2015-09-10 23:02:06 +00:00
|
|
|
|
2024-04-08 13:27:14 +00:00
|
|
|
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
|
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
def get_object(self):
|
2015-09-09 04:28:33 +00:00
|
|
|
queryset = self.get_queryset()
|
|
|
|
queryset = self.filter_queryset(queryset)
|
2020-02-17 20:58:07 +00:00
|
|
|
lookup = self.kwargs["pk"]
|
2015-09-06 01:22:06 +00:00
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
if self.idPattern.match(lookup):
|
2020-05-03 13:02:07 +00:00
|
|
|
lookup_id = int(lookup)
|
|
|
|
if abs(lookup_id) > 2147483647:
|
|
|
|
raise Http404
|
|
|
|
|
2015-09-09 04:28:33 +00:00
|
|
|
resp = get_object_or_404(queryset, pk=lookup)
|
2015-09-06 01:22:06 +00:00
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
elif self.namePattern.match(lookup):
|
2015-09-09 04:28:33 +00:00
|
|
|
resp = get_object_or_404(queryset, name=lookup)
|
2015-09-06 01:22:06 +00:00
|
|
|
|
2015-09-09 04:28:33 +00:00
|
|
|
else:
|
2016-06-04 16:27:36 +00:00
|
|
|
raise Http404
|
2016-03-05 09:27:25 +00:00
|
|
|
|
2015-09-09 04:28:33 +00:00
|
|
|
return resp
|
|
|
|
|
2016-03-05 09:27:25 +00:00
|
|
|
|
2020-02-17 20:58:07 +00:00
|
|
|
class PokeapiCommonViewset(
|
|
|
|
ListOrDetailSerialRelation, NameOrIdRetrieval, viewsets.ReadOnlyModelViewSet
|
|
|
|
):
|
2015-09-10 23:02:06 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
##########
|
|
|
|
# APIS #
|
|
|
|
##########
|
|
|
|
|
2020-02-17 20:58:07 +00:00
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
class AbilityResource(PokeapiCommonViewset):
|
2015-09-06 02:10:13 +00:00
|
|
|
queryset = Ability.objects.all()
|
2015-09-11 12:15:45 +00:00
|
|
|
serializer_class = AbilityDetailSerializer
|
|
|
|
list_serializer_class = AbilitySummarySerializer
|
2015-09-09 04:28:33 +00:00
|
|
|
|
|
|
|
|
2015-10-03 03:40:21 +00:00
|
|
|
class BerryResource(PokeapiCommonViewset):
|
|
|
|
queryset = Berry.objects.all()
|
|
|
|
serializer_class = BerryDetailSerializer
|
|
|
|
list_serializer_class = BerrySummarySerializer
|
|
|
|
|
|
|
|
|
2015-10-06 17:01:32 +00:00
|
|
|
class BerryFirmnessResource(PokeapiCommonViewset):
|
|
|
|
queryset = BerryFirmness.objects.all()
|
|
|
|
serializer_class = BerryFirmnessDetailSerializer
|
|
|
|
list_serializer_class = BerryFirmnessSummarySerializer
|
|
|
|
|
2016-03-05 09:27:25 +00:00
|
|
|
|
2015-11-10 19:20:06 +00:00
|
|
|
class BerryFlavorResource(PokeapiCommonViewset):
|
|
|
|
queryset = BerryFlavor.objects.all()
|
|
|
|
serializer_class = BerryFlavorDetailSerializer
|
|
|
|
list_serializer_class = BerryFlavorSummarySerializer
|
|
|
|
|
2015-10-06 17:01:32 +00:00
|
|
|
|
2015-10-29 17:45:58 +00:00
|
|
|
class CharacteristicResource(PokeapiCommonViewset):
|
|
|
|
queryset = Characteristic.objects.all()
|
|
|
|
serializer_class = CharacteristicDetailSerializer
|
|
|
|
list_serializer_class = CharacteristicSummarySerializer
|
|
|
|
|
|
|
|
|
2015-11-05 14:55:14 +00:00
|
|
|
class ContestEffectResource(PokeapiCommonViewset):
|
|
|
|
queryset = ContestEffect.objects.all()
|
|
|
|
serializer_class = ContestEffectDetailSerializer
|
|
|
|
list_serializer_class = ContestEffectSummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ContestTypeResource(PokeapiCommonViewset):
|
|
|
|
queryset = ContestType.objects.all()
|
|
|
|
serializer_class = ContestTypeDetailSerializer
|
|
|
|
list_serializer_class = ContestTypeSummarySerializer
|
|
|
|
|
|
|
|
|
2015-10-03 03:40:21 +00:00
|
|
|
class EggGroupResource(PokeapiCommonViewset):
|
|
|
|
queryset = EggGroup.objects.all()
|
|
|
|
serializer_class = EggGroupDetailSerializer
|
|
|
|
list_serializer_class = EggGroupSummarySerializer
|
|
|
|
|
|
|
|
|
2015-11-05 14:55:14 +00:00
|
|
|
class EncounterConditionResource(PokeapiCommonViewset):
|
|
|
|
queryset = EncounterCondition.objects.all()
|
|
|
|
serializer_class = EncounterConditionDetailSerializer
|
|
|
|
list_serializer_class = EncounterConditionSummarySerializer
|
|
|
|
|
2015-10-29 17:45:58 +00:00
|
|
|
|
2015-11-05 14:55:14 +00:00
|
|
|
class EncounterConditionValueResource(PokeapiCommonViewset):
|
|
|
|
queryset = EncounterConditionValue.objects.all()
|
|
|
|
serializer_class = EncounterConditionValueDetailSerializer
|
|
|
|
list_serializer_class = EncounterConditionValueSummarySerializer
|
2015-10-29 17:45:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EncounterMethodResource(PokeapiCommonViewset):
|
|
|
|
queryset = EncounterMethod.objects.all()
|
|
|
|
serializer_class = EncounterMethodDetailSerializer
|
|
|
|
list_serializer_class = EncounterMethodSummarySerializer
|
|
|
|
|
|
|
|
|
2015-10-06 17:01:32 +00:00
|
|
|
class EvolutionChainResource(PokeapiCommonViewset):
|
|
|
|
queryset = EvolutionChain.objects.all()
|
|
|
|
serializer_class = EvolutionChainDetailSerializer
|
|
|
|
list_serializer_class = EvolutionChainSummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class EvolutionTriggerResource(PokeapiCommonViewset):
|
|
|
|
queryset = EvolutionTrigger.objects.all()
|
|
|
|
serializer_class = EvolutionTriggerDetailSerializer
|
|
|
|
list_serializer_class = EvolutionTriggerSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
class GenerationResource(PokeapiCommonViewset):
|
2015-09-09 04:28:33 +00:00
|
|
|
queryset = Generation.objects.all()
|
2015-09-12 04:53:46 +00:00
|
|
|
serializer_class = GenerationDetailSerializer
|
|
|
|
list_serializer_class = GenerationSummarySerializer
|
2015-09-09 04:28:33 +00:00
|
|
|
|
2015-09-19 22:13:41 +00:00
|
|
|
|
2015-11-08 21:26:02 +00:00
|
|
|
class GenderResource(PokeapiCommonViewset):
|
|
|
|
queryset = Gender.objects.all()
|
|
|
|
serializer_class = GenderDetailSerializer
|
|
|
|
list_serializer_class = GenderSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-19 22:13:41 +00:00
|
|
|
class GrowthRateResource(PokeapiCommonViewset):
|
|
|
|
queryset = GrowthRate.objects.all()
|
|
|
|
serializer_class = GrowthRateDetailSerializer
|
|
|
|
list_serializer_class = GrowthRateSummarySerializer
|
|
|
|
|
|
|
|
|
2015-10-03 03:40:21 +00:00
|
|
|
class ItemResource(PokeapiCommonViewset):
|
|
|
|
queryset = Item.objects.all()
|
|
|
|
serializer_class = ItemDetailSerializer
|
|
|
|
list_serializer_class = ItemSummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ItemCategoryResource(PokeapiCommonViewset):
|
|
|
|
queryset = ItemCategory.objects.all()
|
|
|
|
serializer_class = ItemCategoryDetailSerializer
|
|
|
|
list_serializer_class = ItemCategorySummarySerializer
|
|
|
|
|
|
|
|
|
2015-10-23 04:56:01 +00:00
|
|
|
class ItemAttributeResource(PokeapiCommonViewset):
|
|
|
|
queryset = ItemAttribute.objects.all()
|
|
|
|
serializer_class = ItemAttributeDetailSerializer
|
|
|
|
list_serializer_class = ItemAttributeSummarySerializer
|
2015-10-03 03:40:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ItemFlingEffectResource(PokeapiCommonViewset):
|
|
|
|
queryset = ItemFlingEffect.objects.all()
|
|
|
|
serializer_class = ItemFlingEffectDetailSerializer
|
|
|
|
list_serializer_class = ItemFlingEffectSummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class ItemPocketResource(PokeapiCommonViewset):
|
|
|
|
queryset = ItemPocket.objects.all()
|
|
|
|
serializer_class = ItemPocketDetailSerializer
|
|
|
|
list_serializer_class = ItemPocketSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-10 23:02:06 +00:00
|
|
|
class LanguageResource(PokeapiCommonViewset):
|
|
|
|
queryset = Language.objects.all()
|
2015-09-11 12:15:45 +00:00
|
|
|
serializer_class = LanguageDetailSerializer
|
2015-10-06 17:01:32 +00:00
|
|
|
list_serializer_class = LanguageSummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class LocationResource(PokeapiCommonViewset):
|
|
|
|
queryset = Location.objects.all()
|
|
|
|
serializer_class = LocationDetailSerializer
|
|
|
|
list_serializer_class = LocationSummarySerializer
|
2015-09-10 23:02:06 +00:00
|
|
|
|
|
|
|
|
2018-10-18 18:21:53 +00:00
|
|
|
class LocationAreaResource(ListOrDetailSerialRelation, viewsets.ReadOnlyModelViewSet):
|
2015-10-29 17:45:58 +00:00
|
|
|
queryset = LocationArea.objects.all()
|
|
|
|
serializer_class = LocationAreaDetailSerializer
|
|
|
|
list_serializer_class = LocationAreaSummarySerializer
|
|
|
|
|
|
|
|
|
2016-06-25 16:45:20 +00:00
|
|
|
class MachineResource(PokeapiCommonViewset):
|
|
|
|
queryset = Machine.objects.all()
|
|
|
|
serializer_class = MachineDetailSerializer
|
|
|
|
list_serializer_class = MachineSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
class MoveResource(PokeapiCommonViewset):
|
2015-09-09 04:28:33 +00:00
|
|
|
queryset = Move.objects.all()
|
2015-09-19 22:13:41 +00:00
|
|
|
serializer_class = MoveDetailSerializer
|
|
|
|
list_serializer_class = MoveSummarySerializer
|
2015-09-09 04:28:33 +00:00
|
|
|
|
|
|
|
|
2015-09-27 04:20:19 +00:00
|
|
|
class MoveDamageClassResource(PokeapiCommonViewset):
|
|
|
|
queryset = MoveDamageClass.objects.all()
|
|
|
|
serializer_class = MoveDamageClassDetailSerializer
|
|
|
|
list_serializer_class = MoveDamageClassSummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class MoveMetaAilmentResource(PokeapiCommonViewset):
|
|
|
|
queryset = MoveMetaAilment.objects.all()
|
|
|
|
serializer_class = MoveMetaAilmentDetailSerializer
|
|
|
|
list_serializer_class = MoveMetaAilmentSummarySerializer
|
|
|
|
|
|
|
|
|
2015-11-08 21:26:02 +00:00
|
|
|
class MoveBattleStyleResource(PokeapiCommonViewset):
|
|
|
|
queryset = MoveBattleStyle.objects.all()
|
|
|
|
serializer_class = MoveBattleStyleDetailSerializer
|
|
|
|
list_serializer_class = MoveBattleStyleSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-27 04:20:19 +00:00
|
|
|
class MoveMetaCategoryResource(PokeapiCommonViewset):
|
|
|
|
queryset = MoveMetaCategory.objects.all()
|
|
|
|
serializer_class = MoveMetaCategoryDetailSerializer
|
|
|
|
list_serializer_class = MoveMetaCategorySummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class MoveLearnMethodResource(PokeapiCommonViewset):
|
|
|
|
queryset = MoveLearnMethod.objects.all()
|
|
|
|
serializer_class = MoveLearnMethodDetailSerializer
|
|
|
|
list_serializer_class = MoveLearnMethodSummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class MoveTargetResource(PokeapiCommonViewset):
|
|
|
|
queryset = MoveTarget.objects.all()
|
|
|
|
serializer_class = MoveTargetDetailSerializer
|
|
|
|
list_serializer_class = MoveTargetSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
class NatureResource(PokeapiCommonViewset):
|
2015-09-09 04:28:33 +00:00
|
|
|
queryset = Nature.objects.all()
|
2015-10-06 17:01:32 +00:00
|
|
|
serializer_class = NatureDetailSerializer
|
|
|
|
list_serializer_class = NatureSummarySerializer
|
2015-09-09 04:28:33 +00:00
|
|
|
|
|
|
|
|
2015-11-08 21:26:02 +00:00
|
|
|
class PalParkAreaResource(PokeapiCommonViewset):
|
|
|
|
queryset = PalParkArea.objects.all()
|
|
|
|
serializer_class = PalParkAreaDetailSerializer
|
|
|
|
list_serializer_class = PalParkAreaSummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class PokeathlonStatResource(PokeapiCommonViewset):
|
|
|
|
queryset = PokeathlonStat.objects.all()
|
|
|
|
serializer_class = PokeathlonStatDetailSerializer
|
|
|
|
list_serializer_class = PokeathlonStatSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-15 12:26:47 +00:00
|
|
|
class PokedexResource(PokeapiCommonViewset):
|
|
|
|
queryset = Pokedex.objects.all()
|
|
|
|
serializer_class = PokedexDetailSerializer
|
|
|
|
list_serializer_class = PokedexSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-19 22:13:41 +00:00
|
|
|
class PokemonColorResource(PokeapiCommonViewset):
|
|
|
|
queryset = PokemonColor.objects.all()
|
|
|
|
serializer_class = PokemonColorDetailSerializer
|
|
|
|
list_serializer_class = PokemonColorSummarySerializer
|
|
|
|
|
|
|
|
|
2015-10-06 17:01:32 +00:00
|
|
|
class PokemonFormResource(PokeapiCommonViewset):
|
|
|
|
queryset = PokemonForm.objects.all()
|
|
|
|
serializer_class = PokemonFormDetailSerializer
|
|
|
|
list_serializer_class = PokemonFormSummarySerializer
|
|
|
|
|
|
|
|
|
2015-10-03 03:40:21 +00:00
|
|
|
class PokemonHabitatResource(PokeapiCommonViewset):
|
|
|
|
queryset = PokemonHabitat.objects.all()
|
|
|
|
serializer_class = PokemonHabitatDetailSerializer
|
|
|
|
list_serializer_class = PokemonHabitatSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-19 22:13:41 +00:00
|
|
|
class PokemonShapeResource(PokeapiCommonViewset):
|
|
|
|
queryset = PokemonShape.objects.all()
|
|
|
|
serializer_class = PokemonShapeDetailSerializer
|
|
|
|
list_serializer_class = PokemonShapeSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
class PokemonResource(PokeapiCommonViewset):
|
2015-09-27 04:20:19 +00:00
|
|
|
queryset = Pokemon.objects.all()
|
|
|
|
serializer_class = PokemonDetailSerializer
|
2016-03-05 09:27:25 +00:00
|
|
|
list_serializer_class = PokemonSummarySerializer
|
2015-09-09 04:28:33 +00:00
|
|
|
|
|
|
|
|
2015-10-03 03:40:21 +00:00
|
|
|
class PokemonSpeciesResource(PokeapiCommonViewset):
|
2020-02-17 20:58:07 +00:00
|
|
|
queryset = PokemonSpecies.objects.all().order_by("id")
|
2015-10-03 03:40:21 +00:00
|
|
|
serializer_class = PokemonSpeciesDetailSerializer
|
|
|
|
list_serializer_class = PokemonSpeciesSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-12 04:53:46 +00:00
|
|
|
class RegionResource(PokeapiCommonViewset):
|
|
|
|
queryset = Region.objects.all()
|
|
|
|
serializer_class = RegionDetailSerializer
|
|
|
|
list_serializer_class = RegionSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-27 04:20:19 +00:00
|
|
|
class StatResource(PokeapiCommonViewset):
|
|
|
|
queryset = Stat.objects.all()
|
|
|
|
serializer_class = StatDetailSerializer
|
|
|
|
list_serializer_class = StatSummarySerializer
|
|
|
|
|
|
|
|
|
2015-11-05 14:55:14 +00:00
|
|
|
class SuperContestEffectResource(PokeapiCommonViewset):
|
|
|
|
queryset = SuperContestEffect.objects.all()
|
|
|
|
serializer_class = SuperContestEffectDetailSerializer
|
|
|
|
list_serializer_class = SuperContestEffectSummarySerializer
|
|
|
|
|
|
|
|
|
2015-09-10 04:03:18 +00:00
|
|
|
class TypeResource(PokeapiCommonViewset):
|
2015-09-09 04:28:33 +00:00
|
|
|
queryset = Type.objects.all()
|
2015-09-12 04:53:46 +00:00
|
|
|
serializer_class = TypeDetailSerializer
|
|
|
|
list_serializer_class = TypeSummarySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class VersionResource(PokeapiCommonViewset):
|
|
|
|
queryset = Version.objects.all()
|
|
|
|
serializer_class = VersionDetailSerializer
|
|
|
|
list_serializer_class = VersionSummarySerializer
|
|
|
|
|
2015-09-09 04:28:33 +00:00
|
|
|
|
2015-09-12 04:53:46 +00:00
|
|
|
class VersionGroupResource(PokeapiCommonViewset):
|
|
|
|
queryset = VersionGroup.objects.all()
|
|
|
|
serializer_class = VersionGroupDetailSerializer
|
|
|
|
list_serializer_class = VersionGroupSummarySerializer
|
2016-05-30 13:58:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PokemonEncounterView(APIView):
|
|
|
|
"""
|
|
|
|
Handles Pokemon Encounters as a sub-resource.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request, pokemon_id):
|
|
|
|
self.context = dict(request=request)
|
|
|
|
|
|
|
|
try:
|
|
|
|
pokemon = Pokemon.objects.get(pk=pokemon_id)
|
|
|
|
except Pokemon.DoesNotExist:
|
2016-06-25 17:33:52 +00:00
|
|
|
raise Http404
|
|
|
|
|
|
|
|
encounter_objects = Encounter.objects.filter(pokemon=pokemon)
|
|
|
|
|
2020-02-17 20:58:07 +00:00
|
|
|
area_ids = (
|
|
|
|
encounter_objects.order_by("location_area")
|
|
|
|
.distinct("location_area")
|
|
|
|
.values_list("location_area", flat=True)
|
|
|
|
)
|
2016-06-25 17:33:52 +00:00
|
|
|
|
|
|
|
location_area_objects = LocationArea.objects.filter(pk__in=area_ids)
|
|
|
|
version_objects = Version.objects
|
|
|
|
|
2016-05-30 13:58:03 +00:00
|
|
|
encounters_list = []
|
|
|
|
|
2016-06-25 17:33:52 +00:00
|
|
|
for area_id in area_ids:
|
|
|
|
location_area = location_area_objects.get(pk=area_id)
|
2016-05-30 13:58:03 +00:00
|
|
|
|
2024-04-08 13:51:12 +00:00
|
|
|
area_encounters = encounter_objects.filter(
|
|
|
|
location_area_id=area_id)
|
2016-05-30 13:58:03 +00:00
|
|
|
|
2020-02-17 20:58:07 +00:00
|
|
|
version_ids = (
|
|
|
|
area_encounters.order_by("version_id")
|
|
|
|
.distinct("version_id")
|
|
|
|
.values_list("version_id", flat=True)
|
|
|
|
)
|
2016-05-30 13:58:03 +00:00
|
|
|
|
2016-06-25 17:33:52 +00:00
|
|
|
version_details_list = []
|
2016-05-30 13:58:03 +00:00
|
|
|
|
2016-06-25 17:33:52 +00:00
|
|
|
for version_id in version_ids:
|
|
|
|
version = version_objects.get(pk=version_id)
|
2016-05-30 13:58:03 +00:00
|
|
|
|
2020-02-17 20:58:07 +00:00
|
|
|
version_encounters = area_encounters.filter(
|
|
|
|
version_id=version_id
|
|
|
|
).order_by("encounter_slot_id")
|
2016-05-30 13:58:03 +00:00
|
|
|
|
2016-06-25 17:33:52 +00:00
|
|
|
encounters_data = EncounterDetailSerializer(
|
2020-02-17 20:58:07 +00:00
|
|
|
version_encounters, many=True, context=self.context
|
|
|
|
).data
|
2016-05-30 13:58:03 +00:00
|
|
|
|
2016-06-25 17:33:52 +00:00
|
|
|
max_chance = 0
|
|
|
|
encounter_details_list = []
|
|
|
|
|
|
|
|
for encounter in encounters_data:
|
2024-04-08 13:51:12 +00:00
|
|
|
slot = EncounterSlot.objects.get(
|
|
|
|
pk=encounter["encounter_slot"])
|
|
|
|
slot_data = EncounterSlotSerializer(
|
|
|
|
slot, context=self.context).data
|
2016-06-25 17:33:52 +00:00
|
|
|
|
2020-02-17 20:58:07 +00:00
|
|
|
del encounter["pokemon"]
|
|
|
|
del encounter["encounter_slot"]
|
|
|
|
del encounter["location_area"]
|
|
|
|
del encounter["version"]
|
|
|
|
encounter["chance"] = slot_data["chance"]
|
|
|
|
max_chance += slot_data["chance"]
|
|
|
|
encounter["method"] = slot_data["encounter_method"]
|
2016-05-30 13:58:03 +00:00
|
|
|
|
2016-06-25 17:33:52 +00:00
|
|
|
encounter_details_list.append(encounter)
|
2016-05-30 13:58:03 +00:00
|
|
|
|
2020-02-17 20:58:07 +00:00
|
|
|
version_details_list.append(
|
|
|
|
{
|
|
|
|
"version": VersionSummarySerializer(
|
|
|
|
version, context=self.context
|
|
|
|
).data,
|
|
|
|
"max_chance": max_chance,
|
|
|
|
"encounter_details": encounter_details_list,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
encounters_list.append(
|
|
|
|
{
|
|
|
|
"location_area": LocationAreaSummarySerializer(
|
|
|
|
location_area, context=self.context
|
|
|
|
).data,
|
|
|
|
"version_details": version_details_list,
|
|
|
|
}
|
|
|
|
)
|
2016-05-30 13:58:03 +00:00
|
|
|
|
|
|
|
return Response(encounters_list)
|