This repository was archived by the owner on Jun 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
104 lines (85 loc) · 4.88 KB
/
Copy pathmodels.py
File metadata and controls
104 lines (85 loc) · 4.88 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
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), nullable=False, unique=True)
email = db.Column(db.String(150), nullable=False, unique=True)
password_hash = db.Column(db.String(256), nullable=False)
last_login = db.Column(db.DateTime, default=db.func.current_timestamp())
is_admin = db.Column(db.Boolean, default=False) # New field for admin status
tournaments = db.relationship('Tournament', backref='organizer', lazy=True)
def set_password(self, password):
# local import to secure the password
from werkzeug.security import generate_password_hash
self.password_hash = generate_password_hash(password)
def check_password(self, password):
from werkzeug.security import check_password_hash
return check_password_hash(self.password_hash, password)
class Tournament(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False)
date = db.Column(db.Date, nullable=False)
location = db.Column(db.String(200))
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
matches = db.relationship('Match', backref='tournament', lazy=True, cascade="all, delete-orphan")
class Player(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150), nullable=False)
# Players can be part of multiple teams
teams_as_player1 = db.relationship('Team', foreign_keys='Team.player1_id', backref='player1', lazy=True)
teams_as_player2 = db.relationship('Team', foreign_keys='Team.player2_id', backref='player2', lazy=True)
class Team(db.Model):
id = db.Column(db.Integer, primary_key=True)
player1_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=False)
player2_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=True) # Nullable for singles matches
# Teams can participate in multiple matches
matches_as_team1 = db.relationship('Match', foreign_keys='Match.team1_id', backref='team1', lazy=True)
matches_as_team2 = db.relationship('Match', foreign_keys='Match.team2_id', backref='team2', lazy=True)
class Match(db.Model):
id = db.Column(db.Integer, primary_key=True)
tournament_id = db.Column(db.Integer, db.ForeignKey('tournament.id'), nullable=False)
round_name = db.Column(db.String(100), nullable=False)
group_name = db.Column(db.String(100)) # Optional group name
team1_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
team2_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=False)
score1 = db.Column(db.String(100), nullable=False) # e.g., "21-19, 19-21, 21-18"
score2 = db.Column(db.String(100), nullable=False) # e.g., "19-21, 21-19, 18-21"
match_type = db.Column(db.String(50), nullable=False) # e.g., "Men's Singles"
timestamp = db.Column(db.DateTime, default=db.func.current_timestamp())
def get_winner(self):
"""Determine the winner of the match based on scores"""
score1_sets = self.score1.split(', ')
score2_sets = self.score2.split(', ')
team1_wins = 0
team2_wins = 0
for i in range(min(len(score1_sets), len(score2_sets))):
try:
score1_points = int(score1_sets[i].split('-')[0])
score2_points = int(score2_sets[i].split('-')[0])
if score1_points > score2_points:
team1_wins += 1
else:
team2_wins += 1
except (ValueError, IndexError):
# If there's an error parsing scores, skip this set
continue
if team1_wins > team2_wins:
return self.team1
else:
return self.team2
class SharedTournament(db.Model):
"""Model for tournament sharing between users"""
id = db.Column(db.Integer, primary_key=True)
tournament_id = db.Column(db.Integer, db.ForeignKey('tournament.id', ondelete='CASCADE'), nullable=False)
owner_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False)
shared_with_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'), nullable=False)
shared_at = db.Column(db.DateTime, default=db.func.current_timestamp())
# Define relationships
tournament = db.relationship('Tournament', backref='shared_tournaments')
owner = db.relationship('User', foreign_keys=[owner_id], backref='tournaments_shared')
shared_with = db.relationship('User', foreign_keys=[shared_with_id], backref='tournaments_shared_with_me')
# Ensure a tournament is only shared once between same users
__table_args__ = (
db.UniqueConstraint('tournament_id', 'owner_id', 'shared_with_id', name='unique_tournament_sharing'),
)