Skip to content

Feature/implement points display board #231

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 13 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
Binary file added .coverage
Binary file not shown.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[run]
omit =
lib/
bin/
*/__init__.py
tests/*
12 changes: 12 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[flake8]
max-line-length = 80
exclude =
bin/activate.py,
lib,
packages,
migrations,
build,
dist,
*.pyc,
__pycache__,
bin/activate_this.py
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__
pyvenv.cfg
.DS_Store
35 changes: 19 additions & 16 deletions clubs.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{"clubs":[
{
"name":"Simply Lift",
"email":"john@simplylift.co",
"points":"13"
},
{
"name":"Iron Temple",
"email": "admin@irontemple.com",
"points":"4"
},
{ "name":"She Lifts",
"email": "kate@shelifts.co.uk",
"points":"12"
}
]}
{
"clubs": [
{
"name": "Simply Lift",
"email": "john@simplylift.co",
"points": "10"
},
{
"name": "Iron Temple",
"email": "admin@irontemple.com",
"points": "4"
},
{
"name": "She Lifts",
"email": "kate@shelifts.co.uk",
"points": "12"
}
]
}
5 changes: 5 additions & 0 deletions competitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"name": "Fall Classic",
"date": "2020-10-22 13:30:00",
"numberOfPlaces": "13"
},
{
"name": "New",
"date": "2024-10-22 13:30:00",
"numberOfPlaces": "13"
}
]
}
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
line-length = 80
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]

testpaths = tests
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1
pytest
black
flake8
204 changes: 168 additions & 36 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,191 @@
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, session)

app = Flask(__name__)
app.secret_key = "something_special"
app.config["CLUB_FILE"] = "clubs.json"
app.config["COMPETITIONS_FILE"] = "competitions.json"


def loadClubs():
with open('clubs.json') as c:
listOfClubs = json.load(c)['clubs']
return listOfClubs
with open(app.config["CLUB_FILE"]) as c:
listOfClubs = json.load(c)["clubs"]
return listOfClubs


def loadCompetitions():
with open('competitions.json') as comps:
listOfCompetitions = json.load(comps)['competitions']
return listOfCompetitions
with open(app.config["COMPETITIONS_FILE"], "r") as comps:
listOfCompetitions = json.load(comps)["competitions"]
return listOfCompetitions


app = Flask(__name__)
app.secret_key = 'something_special'

competitions = loadCompetitions()
clubs = loadClubs()

@app.route('/')

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

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

def get_club_from_email(email):
try:
club = [club for club in clubs if club["email"] == email][0]
return club
except IndexError:
return None


@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)
@app.route("/showSummary", methods=["POST", "GET"])
def showSummary():
if request.method == "POST":
club = get_club_from_email(request.form["email"])
if club:
session["club_email"] = club["email"]
return render_template(
"welcome.html", club=club, competitions=competitions
)
else:
flash("Sorry, that email wasn't found.")
return redirect(url_for("index"))
else:
if session:
club = [
club for club in clubs if club["email"] == session["club_email"]
][0]
return render_template(
"welcome.html", club=club, competitions=competitions
)
else:
flash("No session")
return redirect(url_for("index"))


@app.route("/display-points")
def display_points():
return render_template("display_points.html", clubs=clubs)


def validate_competition_date(competition):
competition_date = datetime.strptime(
competition["date"], "%Y-%m-%d %H:%M:%S"
)
if competition_date < datetime.now():
return "This competition is already over. You cannot book a place."


@app.route("/book/<competition>/<club>")
def book(competition, club):
foundClub = get_club_from_name(club)
foundCompetition = get_competition_from_name(competition)
if not foundClub or not foundCompetition:
flash("Something went wrong-please try again")
return render_template('welcome.html', club=club, competitions=competitions)


@app.route('/purchasePlaces',methods=['POST'])
return render_template(
"welcome.html", club=club, competitions=competitions
)
error_message = validate_competition_date(foundCompetition)
if error_message:
flash(error_message)
return render_template(
"welcome.html", club=foundClub, competitions=competitions
)
return render_template(
"booking.html", club=foundClub, competition=foundCompetition
)


def get_competition_from_name(name):
try:
competition = [
competition
for competition in competitions
if competition["name"] == name
][0]
return competition
except IndexError:
return None


def get_club_from_name(name):
try:
club = [club for club in clubs if club["name"] == name][0]
return club
except IndexError:
return None


def get_index_club(club):
return clubs.index(club)


def check_places(places, club):
if not places or int(places) < 1:
return "Places required must be a positive integer"
if int(places) > 12:
return (
"Places required must be a positive integer "
"that does not exceed 12"
)
if int(places) > int(club["points"]):
return "Places required exceed club's total points"


def take_places(places, club, competition):
try:
competition["numberOfPlaces"] = (
int(competition["numberOfPlaces"]) - places
)
club["points"] = int(club["points"]) - places
return True
except Exception:
return False


def update_clubs(club, index_club):
try:
with open(app.config["CLUB_FILE"], "w") as file_club:
dict_clubs = {}
clubs[index_club]["points"] = str(club["points"])
dict_clubs["clubs"] = clubs
json.dump(dict_clubs, file_club, indent=4)
return True
except FileNotFoundError:
return False


@app.route("/purchasePlaces", methods=["POST"])
def purchasePlaces():
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
flash('Great-booking complete!')
return render_template('welcome.html', club=club, competitions=competitions)
competition = get_competition_from_name(request.form["competition"])
club = get_club_from_name(request.form["club"])
error_message = check_places(request.form["places"], club)
if error_message:
flash(error_message)
return redirect(
url_for("book", competition=competition["name"], club=club["name"])
)
placesRequired = int(request.form["places"])
if take_places(placesRequired, club, competition) and update_clubs(
club, get_index_club(club)
):
flash("Great-booking complete!")
return render_template(
"welcome.html", club=club, competitions=competitions
)
else:
flash("Something went wrong-please try again")
return redirect(
url_for("book", competition=competition["name"], club=club["name"])
)


# TODO: Add route for points display
@app.route("/logout")
def logout():
if session:
session.pop("club_email", None)
return redirect(url_for("index"))


@app.route('/logout')
def logout():
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(debug=True)
12 changes: 11 additions & 1 deletion templates/booking.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
Expand All @@ -6,11 +7,20 @@
</head>
<body>
<h2>{{competition['name']}}</h2>
{% with messages = get_flashed_messages()%}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif%}
{% endwith %}
Places available: {{competition['numberOfPlaces']}}
<form action="/purchasePlaces" method="post">
<input type="hidden" name="club" value="{{club['name']}}">
<input type="hidden" name="competition" value="{{competition['name']}}">
<label for="places">How many places?</label><input type="number" name="places" id=""/>
<label for="places">How many places?</label><input type="number" name="places" id="places" max="12"/>
<button type="submit">Book</button>
</form>
</body>
Expand Down
42 changes: 42 additions & 0 deletions templates/display_points.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display points | GUDLFT Registration</title>
</head>
<body>

{% if session.club_email %}
<a href="{{ url_for('showSummary') }}">Back to summary</a>
<br> <a href="{{ url_for('logout') }}">Logout</a> <br>
{% else %}
<a href="{{ url_for('index') }}">Previous</a>
{% endif %}


<h1> Dashboard clubs points </h1>
<br>
<table>
<thead>
<tr>
<th>Club</th>
<th>Points</th>
</tr>
</thead>
{% for club in clubs %}
<tbody>
<tr>

<td>{{ club['name'] }}</td>
<td>{{ club['points'] }}</td>

</tr>
</tbody>
{% endfor %}

</table>


</body>
</html>
Loading