Skip to content

Commit 5a66e2c

Browse files
committed
(fix) Linting
1 parent 7069a65 commit 5a66e2c

File tree

6 files changed

+111
-47
lines changed

6 files changed

+111
-47
lines changed

.flake8

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[flake8]
2+
max-line-length = 80
3+
exclude =
4+
bin/activate.py,
5+
lib,
6+
packages,
7+
migrations,
8+
build,
9+
dist,
10+
*.pyc,
11+
__pycache__,
12+
bin/activate_this.py

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
line-length = 80

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ itsdangerous==1.1.0
44
Jinja2==2.11.2
55
MarkupSafe==1.1.1
66
Werkzeug==1.0.1
7-
pytest
7+
pytest
8+
black
9+
flake8

server.py

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,80 @@
11
import json
2-
from flask import Flask,render_template,request,redirect,flash,url_for
2+
from flask import Flask, render_template, request, redirect, flash, url_for
33

44

55
def loadClubs():
6-
with open('clubs.json') as c:
7-
listOfClubs = json.load(c)["clubs"]
8-
return listOfClubs
6+
with open("clubs.json") as c:
7+
listOfClubs = json.load(c)["clubs"]
8+
return listOfClubs
99

1010

1111
def loadCompetitions():
12-
with open('competitions.json') as comps:
13-
listOfCompetitions = json.load(comps)["competitions"]
14-
return listOfCompetitions
12+
with open("competitions.json") as comps:
13+
listOfCompetitions = json.load(comps)["competitions"]
14+
return listOfCompetitions
1515

1616

1717
app = Flask(__name__)
18-
app.secret_key = 'something_special'
18+
app.secret_key = "something_special"
1919

2020
competitions = loadCompetitions()
2121
clubs = loadClubs()
2222

23-
@app.route('/')
23+
24+
@app.route("/")
2425
def index():
25-
return render_template('index.html')
26+
return render_template("index.html")
27+
2628

27-
@app.route('/showSummary',methods=['POST'])
29+
@app.route("/showSummary", methods=["POST"])
2830
def showSummary():
29-
club = [club for club in clubs if club['email'] == request.form['email']][0]
30-
return render_template('welcome.html',club=club,competitions=competitions)
31+
club = [club for club in clubs if club["email"] == request.form["email"]][0]
32+
return render_template("welcome.html", club=club, competitions=competitions)
3133

3234

33-
@app.route('/book/<competition>/<club>')
34-
def book(competition,club):
35-
foundClub = [c for c in clubs if c['name'] == club][0]
36-
foundCompetition = [c for c in competitions if c['name'] == competition][0]
35+
@app.route("/book/<competition>/<club>")
36+
def book(competition, club):
37+
foundClub = [c for c in clubs if c["name"] == club][0]
38+
foundCompetition = [c for c in competitions if c["name"] == competition][0]
3739
if foundClub and foundCompetition:
38-
return render_template('booking.html',club=foundClub,competition=foundCompetition)
40+
return render_template(
41+
"booking.html", club=foundClub, competition=foundCompetition
42+
)
3943
else:
4044
flash("Something went wrong-please try again")
41-
return render_template('welcome.html', club=club, competitions=competitions)
45+
return render_template(
46+
"welcome.html", club=club, competitions=competitions
47+
)
4248

4349

44-
@app.route('/purchasePlaces',methods=['POST'])
50+
@app.route("/purchasePlaces", methods=["POST"])
4551
def purchasePlaces():
46-
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
47-
club = [c for c in clubs if c['name'] == request.form['club']][0]
48-
if not request.form['places'] or int(request.form['places']) < 1 :
52+
competition = [
53+
c for c in competitions if c["name"] == request.form["competition"]
54+
][0]
55+
club = [c for c in clubs if c["name"] == request.form["club"]][0]
56+
if not request.form["places"] or int(request.form["places"]) < 1:
4957
flash("Places required must be a positive integer")
50-
return redirect(url_for('book',competition=competition['name'],club=club['name']))
51-
placesRequired = int(request.form['places'])
52-
if placesRequired > int(club['points']):
58+
return redirect(
59+
url_for("book", competition=competition["name"], club=club["name"])
60+
)
61+
placesRequired = int(request.form["places"])
62+
if placesRequired > int(club["points"]):
5363
flash("Places required exceed club's total points")
54-
return redirect(url_for('book',competition=competition['name'],club=club['name']))
55-
competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesRequired
56-
club['points'] = int(club['points']) - placesRequired
57-
flash('Great-booking complete!')
58-
return render_template('welcome.html', club=club, competitions=competitions)
64+
return redirect(
65+
url_for("book", competition=competition["name"], club=club["name"])
66+
)
67+
competition["numberOfPlaces"] = (
68+
int(competition["numberOfPlaces"]) - placesRequired
69+
)
70+
club["points"] = int(club["points"]) - placesRequired
71+
flash("Great-booking complete!")
72+
return render_template("welcome.html", club=club, competitions=competitions)
5973

