From 9b4bd667f5f7322b5e9b2fb03e165c589cab6f17 Mon Sep 17 00:00:00 2001 From: Guillaume Dequenne Date: Tue, 19 Apr 2022 11:40:06 +0200 Subject: [PATCH 1/3] Add awesome new feature --- pokedex/app.py | 15 +++++++++++++++ pokedex/helper.py | 17 +++++++++++++++++ pokedex/templates/index.html | 2 +- pokedex/templates/pokemon.html | 16 ++++++++++++++++ pokedex/utils.py | 2 ++ tests/test_app.py | 6 ++++++ tests/test_helper.py | 7 +++++++ 7 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 pokedex/templates/pokemon.html create mode 100644 pokedex/utils.py diff --git a/pokedex/app.py b/pokedex/app.py index bddc552..5935c3f 100644 --- a/pokedex/app.py +++ b/pokedex/app.py @@ -23,6 +23,21 @@ def subscribe(): return redirect(url_for("index")) +@app.route("/") +def get_pokemon(pokemon_id: str): + try: + _, pokemon_name, image_url, description = helper.fetch_pokemon(get_db(), pokemon_id) + return render_template( + "pokemon.html", + description=description, + sprites=[image_url], + name=pokemon_name, + pokemon_id=pokemon_id, + ) + except: + return redirect(url_for("index")) + + def get_db(): if "db" not in g: g.db = helper.ConnectionWrapper(app.config["DATABASE"]) diff --git a/pokedex/helper.py b/pokedex/helper.py index 4db91b3..90212ed 100644 --- a/pokedex/helper.py +++ b/pokedex/helper.py @@ -1,6 +1,8 @@ import re import sqlite3 +from pokedex.utils import is_pikachu + class ConnectionWrapper: """A simple connection wrapper class""" @@ -8,6 +10,10 @@ class ConnectionWrapper: def __init__(self, db_path): self.__conn = sqlite3.connect(db_path) + def get_single_pokemon(self, pokemon_id): + statement = f"SELECT * FROM POKEDEX WHERE id = '{pokemon_id}'" + return self.__conn.execute(statement).fetchone() + def get_all_pokemons(self): statement = "SELECT * FROM POKEDEX" return self.__conn.execute(statement).fetchall() @@ -36,3 +42,14 @@ def register_subscriber(wrapper: ConnectionWrapper, email): ValueError("Invalid email!") wrapper.register_subscriber(email) pass + + +def fetch_pokemon(wrapper: ConnectionWrapper, pokemon_id: str): + result = wrapper.get_single_pokemon(pokemon_id) + if result is None: + raise "Pokemon not found" + if pokemon_id == 25: + if is_pikachu(result): + # Team Rocket is trying to steal Pikachu (#25)! + result = ("", "We stole Pikachu!", "", "") + return result diff --git a/pokedex/templates/index.html b/pokedex/templates/index.html index 4d0f219..f790a89 100644 --- a/pokedex/templates/index.html +++ b/pokedex/templates/index.html @@ -8,7 +8,7 @@

Pokedex

{% for i in range(pokemon | length) %}
-
{{ pokemon[i].pokemon_name }} #{{ i + 1 }}
+
{{ pokemon[i].pokemon_name }} #{{ i + 1 }}
{% endfor %} diff --git a/pokedex/templates/pokemon.html b/pokedex/templates/pokemon.html new file mode 100644 index 0000000..2ed0db4 --- /dev/null +++ b/pokedex/templates/pokemon.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} +{% block styles %} + {{ super() }} + +{% endblock %} +{% block content %} +
+
+

{{ name }} # {% autoescape false %} {{ pokemon_id }} {% endautoescape %}

+
+ +
+

{{ description }}

