-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
162 lines (124 loc) · 5.91 KB
/
models.py
File metadata and controls
162 lines (124 loc) · 5.91 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
import datetime
from decimal import Decimal
from sqlalchemy import (
Integer,
String,
Numeric,
Float,
ForeignKey,
Date,
UniqueConstraint,
)
from sqlalchemy.orm import relationship, Mapped, mapped_column
from sqlalchemy.ext.hybrid import hybrid_property
from db import Base
class TippingGame(Base):
__tablename__ = "tipping_games"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(200), nullable=False)
stake_per_person: Mapped[Decimal] = mapped_column(Numeric(10, 2), nullable=False, default=0)
url: Mapped[str] = mapped_column(String(500), nullable=True)
# Beziehungen
members: Mapped[list["Member"]] = relationship(
"Member",
back_populates="game",
cascade="all, delete-orphan",
passive_deletes=True,
lazy="selectin",
)
config: Mapped["GameConfig"] = relationship(
"GameConfig",
back_populates="game",
cascade="all, delete-orphan",
uselist=False,
lazy="selectin",
)
placement_payouts: Mapped[list["PlacementPayout"]] = relationship(
"PlacementPayout",
back_populates="game",
cascade="all, delete-orphan",
order_by="PlacementPayout.rank.asc()",
lazy="selectin",
)
@hybrid_property
def total_stake(self) -> Decimal:
count = len(self.members) if self.members is not None else 0
return (self.stake_per_person or Decimal("0")) * Decimal(count)
class Member(Base):
__tablename__ = "members"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
game_id: Mapped[int] = mapped_column(ForeignKey("tipping_games.id", ondelete="CASCADE"), nullable=False)
first_name: Mapped[str] = mapped_column(String(120), nullable=False)
last_name: Mapped[str] = mapped_column(String(120), nullable=False)
email: Mapped[str] = mapped_column(String(255), nullable=False)
nickname: Mapped[str] = mapped_column(String(120))
game: Mapped["TippingGame"] = relationship("TippingGame", back_populates="members")
payment_method: Mapped["PaymentMethod"] = relationship(
"PaymentMethod",
back_populates="member",
cascade="all, delete-orphan",
uselist=False,
lazy="selectin",
)
victory_statuses: Mapped[list["VictoryStatus"]] = relationship(
"VictoryStatus",
back_populates="member",
cascade="all, delete-orphan",
order_by="VictoryStatus.date.desc()",
lazy="selectin",
)
points_statuses: Mapped[list["PointsStatus"]] = relationship(
"PointsStatus",
back_populates="member",
cascade="all, delete-orphan",
order_by="PointsStatus.date.desc()",
lazy="selectin",
)
@property
def latest_victory(self) -> "VictoryStatus | None":
return self.victory_statuses[0] if self.victory_statuses else None
@property
def latest_points(self) -> "PointsStatus | None":
return self.points_statuses[0] if self.points_statuses else None
class PaymentMethod(Base):
__tablename__ = "payment_methods"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
label: Mapped[str] = mapped_column(String(120), nullable=False) # Bezeichnung
reference: Mapped[str] = mapped_column(String(255)) # Referenz
member_id: Mapped[int] = mapped_column(ForeignKey("members.id", ondelete="CASCADE"), unique=True)
member: Mapped["Member"] = relationship("Member", back_populates="payment_method")
class VictoryStatus(Base):
__tablename__ = "victory_statuses"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
member_id: Mapped[int] = mapped_column(ForeignKey("members.id", ondelete="CASCADE"), nullable=False)
victories: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
date: Mapped[datetime.date] = mapped_column(Date, nullable=False)
member: Mapped["Member"] = relationship("Member", back_populates="victory_statuses")
class PointsStatus(Base):
__tablename__ = "points_statuses"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
member_id: Mapped[int] = mapped_column(ForeignKey("members.id", ondelete="CASCADE"), nullable=False)
points: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
date: Mapped[datetime.date] = mapped_column(Date, nullable=False)
member: Mapped["Member"] = relationship("Member", back_populates="points_statuses")
# ------------------------------
# Erweiterung: Spiel-Konfiguration
# ------------------------------
class GameConfig(Base):
__tablename__ = "game_configs"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
game_id: Mapped[int] = mapped_column(ForeignKey("tipping_games.id", ondelete="CASCADE"), unique=True)
# Anteile in Prozent (0..100)
victory_share_percent: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=50)
placement_share_percent: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=50)
# Für Siege
num_matchdays: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
game: Mapped["TippingGame"] = relationship("TippingGame", back_populates="config")
class PlacementPayout(Base):
__tablename__ = "placement_payouts"
__table_args__ = (UniqueConstraint("game_id", "rank", name="uq_game_rank"),)
id: Mapped[int] = mapped_column(Integer, primary_key=True)
game_id: Mapped[int] = mapped_column(ForeignKey("tipping_games.id", ondelete="CASCADE"), nullable=False)
rank: Mapped[int] = mapped_column(Integer, nullable=False) # 1 = Sieger, 2 = Platz 2, ...
percent: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False) # Anteil vom Platzierungs-Topf
game: Mapped["TippingGame"] = relationship("TippingGame", back_populates="placement_payouts")