6074

6175
# TODO: Add route for points display
6276

6377

64-
@app.route('/logout')
78+
@app.route("/logout")
6579
def logout():
66-
return redirect(url_for('index'))
80+
return redirect(url_for("index"))

templates/booking.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!DOCTYPE html>
12
<html lang="en">
23
<head>
34
<meta charset="UTF-8">
@@ -19,7 +20,7 @@ <h2>{{competition['name']}}</h2>
1920
<form action="/purchasePlaces" method="post">
2021
<input type="hidden" name="club" value="{{club['name']}}">
2122
<input type="hidden" name="competition" value="{{competition['name']}}">
22-
<label for="places">How many places?</label><input type="number" name="places" id=""/>
23+
<label for="places">How many places?</label><input type="number" name="places" id="places"/>
2324
<button type="submit">Book</button>
2425
</form>
2526
</body>

tests/test_endpoints.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,74 @@
22
from server import app
33
import html
44

5+
56
@pytest.fixture
67
def client():
78
return app.test_client()
89

10+
911
def test_purchasePlaces_available_points(client):
10-
response = client.post("/purchasePlaces", data = {"places":1,"competition":"Fall Classic","club":"Iron Temple"})
12+
response = client.post(
13+
"/purchasePlaces",
14+
data={
15+
"places": 1,
16+
"competition": "Fall Classic",
17+
"club": "Iron Temple",
18+
},
19+
)
1120
assert response.status_code == 200
12-
assert b'Great-booking complete!' in response.data
21+
assert b"Great-booking complete!" in response.data
22+
1323

1424
def test_purchasePlaces_unavailable_points(client):
15-
response = client.post("/purchasePlaces", data = {"places":5,"competition":"Fall Classic","club":"Iron Temple"})
25+
response = client.post(
26+
"/purchasePlaces",
27+
data={
28+
"places": 5,
29+
"competition": "Fall Classic",
30+
"club": "Iron Temple",
31+
},
32+
)
1633
assert response.status_code == 302
1734
response_redirect = client.get("/book/Fall%20Classic/Iron%20Temple")
1835
# Byte string to string into a specific encoding utf-8
19-
response_data = response_redirect.data.decode('UTF-8')
36+
response_data = response_redirect.data.decode("UTF-8")
2037
# Html entities to actual special char (" ' "), human readable
21-
converted_str = html.unescape(response_data)
38+
converted_str = html.unescape(response_data)
2239
assert "Places required exceed club's total points" in converted_str
2340

41+
2442
def test_purchasePlaces_negative_input(client):
25-
response = client.post("/purchasePlaces", data = {"places":-1,"competition":"Fall Classic","club":"Iron Temple"})
43+
response = client.post(
44+
"/purchasePlaces",
45+
data={
46+
"places": -1,
47+
"competition": "Fall Classic",
48+
"club": "Iron Temple",
49+
},
50+
)
2651
assert response.status_code == 302
2752
response_redirect = client.get("/book/Fall%20Classic/Iron%20Temple")
2853
# Byte string to string into a specific encoding utf-8
29-
response_data = response_redirect.data.decode('UTF-8')
54+
response_data = response_redirect.data.decode("UTF-8")
3055
# Html entities to actual special char (" ' "), human readable
31-
converted_str = html.unescape(response_data)
56+
converted_str = html.unescape(response_data)
3257
assert "Places required must be a positive integer" in converted_str
3358

59+
3460
def test_purchasePlaces_none_input(client):
35-
response = client.post("/purchasePlaces", data = {"places": "","competition":"Fall Classic","club":"Iron Temple"})
61+
response = client.post(
62+
"/purchasePlaces",
63+
data={
64+
"places": "",
65+
"competition": "Fall Classic",
66+
"club": "Iron Temple",
67+
},
68+
)
3669
assert response.status_code == 302
3770
response_redirect = client.get("/book/Fall%20Classic/Iron%20Temple")
3871
# Byte string to string into a specific encoding utf-8
39-
response_data = response_redirect.data.decode('UTF-8')
72+
response_data = response_redirect.data.decode("UTF-8")
4073
# Html entities to actual special char (" ' "), human readable
41-
converted_str = html.unescape(response_data)
42-
assert "Places required must be a positive integer" in converted_str
74+
converted_str = html.unescape(response_data)
75+
assert "Places required must be a positive integer" in converted_str

0 commit comments

Comments
 (0)