Coverage for server.py: 94%
65 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-09 17:52 +0200
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-09 17:52 +0200
1import json
2from flask import Flask,render_template,request,redirect,flash,url_for,session
3from datetime import datetime
6def loadClubs():
7 with open('clubs.json') as c:
8 listOfClubs = json.load(c)['clubs']
9 return listOfClubs
12def loadCompetitions():
13 with open('competitions.json') as comps:
14 listOfCompetitions = json.load(comps)['competitions']
15 return listOfCompetitions
18app = Flask(__name__)
19app.secret_key = 'something_special'
21competitions = loadCompetitions()
22clubs = loadClubs()
23now = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
26@app.route('/')
27def index():
28 return render_template('index.html')
30@app.route('/showSummary',methods=['POST'])
31def showSummary():
32 try:
33 club = [club for club in clubs if club['email'] == request.form['email']][0]
34 return render_template('welcome.html', club=club, competitions=competitions, now=now)
35 except IndexError:
36 flash("Sorry, that email wasn't found")
37 return redirect(url_for('index'))
39@app.route('/book/<competition>/<club>')
40def book(competition,club):
41 foundClub = [c for c in clubs if c['name'] == club][0]
42 foundCompetition = [c for c in competitions if c['name'] == competition][0]
43 if foundClub and foundCompetition:
44 if foundCompetition['date'] > now:
45 return render_template('booking.html',club=foundClub,competition=foundCompetition)
46 else:
47 flash("You cannot book for a past competition")
48 return render_template('welcome.html', club=foundClub,
49 competitions=competitions, now=now)
50 else:
51 flash("Something went wrong-please try again")
52 return render_template('welcome.html', club=club, competitions=competitions, now=now)
55@app.route('/purchasePlaces',methods=['POST'])
56def purchasePlaces():
57 competition = [c for c in competitions if c['name'] == request.form['competition']][0]
58 club = [c for c in clubs if c['name'] == request.form['club']][0]
59 placesRequired = int(request.form['places'])
60 # check if that's the first book, and update session
61 if competition['name'] in session:
62 places = {competition['name']: session[competition['name']] + placesRequired}
63 else:
64 places = {competition['name']: placesRequired}
65 points = int(club['points'])
66 # prevent to book more than 12 places
67 if placesRequired <= 12:
68 if places[competition['name']] <= 12:
69 # prevent to book more than available points
70 if placesRequired <= points:
71 competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesRequired
72 club['points'] = int(club['points']) - placesRequired
73 # set the session if that's the first book for this competition
74 if not competition['name'] in session:
75 session[competition['name']] = placesRequired
76 flash(f"Great ! {placesRequired} places booked for {competition['name']}")
77 else:
78 flash("You don't have enough points")
79 else:
80 flash(f"You already booked 12 places for {competition['name']}")
81 else:
82 flash("You can't book more than 12 places")
83 return render_template('welcome.html', club=club,
84 competitions=competitions, now=now)
86# TODO: Add route for points display
87@app.route('/points')
88def displayPoints():
89 return render_template('points.html', clubs=clubs)
91@app.route('/logout')
92def logout():
93 return redirect(url_for('index'))