cleaned up the berry mess

This commit is contained in:
Adickes 2015-11-10 14:20:06 -05:00
parent aa1d7d9939
commit 75e39ba530
5 changed files with 1720 additions and 1652 deletions

View file

@ -51,6 +51,7 @@ router.register(r"ability", AbilityResource)
router.register(r"ability-change", AbilityChangeResource)
router.register(r"berry", BerryResource)
router.register(r"berry-firmness", BerryFirmnessResource)
router.register(r"berry-flavor", BerryFlavorResource)
router.register(r"characteristic", CharacteristicResource)
router.register(r"contest-type", ContestTypeResource)
router.register(r"contest-effect", ContestEffectResource)

File diff suppressed because it is too large Load diff

View file

@ -709,11 +709,29 @@ class Berry(HasName, HasItem, HasNature):
smoothness = models.IntegerField()
class BerryFlavor(HasContestType):
"""
Berry Flavors are a bit of a hack because their relationship
in terms of flavors to contest types is really awkward the
way it was handled in the veekun data set. Berry Flavor here
does not match the csv table. Berry Flavor Map
is a table fabricated just to suit this project.
"""
class BerryFlavor(HasName, HasContestType):
pass
class BerryFlavorName(IsName):
berry_flavor = models.ForeignKey(BerryFlavor, blank=True, null=True, related_name="%(class)s")
class BerryFlavorMap(models.Model):
berry = models.ForeignKey(Berry, blank=True, null=True, related_name="%(class)s")
flavor = models.IntegerField()
berry_flavor = models.ForeignKey(BerryFlavor, blank=True, null=True, related_name="%(class)s")
potency = models.IntegerField()

View file

@ -30,6 +30,12 @@ class BerryFirmnessSummarySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = BerryFirmness
fields = ('name', 'url')
class BerryFlavorSummarySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = BerryFlavor
fields = ('name', 'url')
class BerrySummarySerializer(serializers.HyperlinkedModelSerializer):
@ -306,6 +312,15 @@ class VersionGroupSummarySerializer(serializers.HyperlinkedModelSerializer):
# MAP SERIALIZERS #
#####################
class BerryFlavorMapSerializer(serializers.ModelSerializer):
berry = BerrySummarySerializer()
berry_flavor = BerryFlavorSummarySerializer()
class Meta:
model = BerryFlavorMap
fields = ('berry', 'berry_flavor', 'potency')
class ItemAttributeMapSerializer(serializers.ModelSerializer):
item = ItemSummarySerializer()
@ -1178,13 +1193,23 @@ class BerryFirmnessDetailSerializer(serializers.ModelSerializer):
fields = ('id', 'name', 'names')
class BerryFlavorSerializer(serializers.ModelSerializer):
class BerryFlavorNameSerializer(serializers.ModelSerializer):
language = LanguageSummarySerializer()
class Meta:
model = BerryFlavorName
fields = ('name', 'language')
class BerryFlavorDetailSerializer(serializers.ModelSerializer):
names = BerryFlavorNameSerializer(many=True, read_only=True, source="berryflavorname")
contest_type = ContestTypeSummarySerializer()
class Meta:
model = BerryFlavor
fields = ('flavor', 'contest_type')
fields = ('id', 'name', 'contest_type', 'names')
class BerryDetailSerializer(serializers.ModelSerializer):
@ -1193,8 +1218,7 @@ class BerryDetailSerializer(serializers.ModelSerializer):
nature = NatureSummarySerializer()
nature_power = serializers.IntegerField(source='natural_gift_power')
firmness = BerryFirmnessSummarySerializer(source="berry_firmness")
# flavors = BerryFlavorSerializer(many=True, read_only=True, source="berryflavor")
flavors = serializers.SerializerMethodField('get_flavor_strengths')
flavors = serializers.SerializerMethodField('get_berry_flavors')
class Meta:
model = Berry
@ -1213,32 +1237,16 @@ class BerryDetailSerializer(serializers.ModelSerializer):
'flavors'
)
def get_flavor_strengths(self, obj):
def get_berry_flavors(self, obj):
"""
I'm not super proud of how this is being done but the data for berry
flavors and how they map to contest types is very wonky in my opinion.
Something worth suggesting a change for in veekun's data set.
Shoot me.
"""
flavor_map_objects = BerryFlavorMap.objects.filter(berry=obj)
flavor_maps = BerryFlavorMapSerializer(flavor_map_objects, many=True, context=self.context).data
flavors = []
flavor_objects = BerryFlavor.objects.filter(berry=obj)
for map in flavor_maps:
del map['berry']
flavors.append(map)
flavors = OrderedDict()
for flavor_obj in flavor_objects:
contest_type_obj = ContestType.objects.get(pk=flavor_obj.contest_type.id)
contest_type = ContestTypeDetailSerializer(contest_type_obj, context=self.context).data
flavor = BerryFlavorSerializer(flavor_obj, context=self.context).data
for contest_type_name in contest_type['names']:
if contest_type_name['language']['name'] == 'en':
dict = flavors[contest_type['names'][0]['flavor'].lower()] = OrderedDict()
dict['strength'] = flavor['flavor']
dict['contest_type'] = flavor['contest_type']
return flavors

View file

@ -86,6 +86,12 @@ class BerryFirmnessResource(PokeapiCommonViewset):
serializer_class = BerryFirmnessDetailSerializer
list_serializer_class = BerryFirmnessSummarySerializer
class BerryFlavorResource(PokeapiCommonViewset):
queryset = BerryFlavor.objects.all()
serializer_class = BerryFlavorDetailSerializer
list_serializer_class = BerryFlavorSummarySerializer
class CharacteristicResource(PokeapiCommonViewset):