From fa854f55ed84c9df26d3f953b8d48cdd75f8ce9a Mon Sep 17 00:00:00 2001 From: Penzi_ <101235314+PaoloAJ@users.noreply.github.com> Date: Fri, 3 Oct 2025 01:08:38 -0400 Subject: [PATCH] Implemented and tested feature --- Makefile | 24 +++++++------ pokemon_v2/serializers.py | 76 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 44f25481e..b2155582c 100755 --- a/Makefile +++ b/Makefile @@ -4,6 +4,10 @@ docker_config = --settings=config.docker-compose gql_compose_config = -f docker-compose.yml -f Resources/compose/docker-compose-prod-graphql.yml gqlv1beta_compose_config = -f docker-compose.yml -f Resources/compose/docker-compose-prod-graphql.yml -f Resources/compose/docker-compose-prod-graphql-v1beta.yml +# Auto-detect Python and pip commands +PYTHON := $(shell which python3 2>/dev/null || which python 2>/dev/null || echo python3) +PIP := $(shell which pip3 2>/dev/null || which pip 2>/dev/null || echo pip3) + .PHONY: help .SILENT: @@ -11,40 +15,40 @@ help: @grep -E '^[a-zA-Z_-]+:.*?# .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?# "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' install: # Install base requirements to run project - pip install -r requirements.txt + $(PIP) install -r requirements.txt dev-install: # Install developer requirements + base requirements - pip install -r test-requirements.txt + $(PIP) install -r test-requirements.txt setup: # Set up the project database - python manage.py migrate ${local_config} + $(PYTHON) manage.py migrate ${local_config} build-db: # Build database - echo "from data.v2.build import build_all; build_all()" | python manage.py shell ${local_config} + echo "from data.v2.build import build_all; build_all()" | $(PYTHON) manage.py shell ${local_config} wipe-sqlite-db: # Delete's the project database rm -rf db.sqlite3 serve: # Run the project locally - python manage.py runserver ${local_config} + $(PYTHON) manage.py runserver ${local_config} test: # Run tests - python manage.py test ${local_config} + $(PYTHON) manage.py test ${local_config} clean: # Remove any pyc files find . -type f -name '*.pyc' -delete migrate: # Run any outstanding migrations - python manage.py migrate ${local_config} + $(PYTHON) manage.py migrate ${local_config} make-migrations: # Create migrations files if schema has changed - python manage.py makemigrations ${local_config} + $(PYTHON) manage.py makemigrations ${local_config} shell: # Load a shell - python manage.py shell ${local_config} + $(PYTHON) manage.py shell ${local_config} openapi-generate: - python manage.py spectacular --color --file openapi.yml ${local_config} + $(PYTHON) manage.py spectacular --color --file openapi.yml ${local_config} docker-up: # (Docker) Create services/volumes/networks docker compose up -d diff --git a/pokemon_v2/serializers.py b/pokemon_v2/serializers.py index d457e686a..c46c83238 100644 --- a/pokemon_v2/serializers.py +++ b/pokemon_v2/serializers.py @@ -1421,6 +1421,7 @@ class StatDetailSerializer(serializers.ModelSerializer): ) affecting_moves = serializers.SerializerMethodField("get_moves_that_affect") affecting_natures = serializers.SerializerMethodField("get_natures_that_affect") + affecting_items = serializers.SerializerMethodField("get_items_that_affect") class Meta: model = Stat @@ -1431,6 +1432,7 @@ class Meta: "is_battle_only", "affecting_moves", "affecting_natures", + "affecting_items", "characteristics", "move_damage_class", "names", @@ -1569,6 +1571,80 @@ def get_natures_that_affect(self, obj): return OrderedDict([("increase", increases), ("decrease", decreases)]) + @extend_schema_field( + field={ + "type": "array", + "items": { + "type": "object", + "required": ["name", "url"], + "properties": { + "name": { + "type": "string", + "examples": ["protein", "x-attack"], + }, + "url": { + "type": "string", + "format": "uri", + "examples": ["https://pokeapi.co/api/v2/item/46/"], + }, + }, + }, + } + ) + def get_items_that_affect(self, obj): + """ + Get items that affect this stat (like vitamins, X-items, etc.) + """ + # Map stat names to their corresponding vitamin items + stat_item_mapping = { + "hp": ["hp-up"], + "attack": ["protein"], + "defense": ["iron"], + "special-attack": ["calcium"], + "special-defense": ["zinc"], + "speed": ["carbos"], + } + + # Get the stat name (lowercase) + stat_name = obj.name.lower() + + # Find items that affect this stat + affecting_items = [] + + # Check for vitamin items + if stat_name in stat_item_mapping: + for item_identifier in stat_item_mapping[stat_name]: + try: + item = Item.objects.get(name=item_identifier) + affecting_items.append( + ItemSummarySerializer(item, context=self.context).data + ) + except Item.DoesNotExist: + pass + + # Check for X-items (like X Attack, X Defense, etc.) + x_item_mapping = { + "attack": ["x-attack"], + "defense": ["x-defense"], + "special-attack": ["x-sp-atk"], + "special-defense": ["x-sp-def"], + "speed": ["x-speed"], + "accuracy": ["x-accuracy"], + "evasion": ["x-evasion"], + } + + if stat_name in x_item_mapping: + for item_identifier in x_item_mapping[stat_name]: + try: + item = Item.objects.get(name=item_identifier) + affecting_items.append( + ItemSummarySerializer(item, context=self.context).data + ) + except Item.DoesNotExist: + pass + + return affecting_items + ############################# # ITEM POCKET SERIALIZERS #