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

Properly return sprites as an object when querying it using GraphQL #959

Merged
merged 14 commits into from
Dec 1, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ docker-stop: # (Docker) Stop containers
docker-down: # (Docker) Stop and removes containers and networks
docker-compose down

docker-test: # (Docker) Run tests
docker-compose exec -T app python manage.py test ${local_config}

docker-prod:
docker-compose -f docker-compose.yml -f docker-compose.override.yml -f Resources/compose/docker-compose-prod-graphql.yml up -d

Expand Down
Empty file removed __init__.py
Empty file.
1 change: 0 additions & 1 deletion config/docker-compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@
}

DEBUG = False
TASTYPIE_FULL_DEBUG = False

ALLOWED_HOSTS = ["*"]
1 change: 0 additions & 1 deletion config/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@
}

DEBUG = True
TASTYPIE_FULL_DEBUG = True
1 change: 0 additions & 1 deletion config/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@
}

DEBUG = True
TASTYPIE_FULL_DEBUG = True
29 changes: 21 additions & 8 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,15 @@
"SECRET_KEY", "ubx+22!jbo(^x2_scm-o$*py3e@-awu-n^hipkm%2l$sw$&2l#"
)

CUSTOM_APPS = (
"tastypie",
"pokemon_v2",
)
CUSTOM_APPS = ("pokemon_v2",)

INSTALLED_APPS = (
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.admin",
"django.contrib.messages",
"django.contrib.humanize",
"corsheaders",
"rest_framework",
Expand All @@ -106,18 +104,33 @@

API_LIMIT_PER_PAGE = 1

TASTYPIE_DEFAULT_FORMATS = ["json"]

CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOW_METHODS = "GET"

CORS_URLS_REGEX = r"^/api/.*$"

REST_FRAMEWORK = {
"DEFAULT_RENDERER_CLASSES": ("drf_ujson.renderers.UJSONRenderer",),
"DEFAULT_PARSER_CLASSES": ("drf_ujson.renderers.UJSONRenderer",),
"DEFAULT_RENDERER_CLASSES": ("rest_framework.renderers.JSONRenderer",),
"DEFAULT_PARSER_CLASSES": ("rest_framework.renderers.JSONRenderer",),
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"PAGE_SIZE": 20,
"PAGINATE_BY": 20,
}


TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
8 changes: 3 additions & 5 deletions data/v2/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,7 @@ def csv_record_to_objects(info):

item_sprites = "items/{0}"
sprites = {"default": file_path_or_none(item_sprites.format(file_name))}
yield ItemSprites(
id=int(info[0]), item_id=int(info[0]), sprites=json.dumps(sprites)
)
yield ItemSprites(id=int(info[0]), item_id=int(info[0]), sprites=sprites)

build_generic((ItemSprites,), "items.csv", csv_record_to_objects)

Expand Down Expand Up @@ -1901,7 +1899,7 @@ def csv_record_to_objects(info):
yield PokemonSprites(
id=int(info[0]),
pokemon=Pokemon.objects.get(pk=int(info[0])),
sprites=json.dumps(sprites),
sprites=sprites,
)

build_generic((PokemonSprites,), "pokemon.csv", csv_record_to_objects)
Expand Down Expand Up @@ -2024,7 +2022,7 @@ def csv_record_to_objects(info):
),
}
yield PokemonFormSprites(
id=int(info[0]), pokemon_form_id=int(info[0]), sprites=json.dumps(sprites)
id=int(info[0]), pokemon_form_id=int(info[0]), sprites=sprites
)

build_generic((PokemonFormSprites,), "pokemon_forms.csv", csv_record_to_objects)
Expand Down
18 changes: 18 additions & 0 deletions pokemon_v2/migrations/0014_auto_20231119_0303.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.15 on 2023-11-19 03:03

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("pokemon_v2", "0013_pokemonabilitypast"),
]

operations = [
migrations.AlterField(
model_name="pokemonsprites",
name="sprites",
field=django.contrib.postgres.fields.jsonb.JSONField(),
),
]
17 changes: 17 additions & 0 deletions pokemon_v2/migrations/0015_auto_20231121_1209.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.1.14 on 2023-11-21 12:09

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("pokemon_v2", "0014_auto_20231119_0303"),
]

operations = [
migrations.AlterField(
model_name="pokemonsprites",
name="sprites",
field=models.JSONField(),
),
]
22 changes: 22 additions & 0 deletions pokemon_v2/migrations/0016_auto_20231126_1258.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.1.14 on 2023-11-26 12:58

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("pokemon_v2", "0015_auto_20231121_1209"),
]

operations = [
migrations.AlterField(
model_name="itemsprites",
name="sprites",
field=models.JSONField(),
),
migrations.AlterField(
model_name="pokemonformsprites",
name="sprites",
field=models.JSONField(),
),
]
6 changes: 3 additions & 3 deletions pokemon_v2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ class ItemGameIndex(HasItem, HasGeneration, HasGameIndex):


class ItemSprites(HasItem):
sprites = models.CharField(max_length=1000)
sprites = models.JSONField()


####################
Expand Down Expand Up @@ -1716,7 +1716,7 @@ class PokemonFormName(HasPokemonForm, IsName):


class PokemonFormSprites(HasPokemonForm):
sprites = models.CharField(max_length=1000)
sprites = models.JSONField()


class PokemonGameIndex(HasPokemon, HasGameIndex, HasVersion):
Expand Down Expand Up @@ -1795,4 +1795,4 @@ class PokemonTypePast(HasPokemon, HasType, HasGeneration):


class PokemonSprites(HasPokemon):
sprites = models.CharField(max_length=20000)
sprites = models.JSONField()
24 changes: 3 additions & 21 deletions pokemon_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,10 +1417,7 @@ def get_item_machines(self, obj):