+
+
+{% endblock %} diff --git a/pokedex/utils.py b/pokedex/utils.py new file mode 100644 index 0000000..d4743f1 --- /dev/null +++ b/pokedex/utils.py @@ -0,0 +1,2 @@ +def is_pikachu(pokemon_id, pokemon_name): + return pokemon_id == 25 and pokemon_name == "Pikachu" diff --git a/tests/test_app.py b/tests/test_app.py index 108e7f3..113f469 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -7,3 +7,9 @@ def test_index(client): def test_subscribe(client): response = client.post("/subscribe", data={"email": "blah@example.com"}) assert response.status_code == 302 + + +def test_pokemon(client): + response = client.get("/42") + assert response.status_code == 200 + assert b"MockDescription" in response.data diff --git a/tests/test_helper.py b/tests/test_helper.py index cc01fda..348f2c4 100644 --- a/tests/test_helper.py +++ b/tests/test_helper.py @@ -7,3 +7,10 @@ def test_fetch_all_pokemon(): connection_wrapper_mock = Mock() connection_wrapper_mock.get_all_pokemons.return_value = [(1, "Foo", "", "Bar")] assert len(helper.fetch_all_pokemons(connection_wrapper_mock)) == 1 + + +def test_fetch_pokemon(): + connection_wrapper_mock = Mock() + mock_pokemon = (1, "Foo", "", "Bar") + connection_wrapper_mock.get_single_pokemon.return_value = mock_pokemon + assert helper.fetch_pokemon(connection_wrapper_mock, "1") == mock_pokemon From c39f45d21121a7b821e841b608805e08063e3f91 Mon Sep 17 00:00:00 2001 From: Tom Howlett Date: Sun, 7 Apr 2024 18:51:54 +0100 Subject: [PATCH 2/3] updated class name for new coding standard --- pokedex/helper.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pokedex/helper.py b/pokedex/helper.py index 90212ed..5b30ef7 100644 --- a/pokedex/helper.py +++ b/pokedex/helper.py @@ -35,6 +35,19 @@ def cleanup(self, should_close: bool): def fetch_all_pokemons(wrapper: ConnectionWrapper): return wrapper.get_all_pokemons() +#add a method call fetch_pokemon_by_name that accepts a name and returns the pokemon with that name +def fetch_pokemon_by_name(wrapper: ConnectionWrapper, pokemon_name: str): + statement = f"SELECT * FROM POKEDEX WHERE name = '{pokemon_name}'" + result = wrapper.__conn.execute(statement).fetchone() + if result is None: + raise ValueError("Pokemon not found") + return result + +#add a method call fetch_pokemon_by_start that accepts a letter and returns all the pokemon whose name starts with that letter +def fetch_pokemon_by_start(wrapper: ConnectionWrapper, start_letter: str): + statement = f"SELECT * FROM POKEDEX WHERE name LIKE '{start_letter}%'" + return wrapper.__conn.execute(statement).fetchall() + def register_subscriber(wrapper: ConnectionWrapper, email): pattern = re.compile(r"(.*)@(.*\..*)") From a30f89a274d04e95a5f8b2f496df6afb21395d7e Mon Sep 17 00:00:00 2001 From: Tom Howlett Date: Sun, 7 Apr 2024 20:02:31 +0100 Subject: [PATCH 3/3] added prefix method --- pokedex/app.py | 7 +++++++ pokedex/helper.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/pokedex/app.py b/pokedex/app.py index 5935c3f..01dbbf8 100644 --- a/pokedex/app.py +++ b/pokedex/app.py @@ -36,6 +36,13 @@ def get_pokemon(pokemon_id: str): ) except: return redirect(url_for("index")) + +#add a route that calls prefix_pokemon_name +@app.route("/prefix/") +def prefix_name(name): + prefix = request.args.get("prefix") + return helper.prefix_pokemon_name(name, prefix) + def get_db(): diff --git a/pokedex/helper.py b/pokedex/helper.py index 5b30ef7..f8e2e3a 100644 --- a/pokedex/helper.py +++ b/pokedex/helper.py @@ -48,6 +48,10 @@ def fetch_pokemon_by_start(wrapper: ConnectionWrapper, start_letter: str): statement = f"SELECT * FROM POKEDEX WHERE name LIKE '{start_letter}%'" return wrapper.__conn.execute(statement).fetchall() +#add a method to prefix a pokemon name with a given string and return the new name +def prefix_pokemon_name(name: str, prefix: str): + return prefix + name + def register_subscriber(wrapper: ConnectionWrapper, email): pattern = re.compile(r"(.*)@(.*\..*)")