Skip to content
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ bin
include
lib
.Python
tests/
.envrc
__pycache__
__pycache__
.idea/
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": "13"
},
{
"name": "She Lifts",
"email": "kate@shelifts.co.uk",
"points": "12"
},
{
"name": "Iron Temple",
"email": "admin@irontemple.com",
"points": "4"
}
]
}
12 changes: 6 additions & 6 deletions competitions.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"competitions": [
{
"name": "Spring Festival",
"date": "2020-03-27 10:00:00",
"numberOfPlaces": "25"
},
{
"name": "Fall Classic",
"date": "2020-10-22 13:30:00",
"numberOfPlaces": "13"
"number_of_places": "13"
},
{
"name": "Spring Festival",
"date": "2020-03-27 10:00:00",
"number_of_places": "25"
}
]
}
115 changes: 91 additions & 24 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,121 @@
import json
from flask import Flask,render_template,request,redirect,flash,url_for
from flask import Flask, render_template, request, redirect, flash, url_for


def loadClubs():
def load_clubs():
with open('clubs.json') as c:
listOfClubs = json.load(c)['clubs']
return listOfClubs
list_of_clubs = json.load(c)['clubs']
return list_of_clubs


def loadCompetitions():
def load_competitions():
with open('competitions.json') as comps:
listOfCompetitions = json.load(comps)['competitions']
return listOfCompetitions

list_of_competitions = json.load(comps)['competitions']
return list_of_competitions

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

competitions = loadCompetitions()
clubs = loadClubs()
competitions = load_competitions()
clubs = load_clubs()

def update_club_booked_places(club, places, competition_name):
clubs.remove(club)

club["points"] = str(int(club["points"]) - places)

clubs.append(club)
save_clubs()

def save_clubs():
with open('clubs.json', 'w') as c:
list_of_clubs = {"clubs": clubs}
json.dump(list_of_clubs, c, indent=4)

def update_competition_available_places(competition, places):
competitions.remove(competition)

competition['number_of_places'] = str(int(competition['number_of_places']) - places)

competitions.append(competition)
save_competitions()

def save_competitions():
with open('competitions.json', 'w') as comps:
list_of_competitions = {"competitions": competitions}
json.dump(list_of_competitions, comps, indent=4)

@app.route('/')
def index():
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)
@app.route('/showSummary', methods=['POST'])
def show_summary():

club = next((club for club in clubs if club['email'] == request.form['email']), None)

if club is None:
flash("Sorry, that email was not found.")
return render_template(template_name_or_list="index.html", error="Email not found"), 404

return render_template(template_name_or_list='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)
def book(competition, club):
found_club = [c for c in clubs if c['name'] == club][0]
print(f"COMPETITIONS : {competitions}")
found_competition = [c for c in competitions if c['name'] == competition][0]
if found_club and found_competition:
return render_template(template_name_or_list='booking.html',
club=found_club,
competition=found_competition)
else:
flash("Something went wrong-please try again")
return render_template('welcome.html', club=club, competitions=competitions)
return render_template(template_name_or_list='welcome.html',
club=club,
competitions=competitions)


@app.route('/purchasePlaces',methods=['POST'])
def purchasePlaces():
def purchase_places():
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
places_required = int(request.form['places'])

if places_required < 0:
flash("Sorry, you should type a positive number.")
return render_template(template_name_or_list='welcome.html',
club=club,
competitions=competitions,
error="Negative number"), 403

elif places_required > 12:
flash("Sorry, you are not allow to purchase more than 12 places for this competition.")
return render_template(template_name_or_list='welcome.html',
club=club,
competitions=competitions,
error="Places max reached"), 403

elif places_required > int(club['points']):
flash("Sorry, you do not have enough points to purchase.")
return render_template(template_name_or_list='welcome.html',
club=club,
competitions=competitions,
error="Points not enough"), 403

update_club_booked_places(club=club,
places=places_required,
competition_name=competition["name"])

update_competition_available_places(competition=competition, places=places_required)

flash('Great-booking complete!')
return render_template('welcome.html', club=club, competitions=competitions)
return render_template(template_name_or_list='welcome.html',
club=club,
competitions=competitions)


# TODO: Add route for points display
Expand Down
4 changes: 2 additions & 2 deletions templates/booking.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</head>
<body>
<h2>{{competition['name']}}</h2>
Places available: {{competition['numberOfPlaces']}}
<form action="/purchasePlaces" method="post">
Places available: {{competition['number_of_places']}}
<form action="/purchase_places" 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=""/>
Expand Down
13 changes: 12 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
</head>
<body>
<h1>Welcome to the GUDLFT Registration Portal!</h1>

{% with messages = get_flashed_messages()%}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif%}
{%endwith%}

Please enter your secretary email to continue:
<form action="showSummary" method="post">
<form action="show_summary" method="post">
<label for="email">Email:</label>
<input type="email" name="email" id=""/>
<button type="submit">Enter</button>
Expand Down
4 changes: 2 additions & 2 deletions templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ <h3>Competitions:</h3>
<li>
{{comp['name']}}<br />
Date: {{comp['date']}}</br>
Number of Places: {{comp['numberOfPlaces']}}
{%if comp['numberOfPlaces']|int >0%}
Number of Places: {{comp['number_of_places']}}
{%if comp['number_of_places']|int >0%}
<a href="{{ url_for('book',competition=comp['name'],club=club['name']) }}">Book Places</a>
{%endif%}
</li>
Expand Down
Empty file added unit_tests/__init__.py
Empty file.
109 changes: 109 additions & 0 deletions unit_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import pytest

from random import randint

from server import app

@pytest.fixture
def client():
my_app = app
with my_app.test_client() as client:
yield client

@pytest.fixture
def get_clubs():
the_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"
}
]
return the_clubs

@pytest.fixture
def get_competitions():
the_competitions = [
{
"name": "Fall Classic",
"date": "2020-10-22 13:30:00",
"number_of_places": "13"
},
{
"name": "Spring Festival",
"date": "2020-03-27 10:00:00",
"number_of_places": "25"
}
]
return the_competitions

@pytest.fixture
def get_existing_mail():
data = {"email": "kate@shelifts.co.uk"}
return data

@pytest.fixture
def get_existing_mail_2():
data = {"email": "admin@irontemple.com"}
return data

@pytest.fixture
def get_unexisting_mail():
data = {"email": "nicolas.marie@unexisting.com"}
return data

@pytest.fixture
def get_existing_competition_and_club():
data = {"competition": "Spring Festival", "club": "She Lifts"}
return data

@pytest.fixture
def get_existing_competition_and_club_2():
data = {"competition": "Spring Festival", "club": "Iron Temple"}
return data

@pytest.fixture
def get_consistent_purchasing_data():
competition = "Spring Festival"
club_name = "She Lifts"
places_to_book = randint(1, 12)
data = {"competition": competition, "club": club_name, "places": str(places_to_book)}

return data

@pytest.fixture
def get_inconsistent_purchasing_data():
competition = "Spring Festival"
club_name = "Iron Temple"
club_points = 4
places_to_book = randint(club_points+1, 12)
data = {"competition": competition, "club": club_name, "places": str(places_to_book)}

return data

@pytest.fixture
def purchasing_data_over_12_places():
competition = "Spring Festival"
club_name = "She Lifts"
places_to_book = 13
data = {"competition": competition, "club": club_name, "places": str(places_to_book)}

return data

@pytest.fixture
def purchasing_data_with_negative_places():
competition = "Spring Festival"
club_name = "She Lifts"
places_to_book = -2
data = {"competition": competition, "club": club_name, "places": str(places_to_book)}

return data
Loading