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 f54afe489ffd5d3362915b695571ef00a254fc82 Mon Sep 17 00:00:00 2001 From: colin-sonarsource Date: Thu, 18 Jul 2024 10:50:31 +0200 Subject: [PATCH 2/3] Update python-app.yml --- .github/workflows/python-app.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index d90ef26..21c767c 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -39,3 +39,8 @@ jobs: - name: Test with pytest run: | pytest + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} From 73447dd5cc68b9358a3af3a76638e68ecfe64fe4 Mon Sep 17 00:00:00 2001 From: colin-sonarsource Date: Thu, 18 Jul 2024 10:51:02 +0200 Subject: [PATCH 3/3] Create sonar-project.properties --- sonar-project.properties | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 sonar-project.properties diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..eaf48a2 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,13 @@ +sonar.projectKey=colin-sonarsource_python-flask-demo +sonar.organization=colin-sonarsource + +# This is the name and version displayed in the SonarCloud UI. +#sonar.projectName=python-flask-demo +#sonar.projectVersion=1.0 + + +# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. +#sonar.sources=. + +# Encoding of the source code. Default is default system encoding +#sonar.sourceEncoding=UTF-8