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

Add copilot csrf #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions pokedex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ def subscribe():
return redirect(url_for("index"))


@app.route("/<pokemon_id>")
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"))

#add a route that calls prefix_pokemon_name
@app.route("/prefix/<name>")
def prefix_name(name):
prefix = request.args.get("prefix")
return helper.prefix_pokemon_name(name, prefix)



def get_db():
if "db" not in g:
g.db = helper.ConnectionWrapper(app.config["DATABASE"])
Expand Down
34 changes: 34 additions & 0 deletions pokedex/helper.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import re
import sqlite3

from pokedex.utils import is_pikachu


class ConnectionWrapper:
"""A simple connection wrapper class"""

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()
Expand All @@ -29,10 +35,38 @@ 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()

#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"(.*)@(.*\..*)")
if not pattern.match(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
2 changes: 1 addition & 1 deletion pokedex/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1>Pokedex</h1>
{% for i in range(pokemon | length) %}
<div class="col-6 col-md-4 col-lg-3 p-2">
<img alt="" src="{{ pokemon[i].image_url}}" />
<div>{{ pokemon[i].pokemon_name }} #{{ i + 1 }}</div>
<div><a href="{{ url_for("get_pokemon", pokemon_id=pokemon[i].id) }}" class="pk-li p-3">{{ pokemon[i].pokemon_name }} #{{ i + 1 }}</a></div>
</div>
{% endfor %}
</div>
Expand Down
16 changes: 16 additions & 0 deletions pokedex/templates/pokemon.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block styles %}
{{ super() }}
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static', filename='css/style.css') }}">
{% endblock %}
{% block content %}
<div class="full-page d-flex flex-wrap justify-content-center align-items-center">
<div class="container d-flex flex-column flex-wrap justify-content-start align-items-center text-center py-5 m-auto margin-mobile">
<h1>{{ name }} # {% autoescape false %} {{ pokemon_id }} {% endautoescape %}</h1>
<div class="flex-row container d-flex flex-wrap justify-content-center align-items-center text-center mt-2">
<img src="{{ sprites[0] }}" alt="">
</div>
<p>{{ description }}</p>
</div>
</div>
{% endblock %}
2 changes: 2 additions & 0 deletions pokedex/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def is_pikachu(pokemon_id, pokemon_name):
return pokemon_id == 25 and pokemon_name == "Pikachu"
6 changes: 6 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ def test_index(client):
def test_subscribe(client):
response = client.post("/subscribe", data={"email": "[email protected]"})
assert response.status_code == 302


def test_pokemon(client):
response = client.get("/42")
assert response.status_code == 200
assert b"MockDescription" in response.data
7 changes: 7 additions & 0 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading