forked from omeryldzk/DatabaseProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
723 lines (585 loc) · 24.9 KB
/
views.py
File metadata and controls
723 lines (585 loc) · 24.9 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
from datetime import datetime
from flask import current_app, render_template, redirect, request, url_for, flash, abort
from passlib.hash import pbkdf2_sha256 as hasher
from forms import LoginForm, PlayerAttributesForm, PlayerForm, ClubForm, GameAddForm, GameEditForm
from psycopg2 import Error, errors
from models.user import get_user
from models.player import Player
from models.clubs import Club
from models.competitions import Competitions
from models.games import Games
from models.player_bio import PlayerBio
from models.player_attributes import PlayerAttributes
from models.player_photo import PlayerPhoto
from flask_login import current_user, logout_user, login_user, login_required
def home_page():
today = datetime.today()
day_name = today.strftime("%A")
return render_template("home.html", day=day_name)
################################ PLAYER ########################################
@login_required
def add_player_page():
if not current_user.is_admin:
abort(401) # “Unauthorized” error
form = PlayerForm()
db = current_app.config["db"]
if form.validate_on_submit():
name = form.data["name"]
first_name = form.data["first_name"]
last_name = form.data["last_name"]
current_club_name = form.data["current_club_name"]
current_club_id = form.data["current_club_id"]
competition_id = form.data["competition_id"]
player = Player(
id=0, # Assuming you set id to 0 for a new player
name=name,
first_name=first_name,
last_name=last_name,
current_club_name=current_club_name,
current_club_id=current_club_id,
competition_id=competition_id,
)
try:
db.add_player(player) # Adjust this based on your actual method to add a player
except Error as e:
if isinstance(e, errors.ForeignKeyViolation):
flash("There is no related team!", "danger")
return render_template("add_player.html", form=form)
# Handle other errors as needed
flash("Player is added.", "success")
return redirect(url_for("players_page"))
return render_template("add_player.html", form=form)
@login_required
def delete_player_page(id):
if not current_user.is_admin:
abort(401) # “Unauthorized” error
db = current_app.config["db"]
db.delete_player(id)
players = db.get_players()
flash("Player deleted", "success ")
return render_template("players.html", players=players)
@login_required
def edit_player_page(player_id):
if not current_user.is_admin:
abort(401) # “Unauthorized” error
db = current_app.config["db"]
player = db.get_player(player_id)
if player is None:
abort(404)
form = PlayerForm()
if form.validate_on_submit():
name = form.data["name"]
first_name = form.data["first_name"]
last_name = form.data["last_name"]
current_club_name = form.data["current_club_name"]
current_club_id = form.data["current_club_id"]
competition_id = form.data["competition_id"]
updated_player = Player(
id=player.id,
name=name,
first_name=first_name,
last_name=last_name,
current_club_name=current_club_name,
current_club_id=current_club_id,
competition_id=competition_id,
)
try:
db.update_player(player_id, updated_player)
except Error as e:
if isinstance(e, errors.UniqueViolation):
flash("Values must be unique!", "danger")
return render_template("player_edit.html", form=form)
flash("Player is updated.", "success")
return redirect(url_for("player_page", player_id=player_id))
form.name.data = player.name
form.first_name.data = player.first_name
form.last_name.data = player.last_name
return render_template("player_edit.html", form=form)
def players_page():
db = current_app.config["db"]
if request.method == "GET":
players = db.get_players()
return render_template("players.html", players=players)
else:
form_player_keys = request.form.getlist("player_keys")
for form_player_keys in form_player_keys:
db.delete_player(int(form_player_keys))
return redirect(url_for("players_page"))
def comp_player_page(competition_id):
db = current_app.config["db"]
if request.method == "GET":
players = []
players = db.get_players_of_competition(competition_id)
return render_template("players.html", players=players)
else:
form_player_keys = request.form.getlist("player_keys")
for form_player_keys in form_player_keys:
db.delete_player(int(form_player_keys))
return redirect(url_for("comp_player_page", competition_id=competition_id))
def club_player_page(club_id):
db = current_app.config["db"]
if request.method == "GET":
players = db.get_players_of_club(club_id)
return render_template("players.html", players=sorted(players, key=lambda x: x.name))
else:
form_player_keys = request.form.getlist("player_keys")
for form_player_keys in form_player_keys:
db.delete_player(int(form_player_keys))
return redirect(url_for("club_player_page", club_id=club_id))
def player_page(player_id):
db = current_app.config["db"]
player = db.get_player(player_id)
return render_template("player.html", player=player)
################################ PLAYER ATTRIBUTES ########################################
def players_attributes_page():
db = current_app.config["db"]
if request.method == "GET":
player_attributes = db.get_all_player_attributes()
return render_template("player_attributes.html", player_attributes=player_attributes)
else:
player_attributes_to_delete = request.form.get("player_attributes_to_delete")
db.delete_player_attributes(int(player_attributes_to_delete))
flash("Player attributes are deleted.", "success")
return redirect(url_for("players_attributes_page"))
def player_attributes_page(player_id):
db = current_app.config["db"]
player_attributes = db.get_player_attributes(player_id)
if player_attributes is None:
abort(404) # HTTP “Not Found” (404) error.
return render_template("individual_player_attributes.html", player_attributes=player_attributes)
@login_required
def edit_attributes_page(player_attributes_id):
if not current_user.is_admin:
abort(401) # “Unauthorized” error
db = current_app.config["db"]
attributes = db.get_player_attributes(player_attributes_id)
if attributes is None:
abort(404)
form = PlayerAttributesForm()
if form.validate_on_submit():
# Adjust the form field names based on your actual form structure
sub_position = form.data["sub_position"]
position = form.data["position"]
foot = form.data["foot"]
height_in_cm = form.data["height_in_cm"]
market_value_in_eur = form.data["market_value_in_eur"]
highest_market_value_in_eur = form.data["highest_market_value_in_eur"]
contract_expiration_date = form.data["contract_expiration_date"]
updated_attributes = PlayerAttributes(
id=attributes.id,
player_code=attributes.player_code,
sub_position=sub_position,
position=position,
foot=foot,
height_in_cm=height_in_cm,
market_value_in_eur=market_value_in_eur,
highest_market_value_in_eur=highest_market_value_in_eur,
contract_expiration_date=contract_expiration_date,
)
try:
db.update_player_attributes(player_attributes_id, updated_attributes)
except Error as e:
if isinstance(e, errors.UniqueViolation):
flash("Values must be unique!", "danger")
return render_template("player_attributes_edit.html", form=form, player=db.get_player(player_attributes_id))
flash("Attributes are updated.", "success")
return redirect(url_for("player_attributes_page", player_id=player_attributes_id))
if request.method == 'GET':
form.player_code.data = attributes.player_code
form.sub_position.data = attributes.sub_position
form.position.data = attributes.position
form.foot.data = attributes.foot
form.height_in_cm.data = attributes.height_in_cm
form.market_value_in_eur.data = attributes.market_value_in_eur
form.highest_market_value_in_eur.data = attributes.highest_market_value_in_eur
form.contract_expiration_date.data = attributes.contract_expiration_date
player = db.get_player(player_attributes_id)
return render_template("player_attributes_edit.html", form=form, player=player)
@login_required
def delete_attributes_page(id):
if not current_user.is_admin:
abort(401) # “Unauthorized” error
db = current_app.config["db"]
attributes = db.get_player_attributes(id)
if attributes is None:
abort(404)
try:
db.delete_player_attributes(id)
except Error as e:
flash("Error deleting attributes.", "danger")
return redirect(url_for("player_attributes_page", player_id=id))
flash("Attributes deleted.", "success")
return redirect(url_for("players_attributes_page"))
@login_required
def add_attributes_page():
if not current_user.is_admin:
abort(401) # “Unauthorized” error
form = PlayerAttributesForm()
db = current_app.config["db"]
if form.validate_on_submit():
# Adjust the form field names based on your actual form structure
player_code = form.data["player_code"]
sub_position = form.data["sub_position"]
position = form.data["position"]
foot = form.data["foot"]
height_in_cm = form.data["height_in_cm"]
market_value_in_eur = form.data["market_value_in_eur"]
highest_market_value_in_eur = form.data["highest_market_value_in_eur"]
contract_expiration_date = form.data["contract_expiration_date"]
attributes = PlayerAttributes(
id=0, # Assuming you set id to 0 for a new set of attributes
player_code=player_code,
sub_position=sub_position,
position=position,
foot=foot,
height_in_cm=height_in_cm,
market_value_in_eur=market_value_in_eur,
highest_market_value_in_eur=highest_market_value_in_eur,
contract_expiration_date=contract_expiration_date,
)
try:
db.add_player_attributes(attributes)
except Error as e:
flash("Error adding attributes.", "danger")
return render_template("player_attributes_add.html", form=form)
flash("Attributes added.", "success")
return redirect(url_for("players_attributes_page"))
return render_template("player_attributes_add.html", form=form)
#def players_photos_page():
# db = current_app.config["db"]
# player_photos = db.get_all_player_photos()
# return render_template("player_photos.html", player_photos=player_photos)
def player_photo_page(player_id):
db = current_app.config["db"]
player_photo = db.get_player_photos(player_id)
return render_template("player_photos.html", player_photos=player_photo)
#def players_bios_page():
# db = current_app.config["db"]
# player_bios = db.get_all_player_bios()
# return render_template("player_bios.html", player_bios=player_bios)
#def player_bio_page():
# db = current_app.config["db"]
# player_bio = db.get_player_bios()
# return render_template("player_bio.html", player_bios=player_bio)
################################ CLUBS ########################################
def clubs_page():
db = current_app.config["db"]
if request.method == "GET":
clubs = db.get_clubs()
return render_template("clubs.html", clubs=clubs)
else:
search = request.form.get("search")
if search:
clubs = db.get_clubs_by_search(search)
if clubs == None:
flash("NO RESULTS FOUND", "warning")
clubs = db.get_clubs()
return render_template("clubs.html", clubs=clubs)
else:
flash("RESULTS FOUND:", "success")
return render_template("clubs.html", clubs=clubs)
if not current_user.is_admin:
abort(401)
form_club_id_list = request.form.getlist("club_ids")
for form_club_id in form_club_id_list:
db.delete_club(int(form_club_id))
flash("Club has been deleted", "success")
return redirect(url_for("clubs_page"))
def comp_clubs_page(competition_id):
db = current_app.config["db"]
if request.method == "GET":
clubs = db.get_clubs_of_competition(competition_id)
return render_template("comp_clubs.html", clubs=clubs)
else:
search = request.form.get("search")
if search:
clubs = db.get_clubs_by_search(search)
if clubs == None:
flash("NO RESULTS FOUND", "warning")
clubs = db.get_clubs_of_competition(competition_id)
return render_template("comp_clubs.html", clubs=clubs)
else:
flash("RESULTS FOUND:", "success")
return render_template("comp_clubs.html", clubs=clubs)
if not current_user.is_admin:
abort(401)
form_club_id_list = request.form.getlist("club_ids")
for form_club_id in form_club_id_list:
db.delete_club(int(form_club_id))
flash("Club has been deleted", "success")
return redirect(url_for("comp_clubs_page"))
def club_page(club_id):
db = current_app.config["db"]
club = db.get_club(club_id)
if club is None:
abort(404) # HTTP “Not Found” (404) error.
return render_template("club_specific.html", club=club)
@login_required
def club_add_page():
# Check if the current user is an admin
if not current_user.is_admin:
abort(401) # Unauthorized error
# Create an instance of the ClubEditForm
form = ClubForm()
# Access the database from the Flask app configuration
db = current_app.config["db"]
# Validate the form on submission
if form.validate_on_submit():
# Extract form data
club_code = form.data["club_code"]
name = form.data["name"]
domestic_competition_id = form.data["domestic_competition_id"]
total_market_value = form.data["total_market_value"]
squad_size = form.data["squad_size"]
average_age = form.data["average_age"]
foreigners_number = form.data["foreigners_number"]
foreigners_percentage = form.data["foreigners_percentage"]
national_team_players = form.data["national_team_players"]
stadium_name = form.data["stadium_name"]
stadium_seats = form.data["stadium_seats"]
net_transfer_record = form.data["net_transfer_record"]
coach_name = form.data["coach_name"]
last_season = form.data["last_season"]
url = form.data["url"]
# Create a Club instance
club = Club(
club_id=0,
club_code=club_code,
name=name,
domestic_competition_id=domestic_competition_id,
total_market_value=total_market_value,
squad_size=squad_size,
average_age=average_age,
foreigners_number=foreigners_number,
foreigners_percentage=foreigners_percentage,
national_team_players=national_team_players,
stadium_name=stadium_name,
stadium_seats=stadium_seats,
net_transfer_record=net_transfer_record,
coach_name=coach_name,
last_season=last_season,
url=url
)
try:
# Add the Club to the database
db.add_club(club)
except Error as e:
if isinstance(e, errors.ForeignKeyViolation):
flash("There is an issue with foreign key constraints!", "danger")
return render_template("club_add.html", form=form)
flash("Club added successfully!", "success")
return redirect(url_for("clubs_page"))
return render_template("club_add.html", form=form)
@login_required
def club_edit_page(club_id):
# Check if the current user is an admin
if not current_user.is_admin:
abort(401) # Unauthorized error
# Access the database from the Flask app configuration
db = current_app.config["db"]
# Retrieve the club from the database
club = db.get_club(club_id)
# Check if the club exists
if not club:
flash("Club not found!", "danger")
return redirect(url_for("clubs_page"))
# Create an instance of the ClubEditForm and populate it with the current club data
form = ClubForm(obj=club)
# Validate the form on submission
if form.validate_on_submit():
# Update club data with form data
form.populate_obj(club)
try:
# Update the Club in the database
db.update_club(club_id, club)
except Error as e:
if isinstance(e, errors.ForeignKeyViolation):
flash("There is an issue with foreign key constraints!", "danger")
return render_template("club_edit.html", form=form, club_id=club_id)
flash("Club updated successfully!", "success")
return redirect(url_for("clubs_page"))
return render_template("club_edit.html", form=form, club_id=club_id)
@login_required
def club_delete_page(club_id):
# Check if the current user is an admin
if not current_user.is_admin:
abort(401) # Unauthorized error
# Access the database from the Flask app configuration
db = current_app.config["db"]
# Retrieve the club from the database
club = db.get_club(club_id)
# Check if the club exists
if not club:
flash("Club not found!", "danger")
return redirect(url_for("clubs_page"))
# Delete the Club from the database
db.delete_club(club_id)
flash("Club deleted successfully!", "success")
return redirect(url_for("clubs_page"))
def games_page():
db = current_app.config["db"]
game = db.get_games()
if game is None:
abort(404)
return render_template("games.html", game=game)
def club_games_page(club_id):
db = current_app.config["db"]
game = []
game = db.get_games_of_club(club_id)
if game is None:
abort(404)
return render_template("club_games.html", game=game)
def comp_games_page(competition_id):
db = current_app.config["db"]
game = db.get_games_of_competition(competition_id)
if game is None:
abort(404)
return render_template("comp_games.html", game=game)
def game_page(game_id):
db = current_app.config["db"]
game = db.get_game(game_id)
if game is None:
abort(404)
return render_template("game.html", game=game)
@login_required
def game_add_page():
db = current_app.config["db"]
if not current_user.is_admin:
abort(401) # “Unauthorized” error
form = GameAddForm()
if form.validate_on_submit():
competition_id = form.data["competition_id"]
season = form.data["season"]
date = form.data["date"]
home_club_id = form.data["home_club_id"]
away_club_id = form.data["away_club_id"]
home_club_goals = form.data["home_club_goals"]
away_club_goals = form.data["away_club_goals"]
home_club_position = form.data["home_club_position"]
away_club_position = form.data["away_club_position"]
home_club_manager_name = form.data["home_club_manager_name"]
away_club_manager_name = form.data["away_club_manager_name"]
stadium = form.data["stadium"]
attendance = form.data["attendance"]
referee = form.data["referee"]
url = form.data["url"]
home_club_name = form.data["home_club_name"]
away_club_name = form.data["away_club_name"]
game = Games(
game_id=None,
competition_id=competition_id,
season=season,
date=date,
home_club_id=home_club_id,
away_club_id=away_club_id,
home_club_goals=home_club_goals,
away_club_goals=away_club_goals,
home_club_position=home_club_position,
away_club_position=away_club_position,
home_club_manager_name=home_club_manager_name,
away_club_manager_name=away_club_manager_name,
stadium=stadium,
attendance=attendance,
referee=referee,
url=url,
home_club_name=home_club_name,
away_club_name=away_club_name,
)
try:
db.add_game(game)
except Error as e:
if isinstance(e, errors.UniqueViolation):
flash("Games must have unique IDs!", "danger")
if isinstance(e, errors.ForeignKeyViolation):
flash("There is no related team(s)", "danger")
return render_template("game_add.html", form=form, type="Add")
flash("Game is added.", "success")
return redirect(url_for("game_page", game_id=game.game_id))
return render_template("game_add.html", form=form, type="Add")
@login_required
def game_edit_page(game_id):
db = current_app.config["db"]
if not current_user.is_admin:
abort(401) # “Unauthorized” error
game = db.get_game(game_id)
if game is None:
flash("Game not found", "danger")
return redirect(url_for("game_page"))
form = GameEditForm()
if form.validate_on_submit():
# Update the game fields
game.competition_id = form.data["competition_id"]
game.season = form.data["season"]
game.date = form.data["date"]
game.home_club_id = form.data["home_club_id"]
game.away_club_id = form.data["away_club_id"]
game.home_club_goals = form.data["home_club_goals"]
game.away_club_goals = form.data["away_club_goals"]
game.home_club_position = form.data["home_club_position"]
game.away_club_position = form.data["away_club_position"]
game.home_club_manager_name = form.data["home_club_manager_name"]
game.away_club_manager_name = form.data["away_club_manager_name"]
game.stadium = form.data["stadium"]
game.attendance = form.data["attendance"]
game.referee = form.data["referee"]
game.url = form.data["url"]
game.home_club_name = form.data["home_club_name"]
game.away_club_name = form.data["away_club_name"]
try:
db.update_game(game, game_id)
except Error as e:
if isinstance(e, errors.UniqueViolation):
flash("Games must have unique IDs!", "danger")
if isinstance(e, errors.ForeignKeyViolation):
flash("There is no related team(s)", "danger")
return render_template("game_add.html", form=form, type="Update")
flash("Game is updated.", "success")
return redirect(url_for("game_page", game_id=game.game_id))
# Populate the form with existing game data
form.process(obj=game)
return render_template("game_edit.html", form=form, type="Update")
@login_required
def game_delete_page(game_id):
db = current_app.config["db"]
if not current_user.is_admin:
abort(401) # “Unauthorized” error
game = db.get_game(game_id)
if game is None:
flash("Game not found", "danger")
return redirect(url_for("game_page"))
try:
db.delete_game(game_id)
except Error as e:
flash("Error deleting game", "danger")
return redirect(url_for("game_page"))
flash("Game is deleted.", "success")
return redirect(url_for("game_page"))
def goals_page(game_id):
db = current_app.config["db"]
goals = db.get_goals_of_game(game_id)
if goals is None:
abort(404)
return render_template("goals_of_game.html", goals=goals)
def login_page():
form = LoginForm()
if form.validate_on_submit():
username = form.data["username"]
user = get_user(username)
if user is not None:
password = form.data["password"]
if hasher.verify(password, user.password):
login_user(user)
# flash function registers a message that the user will see on the next page
flash("You have logged in.", "success")
# if an anonymous user visits the /movies/add page, they will be redirected
# to the login page (because of the login_view setting,
# and after successfully logging in, this part will redirect the user back to
# the movie addition page.
next_page = request.args.get("next", url_for("home_page"))
return redirect(next_page)
flash("Invalid credentials!", "danger")
return render_template("login.html", form=form)
def logout_page():
logout_user()
flash("You have logged out.")
return redirect(url_for("home_page"))