Skip to content

Feature/public display points table #237

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

Open
wants to merge 16 commits into
base: master
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
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/
25 changes: 14 additions & 11 deletions clubs.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{"clubs":[
{
"clubs": [
{
"name":"Simply Lift",
"email":"john@simplylift.co",
"points":"13"
"name": "Simply Lift",
"email": "john@simplylift.co",
"points": "13"
},
{
"name":"Iron Temple",
"email": "admin@irontemple.com",
"points":"4"
"name": "Iron Temple",
"email": "admin@irontemple.com",
"points": "4"
},
{ "name":"She Lifts",
"email": "kate@shelifts.co.uk",
"points":"12"
{
"name": "She Lifts",
"email": "kate@shelifts.co.uk",
"points": "12"
}
]}
]
}
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
77 changes: 61 additions & 16 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
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 saveClub(clubs):
with open('clubs.json', 'w') as c:
json.dump({'clubs': clubs}, c)


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 +25,80 @@ def loadCompetitions():
competitions = loadCompetitions()
clubs = loadClubs()


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

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

@app.route('/clubs-points')
def clubs_points():
return render_template(
'clubs-points.html',
clubs=clubs
)


@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)
try:
club = \
[club for club in clubs if club['email'] == request.form['email']][
0]
return render_template('welcome.html', club=club,
competitions=competitions)
except IndexError:
flash('Wrong email-please try again')
return render_template('index.html')


@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)

if placesRequired > int(club['points']):
flash('Insufficiant points.')
return render_template('welcome.html', club=club,
competitions=competitions)

club['points'] = str(int(club['points']) - placesRequired)
saveClub(clubs)

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'))
26 changes: 26 additions & 0 deletions templates/clubs-points.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clubs points || GUDLFT</title>
</head>
<body>
<h2>Clubs points</h2>

{% if clubs %}

<table border="1">
<thead><tr><th>Name</th><th>Points</th></tr></thead>
<tbody>
{% for club in clubs %}
<tr><td>{{ club['name'] }}</td><td>{{ club['points'] }}</td></tr>
{% endfor %}
</tbody>
</table>

{% endif %}

<a href="{{ url_for('index') }}">Back</a>

</body>
</html>
11 changes: 11 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
<title>GUDLFT Registration</title>
</head>
<body>

<h1>Welcome to the GUDLFT Registration Portal!</h1>

Please enter your secretary email to continue:
<form action="showSummary" method="post">
<label for="email">Email:</label>
<input type="email" name="email" id=""/>
<button type="submit">Enter</button>
</form>

{% with messages = get_flashed_messages()%}
{% if messages %}
{% for message in messages %}
{{message}}
{% endfor %}
{% endif %}
{% endwith %}

</body>
</html>
24 changes: 13 additions & 11 deletions templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@
<body>
<h2>Welcome, {{club['email']}} </h2><a href="{{url_for('logout')}}">Logout</a>

{% with messages = get_flashed_messages()%}
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif%}
Points available: {{club['points']}}
Points available: {{ club['points'] }}
<h3>Competitions:</h3>
<ul>
{% for comp in competitions%}
{% for comp in competitions %}
<li>
{{comp['name']}}<br />
Date: {{comp['date']}}</br>
Number of Places: {{comp['numberOfPlaces']}}
{%if comp['numberOfPlaces']|int >0%}
<a href="{{ url_for('book',competition=comp['name'],club=club['name']) }}">Book Places</a>
{%endif%}
<p>{{ comp['name'] }}</p>
<p>Date: {{ comp['date'] }}</p>
<p>Number of Places: {{ comp['numberOfPlaces'] }}</p>
{% if comp['numberOfPlaces']|int >0 %}
<a href="{{ url_for('book',competition=comp['name'],club=club['name']) }}">Book Places</a>
{% else %}
<span>- Competition complete</span>
{% endif %}
</li>
<hr />
<hr>
{% endfor %}
</ul>
{%endwith%}
{% endwith %}

</body>
</html>
Empty file added tests/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
import json

import server
from server import app, competitions


@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
yield client


@pytest.fixture
def test_clubs():
with open('tests/test_clubs.json') as c:
return json.load(c)['clubs']


@pytest.fixture
def test_competitions():
with open('tests/test_competitions.json') as comps:
return json.load(comps)['competitions']


@pytest.fixture
def test_competition_full():
competitions[:] = [
{'name': 'Test Comptetition #3',
'date': '2020-03-27 10:00:00',
'numberOfPlaces': '0'}
]


@pytest.fixture
def setup_mocks(mocker, test_clubs, test_competitions):
mocker.patch('server.loadClubs', return_value=test_clubs)
mocker.patch('server.loadCompetitions', return_value=test_competitions)
mocker.patch('server.saveClub')
mocker.patch.object(server, 'clubs', test_clubs)
mocker.patch.object(server, 'competitions', test_competitions)
19 changes: 19 additions & 0 deletions tests/test_clubs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"clubs": [
{
"name": "Test club #1",
"email": "john@test.com",
"points": "13"
},
{
"name": "Test club #2",
"email": "admin@test.com",
"points": "4"
},
{
"name": "Test club #3",
"email": "kate@test.com",
"points": "12"
}
]
}
14 changes: 14 additions & 0 deletions tests/test_competitions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"competitions": [
{
"name": "Test Competition #1",
"date": "2020-03-27 10:00:00",
"numberOfPlaces": "25"
},
{
"name": "Test Competition #2",
"date": "2020-10-22 13:30:00",
"numberOfPlaces": "13"
}
]
}
Loading