def get_item_sprites(self, obj):
sprites_object = ItemSprites.objects.get(item_id=obj)
sprites_data = ItemSpritesSerializer(sprites_object, context=self.context).data
sprites_data = json.loads(sprites_data["sprites"])

return sprites_data
return sprites_object.sprites

def get_item_attributes(self, obj):
item_attribute_maps = ItemAttributeMap.objects.filter(item=obj)
Expand Down Expand Up @@ -2518,12 +2515,7 @@ def get_pokemon_form_pokemon_names(self, obj):

def get_pokemon_form_sprites(self, obj):
sprites_object = PokemonFormSprites.objects.get(pokemon_form_id=obj)
sprites_data = PokemonFormSpritesSerializer(
sprites_object, context=self.context
).data
sprites_data = json.loads(sprites_data["sprites"])

return sprites_data
return sprites_object.sprites

def get_pokemon_form_types(self, obj):
form_type_objects = PokemonFormType.objects.filter(pokemon_form=obj)
Expand Down Expand Up @@ -2713,12 +2705,6 @@ class Meta:
fields = ("game_index", "version")


class PokemonSpritesSerializer(serializers.ModelSerializer):
class Meta:
model = PokemonSprites
fields = ("sprites",)


class PokemonDetailSerializer(serializers.ModelSerializer):
abilities = serializers.SerializerMethodField("get_pokemon_abilities")
past_abilities = serializers.SerializerMethodField("get_past_pokemon_abilities")
Expand Down Expand Up @@ -2763,11 +2749,7 @@ class Meta:

def get_pokemon_sprites(self, obj):
sprites_object = PokemonSprites.objects.get(pokemon_id=obj)
sprites_data = PokemonSpritesSerializer(
sprites_object, context=self.context
).data

return json.loads(sprites_data["sprites"])
return sprites_object.sprites

def get_pokemon_moves(self, obj):
version_objects = VersionGroup.objects.all()
Expand Down
25 changes: 14 additions & 11 deletions pokemon_v2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2588,8 +2588,6 @@ def test_item_api(self):
item_attribute_map = ItemAttributeMap(item=item, item_attribute=item_attribute)
item_attribute_map.save()

sprites_data = json.loads(item_sprites.sprites)

response = self.client.get(
"{}/item/{}/".format(API_V2, item.pk), HTTP_HOST="testserver"
)
Expand Down Expand Up @@ -2708,9 +2706,12 @@ def test_item_api(self):
response.data["baby_trigger_for"]["url"],
"{}{}/evolution-chain/{}/".format(TEST_HOST, API_V2, evolution_chain.pk),
)

sprites_data = json.loads(response.data["sprites"])

# sprites
self.assertEqual(
response.data["sprites"]["default"],
sprites_data["default"],
"{}".format(sprites_data["default"]),
)

Expand Down Expand Up @@ -4837,8 +4838,6 @@ def test_pokemon_api(self):
"{}/pokemon/{}/".format(API_V2, pokemon.pk), HTTP_HOST="testserver"
)

sprites_data = json.loads(pokemon_sprites.sprites)

self.assertEqual(response.status_code, status.HTTP_200_OK)

# base params
Expand Down Expand Up @@ -5024,12 +5023,15 @@ def test_pokemon_api(self):
response.data["forms"][0]["url"],
"{}{}/pokemon-form/{}/".format(TEST_HOST, API_V2, pokemon_form.pk),
)

sprites_data = json.loads(pokemon_sprites.sprites)

# sprite params
self.assertEqual(
response.data["sprites"]["front_default"],
sprites_data["front_default"],
"{}".format(sprites_data["front_default"]),
)
self.assertEqual(response.data["sprites"]["back_default"], None)
self.assertEqual(sprites_data["back_default"], None)

def test_pokemon_form_api(self):
pokemon_species = self.setup_pokemon_species_data()
Expand All @@ -5040,8 +5042,6 @@ def test_pokemon_form_api(self):
pokemon_form_sprites = self.setup_pokemon_form_sprites_data(pokemon_form)
pokemon_form_type = self.setup_pokemon_form_type_data(pokemon_form)

sprites_data = json.loads(pokemon_form_sprites.sprites)

response = self.client.get(
"{}/pokemon-form/{}/".format(API_V2, pokemon_form.pk),
HTTP_HOST="testserver",
Expand Down Expand Up @@ -5074,12 +5074,15 @@ def test_pokemon_form_api(self):
TEST_HOST, API_V2, pokemon_form.version_group.pk
),
)

sprites_data = json.loads(pokemon_form_sprites.sprites)

# sprite params
self.assertEqual(
response.data["sprites"]["front_default"],
sprites_data["front_default"],
"{}".format(sprites_data["front_default"]),
)
self.assertEqual(response.data["sprites"]["back_default"], None)
self.assertEqual(sprites_data["back_default"], None)
# type params
self.assertEqual(response.data["types"][0]["slot"], pokemon_form_type.slot)
self.assertEqual(
Expand Down
13 changes: 4 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
Django==2.1.15
Django==3.1.14
Unipath==1.1
coverage==4.5.1
django-appconf==1.0.4
django-cors-headers==2.5.3
django-cors-headers==3.11.0
django-discover-runner==1.0
django-redis==4.10.0
django-redis==4.12.1
django-cachalot==2.3.5
django-tastypie==0.14.3
djangorestframework==3.9.4
drf-ujson==1.2.0
djangorestframework==3.14.0
gunicorn==20.1.0
mimeparse==0.1.3
psycopg2-binary==2.9.5
python-dateutil==2.8.1
python-mimeparse==1.6.0
simplejson==3.17.2
six==1.15.0