forked from OpenClassrooms-Student-Center/Python_Testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
119 lines (89 loc) · 3.87 KB
/
server.py
File metadata and controls
119 lines (89 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json
from flask import Flask, render_template, request, redirect, flash, url_for
from utilities.has_enough_places import has_enough_places
from utilities.cannot_book_more_places_than_availables import (
cannot_book_more_places_than_availables,
)
from utilities.cannot_book_more_than_12_places import (
cannot_book_more_than_12_places
)
from utilities.cannot_book_places_in_past_competition import (
cannot_book_places_in_past_competition,
)
from utilities.save_club_points_db import save_club_points_db
from utilities.save_competitions_points_db import save_competitions_points_db
def loadClubs():
with open("clubs.json") as c:
listOfClubs = json.load(c)["clubs"]
return listOfClubs
def loadCompetitions():
with open("competitions.json") as comps:
listOfCompetitions = json.load(comps)["competitions"]
return listOfCompetitions
app = Flask(__name__)
app.secret_key = "something_special"
competitions = loadCompetitions()
clubs = loadClubs()
@app.route("/")
def index():
return render_template("index.html")
@app.route("/showSummary", methods=["POST"])
def showSummary():
matching_club = [club for club in clubs if club["email"] == request.form["email"]]
# verification d'existance de l'email et si non redirection
if not matching_club:
flash("sorry, email not found")
return redirect("/")
else:
club = matching_club[0]
return render_template("welcome.html", club=club, competitions=competitions)
@app.route("/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
)
else:
flash("Something went wrong-please try again")
return render_template("welcome.html", club=club, competitions=competitions)
@app.route("/purchasePlaces", methods=["POST"])
def purchasePlaces():
competition = next(
(c for c in competitions if c["name"] == request.form["competition"]), None
)
club = next((c for c in clubs if c["name"] == request.form["club"]), None)
placesRequired = int(request.form["places"])
errors = [] # Liste pour stocker tous les messages d'erreur
if not has_enough_places(placesRequired, club):
errors.append("You can not book more places than your points allow.")
if not cannot_book_more_places_than_availables(placesRequired, competition):
errors.append("You can not book more places than availables")
if not cannot_book_more_than_12_places(placesRequired):
errors.append("You can not book more than 12 places.")
if not cannot_book_places_in_past_competition(competition):
errors.append("You cannot book places for a past competition.")
# Si au moins une erreur a été détectée, on les affiche et on retourne
if errors:
for error in errors:
flash(error)
return render_template("welcome.html", club=club, competitions=competitions)
# Mise à jour des données
competition["numberOfPlaces"] = int(competition["numberOfPlaces"]) - placesRequired
club["points"] = int(club["points"]) - placesRequired
# Sauvegarde dans le fichier JSON
save_club_points_db(clubs)
# Sauvegarde des données des competitions dans les fichiers JSON
save_competitions_points_db(competitions)
flash("Great-booking complete!", "success")
return render_template("welcome.html", club=club, competitions=competitions)
@app.route("/points_display")
def points_display():
"""
Affiche une page avec la liste des clubs et leurs points actuels.
"""
return render_template("points_display.html", clubs=clubs)
@app.route("/logout")
def logout():
return redirect(url_for("index"))