From 474a74d51cf4727b5e5992fe22530559c7126b7b Mon Sep 17 00:00:00 2001 From: Julien Denizot Date: Wed, 6 Nov 2024 14:58:22 +0100 Subject: [PATCH 1/2] Prevent booking for past competitions and display booking page only for upcoming events --- .gitignore | 3 ++- server.py | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 2cba99d87..e2d7aa6dc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ lib .Python tests/ .envrc -__pycache__ \ No newline at end of file +__pycache__ +pyvenv.cfg diff --git a/server.py b/server.py index 4084baeac..ffcdadcbd 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,6 @@ import json from flask import Flask,render_template,request,redirect,flash,url_for +from datetime import datetime def loadClubs(): @@ -31,15 +32,17 @@ def showSummary(): @app.route('/book//') -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) - else: - flash("Something went wrong-please try again") +def book(competition, club): + competition = next((c for c in competitions if c['name'] == competition), None) + club = next((c for c in clubs if c['name'] == club), None) + + competition_date = datetime.strptime(competition['date'], "%Y-%m-%d %H:%M:%S") + if competition_date < datetime.now(): + flash("You cannot book places for a past competition.") return render_template('welcome.html', club=club, competitions=competitions) + return render_template('booking.html', club=club, competition=competition) + @app.route('/purchasePlaces',methods=['POST']) def purchasePlaces(): @@ -56,4 +59,4 @@ def purchasePlaces(): @app.route('/logout') def logout(): - return redirect(url_for('index')) \ No newline at end of file + return redirect(url_for('index')) From e94e448feba62ecaa5c9bcb73eaf4d3f15ec27db Mon Sep 17 00:00:00 2001 From: Julien Denizot Date: Wed, 13 Nov 2024 10:10:16 +0100 Subject: [PATCH 2/2] Add test to prevent booking for past competitions --- tests/unit/test_purchase_places.py | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/unit/test_purchase_places.py diff --git a/tests/unit/test_purchase_places.py b/tests/unit/test_purchase_places.py new file mode 100644 index 000000000..2249fd3e5 --- /dev/null +++ b/tests/unit/test_purchase_places.py @@ -0,0 +1,34 @@ +import sys +import os +import pytest +sys.path.insert( + 0, os.path.abspath( + os.path.join(os.path.dirname(__file__), '../../') + ) +) + +from server import app, clubs, competitions # noqa + + +@pytest.fixture +def client(): + app.config['TESTING'] = True + with app.test_client() as client: + yield client + + +def test_book_past_competition(client): + competition = next( + c for c in competitions if c['name'] == 'Spring Festival' + ) + competition['date'] = "2020-03-27 10:00:00" + + response = client.get( + '/book/Spring Festival/Simply Lift', follow_redirects=True + ) + + assert b"You cannot book places for a past competition." in response.data + + club = next(c for c in clubs if c['name'] == 'Simply Lift') + assert club['points'] == "13" + assert competition['numberOfPlaces'] == "25"