-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshows.py
386 lines (329 loc) · 11 KB
/
shows.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#Standard library import
import mysql.connector
import json
#Connect with the precise info, also with dataset name
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="7gimtwDS.1.1",
database="volleyballdatabase"
)
def return_team_id(username):
mycursor = mydb.cursor()
query = f"""SELECT team_ID
FROM team
WHERE coach_username = '{username}'
AND NOW() BETWEEN contract_start AND contract_finish"""
mycursor.execute(query)
shows=[]
for i in mycursor:
shows.append({"team_id": i[0]})
mycursor.close()
return shows
def delete_session(session_id):
mycursor = mydb.cursor()
query = f"""DELETE FROM match_session
WHERE session_ID = '{session_id}'"""
mycursor.execute(query)
mycursor.close()
def returns_sessions(coach_name):
mycursor = mydb.cursor()
query = f"""SELECT ms.session_ID
FROM team t
JOIN match_session ms ON t.team_ID = ms.team_id
WHERE t.coach_username = '{coach_name}'"""
shows=[]
mycursor.execute(query)
for i in mycursor:
shows.append({"session_ID": i[0]})
mycursor.close()
return shows
def add_session(username, session_ID, team_id, stadium_ID, time_slot, date, assigned_jury_username, played_player_username_list):
mycursor = mydb.cursor()
shows=return_team_id(username)[0]["team_id"]
rating = None
if(len(session_ID)==0):
session_ID="NULL"
else:
session_ID="'" + session_ID + "'"
if(len(team_id)==0):
team_id2="NULL"
else:
team_id2="'" + team_id + "'"
if(len(stadium_ID)==0):
stadium_ID="NULL"
else:
stadium_ID="'" + stadium_ID + "'"
if(len(time_slot)==0):
time_slot="NULL"
else:
time_slot="'" + time_slot + "'"
if(len(date)==0):
date="NULL"
else:
date="'" + date + "'"
if(len(assigned_jury_username)==0):
assigned_jury_username="NULL"
else:
assigned_jury_username="'" + assigned_jury_username + "'"
if(len(played_player_username_list)==0):
played_player_username_list="NULL"
else:
played_player_username_list="'" + played_player_username_list + "'"
if(shows==int(team_id)):
query = f"INSERT INTO match_session (session_ID, team_id, stadium_ID, time_slot, date, assigned_jury_username, rating, played_player_username_list) VALUES ({session_ID}, {team_id2}, {stadium_ID}, {time_slot}, {date}, {assigned_jury_username}, NULL, {played_player_username_list})"
mycursor.execute(query)
mycursor.close()
return (True, shows)
else:
mycursor.close()
return (False,shows)
def return_std_name_and_counties():
mycursor = mydb.cursor()
query ="SELECT S.stadium_name, S.stadium_country FROM stadium S;"
shows=[]
mycursor.execute(query)
for i in mycursor:
shows.append({"name": i[0],"surname": i[1]})
mycursor.close()
return shows
def return_player_home1(username):
mycursor = mydb.cursor()
query = f"""
SELECT DISTINCT
u.name,
u.surname
FROM
played_in_m pm
JOIN
user u ON pm.username = u.username
WHERE
pm.session_ID IN (
SELECT
session_ID
FROM
match_session
WHERE
JSON_CONTAINS(played_player_username_list->'$[*]', '"{username}"') > 0
)
"""
shows=[]
mycursor.execute(query)
for i in mycursor:
shows.append({"name": i[0],"surname": i[1]})
mycursor.close()
return shows
def return_player_home2(username):
mycursor = mydb.cursor()
query = f"""
SELECT
AVG(p.height) AS average_height
FROM
player p
JOIN
user u ON p.username = u.username
WHERE
p.username IN (
SELECT
p.username
FROM
played_in_m pm
JOIN
player p ON pm.username = p.username
JOIN
user u ON p.username = u.username
WHERE
pm.session_ID IN (
SELECT
session_ID
FROM
match_session
WHERE
JSON_CONTAINS(played_player_username_list->'$[*]', '"{username}"') > 0
)
AND
p.username != '{username}' -- Exclude username
GROUP BY
p.username
HAVING
COUNT(p.username) = (
SELECT
MAX(player_count)
FROM (
SELECT
COUNT(p.username) AS player_count
FROM
played_in_m pm
JOIN
player p ON pm.username = p.username
JOIN
user u ON p.username = u.username
WHERE
pm.session_ID IN (
SELECT
session_ID
FROM
match_session
WHERE
JSON_CONTAINS(played_player_username_list->'$[*]', '"{username}"') > 0
)
GROUP BY
p.username
) AS max_player_count
)
)
"""
shows=[]
mycursor.execute(query)
for i in mycursor:
shows.append({"height": i[0]})
mycursor.close()
return shows
def return_unrated_sessions(jury_username):
mycursor = mydb.cursor()
query = f"""
SELECT
m.session_ID
FROM
match_session m
LEFT JOIN
rate r ON m.session_ID = r.session_ID
LEFT JOIN
jury j ON m.assigned_jury_username = j.username
WHERE
m.assigned_jury_username = '{jury_username}'
AND m.rating IS NULL
AND m.date < CURDATE()
"""
shows=[]
mycursor.execute(query)
for i in mycursor:
shows.append({"name": i[0]})
mycursor.close()
return shows
def returnRatings(jury_username):
mycursor = mydb.cursor()
query = """
SELECT
j.username,
AVG(m.rating) AS avrge_rating,
COUNT(*) AS total_rated_sessions
FROM
jury j
JOIN
rate r ON j.username = r.username
JOIN
match_session m ON r.session_ID = m.session_ID AND j.username = m.assigned_jury_username
WHERE
j.username = %s
GROUP BY
j.username
"""
shows=[]
mycursor.execute(query, (jury_username,))
for i in mycursor:
shows.append({"username": i[0], "avg_rating": i[1], "count": i[2]})
mycursor.close()
return shows
def rate_sessions(username ,Match_Session_ID ,rate):
mycursor = mydb.cursor()
query = f"UPDATE match_session SET rating = '{rate}' WHERE session_ID = '{Match_Session_ID}' AND assigned_jury_username = '{username}'"
mycursor.execute(query)
query = f"INSERT INTO rate (session_ID, username) VALUES ('{Match_Session_ID}', '{username}')"
mycursor.execute(query)
mycursor.close()
def create_player(username, date_of_birth, height, weight, password):
if not isManName(username):
mycursor = mydb.cursor()
query =f"INSERT INTO user (username, password) VALUES('{username}', '{password}')"
mycursor.execute(query)
query =f"INSERT INTO player (username, date_of_birth, height, weight, password) VALUES('{username}', '{date_of_birth}', '{height}', '{weight}', '{password}')"
mycursor.execute(query)
mycursor.close()
return False
else:
return True
def create_coach(username, nationality, password):
if not isManName(username):
mycursor = mydb.cursor()
query =f"INSERT INTO user (username, password) VALUES('{username}', '{password}')"
mycursor.execute(query)
query =f"INSERT INTO coach (username, nationality, password) VALUES('{username}', '{nationality}', '{password}')"
mycursor.execute(query)
mycursor.close()
return False
else:
return True
def create_jury(username, nationality, password):
if not isManName(username):
mycursor = mydb.cursor()
query =f"INSERT INTO user (username, password) VALUES('{username}', '{password}')"
mycursor.execute(query)
query =f"INSERT INTO jury (username, nationality, password) VALUES('{username}', '{nationality}', '{password}')"
mycursor.execute(query)
mycursor.close()
return False
else:
return True
def returnStadimName():
shows=[]
mycursor = mydb.cursor()
mycursor.execute("SELECT S.stadium_name FROM stadium S")
for x in mycursor:
shows.append({"name": x[0]})
mycursor.close()
return shows
def changeStadium(old_name, new_name):
mycursor = mydb.cursor()
query = f"UPDATE stadium SET stadium_name = '{new_name}' WHERE stadium_name = '{old_name}'"
mycursor.execute(query)
mycursor.close()
def coach_return_available_session(coach_name):
mycursor = mydb.cursor()
query = f"""SELECT m.session_ID
FROM match_session m
WHERE m.played_player_username_list IS NULL
AND m.team_ID IN (
SELECT t.team_ID
FROM team t
WHERE t.coach_username = '{coach_name}'
AND NOW() BETWEEN t.contract_start AND t.contract_finish
)"""
mycursor.execute(query)
shows=[]
for x in mycursor:
shows.append({"name": x[0]})
mycursor.close()
return shows
def coach_return_player_names(coach_name):
mycursor = mydb.cursor()
query = f"""SELECT register.username
FROM register
JOIN player ON register.username = player.username
WHERE register.team_ID = (
SELECT team_ID
FROM team
WHERE coach_username = '{coach_name}'
AND NOW() BETWEEN contract_start AND contract_finish
) """
mycursor.execute(query)
shows=[]
for x in mycursor:
shows.append({"username": x[0]})
mycursor.close()
return shows
def coach_add_new_squad_quer( playerlist,session):
mycursor = mydb.cursor()
playerlist_json = json.dumps(playerlist)
query = f"""UPDATE match_session SET played_player_username_list = '{playerlist_json}' WHERE session_ID = '{session}'"""
mycursor.execute(query)
mycursor.close()
def isManName(username):
mycursor = mydb.cursor()
mycursor.execute("SELECT M.username, M.password FROM manager M")
for x in mycursor:
if username==x[0]:
mycursor.close()
return True
mycursor.close()
return False