Skip to content

Bugfix/max purchase places #1

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

Merged
merged 2 commits into from
Jul 15, 2024
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ bin
include
lib
.Python
tests/
.envrc
__pycache__
__pycache__
/env
.idea/
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
filterwarnings =
ignore::DeprecationWarning
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1

pytest~=8.2.2
49 changes: 33 additions & 16 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import json
from flask import Flask,render_template,request,redirect,flash,url_for
from flask import Flask, render_template, request, redirect, flash, url_for


def loadClubs():
with open('clubs.json') as c:
listOfClubs = json.load(c)['clubs']
return listOfClubs
listOfClubs = json.load(c)['clubs']
return listOfClubs


def loadCompetitions():
with open('competitions.json') as comps:
listOfCompetitions = json.load(comps)['competitions']
return listOfCompetitions
listOfCompetitions = json.load(comps)['competitions']
return listOfCompetitions


app = Flask(__name__)
Expand All @@ -20,40 +20,57 @@ def loadCompetitions():
competitions = loadCompetitions()
clubs = loadClubs()


@app.route('/')
def index():
return render_template('index.html')

@app.route('/showSummary',methods=['POST'])

@app.route('/showSummary', methods=['POST'])
def showSummary():
club = [club for club in clubs if club['email'] == request.form['email']][0]
return render_template('welcome.html',club=club,competitions=competitions)
club = [club for club in clubs if club['email'] == request.form['email']][
0]
return render_template('welcome.html', club=club,
competitions=competitions)


@app.route('/book/<competition>/<club>')
def book(competition,club):
def book(competition, club):
foundClub = [c for c in clubs if c['name'] == club][0]
foundCompetition = [c for c in competitions if c['name'] == competition][0]
if foundClub and foundCompetition:
return render_template('booking.html',club=foundClub,competition=foundCompetition)
return render_template('booking.html', club=foundClub,
competition=foundCompetition)
else:
flash("Something went wrong-please try again")
return render_template('welcome.html', club=club, competitions=competitions)
return render_template('welcome.html', club=club,
competitions=competitions)


@app.route('/purchasePlaces',methods=['POST'])
@app.route('/purchasePlaces', methods=['POST'])
def purchasePlaces():
competition = [c for c in competitions if c['name'] == request.form['competition']][0]
competition = \
[c for c in competitions if c['name'] == request.form['competition']][0]
club = [c for c in clubs if c['name'] == request.form['club']][0]

placesRequired = int(request.form['places'])
competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired

if placesRequired > 12:
flash('Max purchase 12.')
return render_template('welcome.html', club=club,
competitions=competitions)

competition['numberOfPlaces'] = int(
competition['numberOfPlaces']) - placesRequired
flash('Great-booking complete!')
return render_template('welcome.html', club=club, competitions=competitions)

return render_template('welcome.html', club=club,
competitions=competitions)


# TODO: Add route for points display


@app.route('/logout')
def logout():
return redirect(url_for('index'))
return redirect(url_for('index'))
Empty file added tests/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest

from server import app, loadClubs, loadCompetitions


@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
yield client
39 changes: 39 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from server import loadCompetitions, loadClubs


def test_purchase_places(client):

clubs = loadClubs()
competitions = loadCompetitions()

test_club = clubs[0]
test_competition = competitions[0]
places_to_purchase = 8

response = client.post('/purchasePlaces', data={
'club': test_club['name'],
'competition': test_competition['name'],
'places': places_to_purchase
})

assert response.status_code == 200
assert b'Great-booking complete!' in response.data


def test_max_purchase_places(client):

clubs = loadClubs()
competitions = loadCompetitions()

test_club = clubs[0]
test_competition = competitions[0]
places_to_purchase = 28

response = client.post('/purchasePlaces', data={
'club': test_club['name'],
'competition': test_competition['name'],
'places': places_to_purchase
})

assert response.status_code == 200
assert b'Max purchase 12.' in response.data