Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Bugfix/cannot book completed competition #238

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
47df654
first commit : purchase fix
githubstevemas Jul 13, 2024
6a03829
Fixed max purchase limit
githubstevemas Jul 15, 2024
6db6b83
Merge pull request #1 from githubstevemas/bugfix/max-purchase-places
githubstevemas Jul 15, 2024
3da7f19
Fixed purchase when insufficiant club points
githubstevemas Jul 15, 2024
0e3df6e
Merge pull request #2 from githubstevemas/bugfix/purchase-with-insuff…
githubstevemas Jul 15, 2024
17287a6
Fixed update available club point after purchase
githubstevemas Jul 15, 2024
540ef67
Merge pull request #3 from githubstevemas/bugfix/update-available-clu…
githubstevemas Jul 15, 2024
5b708aa
Fixed wrong email login
githubstevemas Jul 15, 2024
34d8de5
Merge pull request #4 from githubstevemas/bugfix/wrong-email-login
githubstevemas Jul 15, 2024
4f7fa8a
Update test_server.py
githubstevemas Jul 16, 2024
e350c66
Merge pull request #5 from githubstevemas/bugfix/update-available-clu…
githubstevemas Jul 16, 2024
2d98a60
Display when competition is full
githubstevemas Jul 16, 2024
59d2611
Merge branch 'master' into enhancement/display-full-booked-message
githubstevemas Jul 16, 2024
acf85f6
Merge pull request #6 from githubstevemas/enhancement/display-full-bo…
githubstevemas Jul 16, 2024
83a43e9
Added public display of points
githubstevemas Jul 17, 2024
dcf9168
Merge branch 'master' into feature/public-display-points-table
githubstevemas Jul 17, 2024
6ab9a66
Merge pull request #7 from githubstevemas/feature/public-display-poin…
githubstevemas Jul 17, 2024
28200be
Fixed purchase on completed competition
githubstevemas Jul 19, 2024
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
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@ bin
include
lib
.Python
tests/
.envrc
__pycache__
__pycache__
/env
.idea/
/flake8-report
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
@@ -4,3 +4,5 @@ itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1

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

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__)
@@ -20,40 +27,92 @@ def loadCompetitions():
competitions = loadCompetitions()
clubs = loadClubs()


@app.template_filter('is_date_passed')
def is_date_passed(date_str):
date_format = "%Y-%m-%d %H:%M:%S"
date_to_check = datetime.strptime(date_str, date_format)
current_date = datetime.now()
return date_to_check < current_date


@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,
datetime=datetime
)
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
@@ -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>
30 changes: 19 additions & 11 deletions templates/welcome.html
Original file line number Diff line number Diff line change
@@ -7,30 +7,38 @@
<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>

{% if comp['date']|is_date_passed %}
<p>Competition over</p>
{% else %}
<p>Date: {{ comp['date'] }}</p>
{% endif %}

<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": "2029-03-27 10:00:00",
"numberOfPlaces": "25"
},
{
"name": "Test Competition #2",
"date": "2020-10-22 13:30:00",
"numberOfPlaces": "13"
}
]
}
Loading