-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
261 lines (161 loc) · 7.36 KB
/
app.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from flask import Flask, render_template, url_for, redirect, request, session, flash
from functools import wraps
from database import Database, Player, Team, Match, LastMatch
methods = ['GET', 'POST'] #defined for ease
#session is stored in server as data
#session id is stored in client side as cookies
app = Flask(__name__)
app.secret_key = '1499058058097067107'
#----------------------------------------------------DATABASE INIT-------------------------------------------------------------------------
db = Database()
# -----------------------------------------------------ROUTES------------------------------------------------------------
#------------------DECORATOR WRAPPER FOR MANAGING LOGIN SESSIONS
def login_required(f):
@wraps(f)
def wrap(*args, **kws):
if "logged_in" in session:
return f(*args, **kws)
else:
flash("You need to login first!")
return redirect(url_for('login'))
return wrap
#-----------------NAVIGATIONAL ROUTES---------------------
@app.route("/")
@app.route("/home")
def home():
topTeams = db.topTeams() #home
matches = db.homeMatches() #home
return render_template("home.html",title="Home", teams=topTeams, matches=matches)
@app.route("/schedule")
def schedule():
allMatches = db.fullMatches() #./schedule
return render_template("schedule.html",title="Schedule", schedule=allMatches)
@app.route("/points")
def points():
allPoints = db.points() #./points
allTeams = db.allTeams() #./points
return render_template("points.html",title="Points", points=allPoints, teams=allTeams)
@app.route("/rules")
def rules():
return render_template("rules.html",title="Rules")
@app.route("/about")
def about():
return render_template("about.html",title="About")
@app.route("/lastMatch")
def lastMatch():
matchRaw = db.getLastMatch()
if matchRaw == (()):
return render_template("lastMatch.html", title='Last Match', status=False)
else:
match = {
'team1': matchRaw[0][0],
'team2': matchRaw[0][1],
'mom': matchRaw[0][2],
'draw': matchRaw[0][3]
}
return render_template("lastMatch.html", title="Last Match", match=match, status=True)
@app.route("/teams/<teamName>")
def indieTeam(teamName):
team = db.indieTeam(teamName)
return render_template("indieTeam.html",title=team[0]['name'], teamStat=team[0], players=team[1])
#-------------------ADMIN and FORMS ROUTES---------------------------------
@app.route("/dashboard", methods=['GET', 'POST'])
@login_required
def dashboard():
ad_matches = db.ad_matches() #./dashboard
#---------------OPERATIONS--------------------------------
if request.method == 'POST':
if request.form.get('operation') == 'schedule_match':
team1 = request.form.get('team1_id')
team2 = request.form.get('team2_id')
date = request.form.get('date')
match = Match(team1_id=team1, team2_id=team2, date=date)
db.addMatch(match)
return redirect(url_for('dashboard'))
elif request.form.get('operation') == 'delete_match':
match_id = request.form.get('match_id')
match = Match(match_id=match_id)
db.deleteMatch(match)
return redirect(url_for('dashboard'))
elif request.form.get('operation') == 'add_team':
name = request.form.get('team_name')
captain = request.form.get('captain_name')
team = Team(name=name, captain=captain)
db.addTeam(team)
return redirect(url_for('dashboard'))
elif request.form.get('operation') == 'delete_team':
id = request.form.get('team_id')
team = Team(id=id)
db.deleteTeam(team)
return redirect(url_for('dashboard'))
elif request.form.get('operation') == 'add_player':
name = request.form.get('player_name')
team_id = request.form.get('team_id')
player = Player(name=name, team_id=team_id)
db.addPlayer(player)
return redirect(url_for('dashboard'))
elif request.form.get('operation') == 'delete_player':
id = request.form.get('player_id')
team_id = request.form.get('team_id')
player = Player(id=id, team_id=team_id)
db.deletePlayer(player)
return redirect(url_for('dashboard'))
return render_template("./admin/dashboard.html",title="Dash Board", matches=ad_matches)
@app.route("/dashboard/summary", methods=methods)
@login_required
def summary():
if request.method == 'POST':
match_id = request.form.get('match_id')
team1_id = request.form.get('win_id')
team2_id = request.form.get('lose_id')
mom = request.form.get('man_of_match')
draw = False
if request.form.get('draw'):
draw = True
match = LastMatch(match_id, team1_id, team2_id, mom, draw)
db.storeLastMatch(match)
return redirect(url_for('dashboard'))
return render_template("./admin/summary.html", title="Summary")
@app.route("/dashboard/playerStat", methods=methods)
@login_required
def playerStat():
if request.method == 'POST':
id = request.form.get('player_id')
team_id = request.form.get('team_id')
sc_runs = request.form.get('runs_scored')
pl_balls = request.form.get('balls_faced')
gv_runs = request.form.get('runs_given')
th_balls = request.form.get('balls_thrown')
wickets = request.form.get('wickets')
player = Player(id=id, team_id=team_id, sc_runs=sc_runs, pl_balls=pl_balls, gv_runs=gv_runs, th_balls=th_balls, wickets=wickets)
db.updateStats(player)
return redirect(url_for('dashboard'))
return render_template("./admin/playerStat.html", title="Stats")
@app.route("/dashboard/allTeams", methods=methods)
@login_required
def allTeamsAdm():
ad_teams = db.ad_teams() #./dashboard/allTeams
return render_template("./admin/teams.html", title="Stats", teams=ad_teams)
@app.route("/dashboard/allPlayers", methods=methods)
@login_required
def allPlayers():
ad_players = db.ad_players() #./dashboard/allPlayers
return render_template("./admin/allPlayers.html", title="Stats", players=ad_players)
#--------------------LOGIN-LOGOUT ROUTES--------------------------
@app.route("/login", methods=methods)
def login():
error = None
if request.method == 'POST':
if request.form['login_username'] !='admin' or request.form['login_password'] !='password':
error = "! Invalid credentials, please try again"
else:
session['logged_in'] = True
return redirect(url_for('dashboard'))
return render_template("./admin/login.html",title="Login admin", error=error)
@app.route("/logout")
def logout():
session.pop('logged_in', None)
return redirect(url_for('home'))
# ----------------------------------------------------------------ROUTES END-----------------------------------------------------------
if __name__ == "__main__":
app.run(debug=True)