Skip to content

Commit

Permalink
📒 Several tournament models modifications
Browse files Browse the repository at this point in the history
Models in the tournament field are modified thusly:
- The whole file is formatted
- A team is now always attached to a tournament, including when it is
  not validated. A boolean is added in `Team` to know if it has been
  validated
- Both player and manager registrations now have a foreign key to their
  ticket, which is null until the payment of their ticket has succeeded
- Admin view updated
- Tests updated
  • Loading branch information
Lymkwi committed Oct 15, 2023
1 parent 2dd7f6a commit 67060c7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 73 deletions.
6 changes: 3 additions & 3 deletions insalan/tournament/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TournamentAdmin(admin.ModelAdmin):
class TeamAdmin(admin.ModelAdmin):
"""Admin handler for Team"""

list_display = ("id", "name", "tournament")
list_display = ("id", "name", "tournament", "validated")
search_fields = ["name", "tournament"]


Expand All @@ -53,7 +53,7 @@ class TeamAdmin(admin.ModelAdmin):
class PlayerAdmin(admin.ModelAdmin):
"""Admin handler for Player Registrations"""

list_display = ("id", "user", "team", "payment_status")
list_display = ("id", "user", "team", "payment_status", "ticket")
search_fields = ["user", "team", "payment_status"]


Expand All @@ -63,7 +63,7 @@ class PlayerAdmin(admin.ModelAdmin):
class ManagerAdmin(admin.ModelAdmin):
"""Admin handler for Manager Registrations"""

list_display = ("id", "user", "team", "payment_status")
list_display = ("id", "user", "team", "payment_status", "ticket")
search_fields = ["user", "team", "payment_status"]


Expand Down
84 changes: 48 additions & 36 deletions insalan/tournament/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,20 @@ class Event(models.Model):
null=False,
)
description = models.CharField(
verbose_name=_("Description de l'évènement"), max_length=128, default="", blank=True
verbose_name=_("Description de l'évènement"),
max_length=128,
default="",
blank=True,
)
year = models.IntegerField(
verbose_name=_("Année"),
null=False,
validators=[MinValueValidator(2003)]
verbose_name=_("Année"), null=False, validators=[MinValueValidator(2003)]
)
month = models.IntegerField(
verbose_name=_("Mois"),
null=False,
validators=[MinValueValidator(1),MaxValueValidator(12)]
)
ongoing = models.BooleanField(
verbose_name=_("En cours"),
default=False
validators=[MinValueValidator(1), MaxValueValidator(12)],
)
ongoing = models.BooleanField(verbose_name=_("En cours"), default=False)
logo: models.FileField = models.FileField(
verbose_name=_("Logo"),
blank=True,
Expand All @@ -68,6 +66,7 @@ class Event(models.Model):

class Meta:
"""Meta options"""

verbose_name = _("Évènement")
verbose_name_plural = _("Évènements")

Expand All @@ -93,8 +92,10 @@ class Game(models.Model):
"""
A Game is the representation of a Game that is being played at InsaLan
"""

class Meta:
"""Meta options"""

verbose_name = _("Jeu")
verbose_name_plural = _("Jeux")

Expand Down Expand Up @@ -131,15 +132,9 @@ class Tournament(models.Model):
"""

event = models.ForeignKey(
Event,
verbose_name=_("Évènement"),
on_delete=models.CASCADE
)
game = models.ForeignKey(
Game,
verbose_name=_("Jeu"),
on_delete=models.CASCADE
Event, verbose_name=_("Évènement"), on_delete=models.CASCADE
)
game = models.ForeignKey(Game, verbose_name=_("Jeu"), on_delete=models.CASCADE)
name = models.CharField(
verbose_name=_("Nom du tournoi"),
validators=[MinLengthValidator(3)],
Expand All @@ -164,6 +159,7 @@ class Tournament(models.Model):

class Meta:
"""Meta options"""

verbose_name = _("Tournoi")
verbose_name_plural = _("Tournois")

Expand Down Expand Up @@ -204,20 +200,24 @@ class Team(models.Model):

tournament = models.ForeignKey(
Tournament,
null=True,
blank=True,
on_delete=models.SET_NULL,
verbose_name=_("Tournoi")
null=False,
blank=False,
on_delete=models.CASCADE,
verbose_name=_("Tournoi"),
)
name = models.CharField(
max_length=42,
validators=[MinLengthValidator(3)],
null=False,
verbose_name=_("Nom d'équipe"),
)
validated = models.BooleanField(
default=False, blank=True, verbose_name=_("Équipe validée")
)

class Meta:
"""Meta Options"""

verbose_name = _("Équipe")
verbose_name_plural = _("Équipes")
constraints = [
Expand All @@ -228,7 +228,7 @@ class Meta:

def __str__(self) -> str:
"""Format this team to a str"""
if self.tournament is not None:
if self.tournament is not None:
return f"{self.name} ({self.tournament.event})"
return f"{self.name} (???)"

Expand Down Expand Up @@ -290,22 +290,28 @@ class Meta:
verbose_name_plural = _("Inscription de joueurâ‹…euses")

user = models.ForeignKey(
User,
on_delete=models.CASCADE,
verbose_name=_("Utilisateurâ‹…ice")
User, on_delete=models.CASCADE, verbose_name=_("Utilisateurâ‹…ice")
)
team = models.ForeignKey(
"tournament.Team",
on_delete=models.CASCADE,
verbose_name = _("Équipe"),
verbose_name=_("Équipe"),
)
payment_status = models.CharField(
max_length=10,
blank=True,
default=PaymentStatus.NOT_PAID,
choices=PaymentStatus.choices,
null=False,
verbose_name = _("Statut du paiement"),
verbose_name=_("Statut du paiement"),
)
ticket = models.ForeignKey(
Ticket,
on_delete=models.SET_NULL,
verbose_name=_("Ticket"),
null=True,
blank=True,
default=None,
)

def __str__(self) -> str:
Expand Down Expand Up @@ -352,13 +358,12 @@ class Manager(models.Model):
A Manager is someone in charge of heading a team of players.
"""

user = models.ForeignKey(User,
verbose_name=_("Utilisateurâ‹…ice"),
on_delete=models.CASCADE
user = models.ForeignKey(
User, verbose_name=_("Utilisateurâ‹…ice"), on_delete=models.CASCADE
)
team = models.ForeignKey(
"tournament.Team", verbose_name=_("Équipe"), on_delete=models.CASCADE
)
team = models.ForeignKey("tournament.Team",
verbose_name=_("Équipe"),
on_delete=models.CASCADE)
payment_status = models.CharField(
verbose_name=_("Statut du paiement"),
max_length=10,
Expand All @@ -367,6 +372,14 @@ class Manager(models.Model):
choices=PaymentStatus.choices,
null=False,
)
ticket = models.ForeignKey(
Ticket,
on_delete=models.SET_NULL,
verbose_name=_("Ticket"),
null=True,
blank=True,
default=None,
)

class Meta:
"""Meta Options"""
Expand All @@ -381,9 +394,7 @@ class Meta:

def __str__(self) -> str:
"""Format this manager registration as a str"""
return (
f"(Manager) {self.user.username} for {self.team}"
)
return f"(Manager) {self.user.username} for {self.team}"

def as_user(self) -> User:
"""Return the current player as a User object"""
Expand All @@ -393,4 +404,5 @@ def get_team(self):
"""Return the Team object of the current team"""
return self.team


# vim: set cc=80 tw=80:
54 changes: 20 additions & 34 deletions insalan/tournament/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,23 +473,6 @@ def test_payment_status_set(self):

self.assertEqual(PaymentStatus.PAY_LATER, play_reg.payment_status)

def test_get_full_null_tournament(self):
"""Get a team with a null tournament"""
team = Team.objects.create(name="LaZone", tournament=None)

team.full_clean()

self.assertIsNone(team.get_tournament())

def test_team_null_tourney_repr(self):
"""
Test that the representation of a Team when its tournament is null is as
expectde.
"""
team = Team.objects.create(name="LaZone", tournament=None)

self.assertEqual(str(team), "LaZone (???)")

def test_get_team_players(self):
"""Get the players of a Team"""
team = Team.objects.get(name="LaLooze")
Expand Down Expand Up @@ -550,18 +533,6 @@ def test_team_name_too_long(self):
team.name = "C" * 42
team.full_clean()

def test_tournament_deletion_set_null(self):
"""Verify that a Team is deleted when its Tournament is"""
team = Team.objects.all()[0]
tourney = team.tournament

Team.objects.get(id=team.id)

# Delete and verify
tourney.delete()

self.assertIsInstance(Team.objects.get(id=team.id).tournament, NoneType)


# Player Class Tests
class PlayerTestCase(TestCase):
Expand Down Expand Up @@ -773,8 +744,10 @@ def test_get_player_team_correct(self):
def test_player_team_deletion(self):
"""Verify the behaviour of a Player when their team gets deleted"""
user_obj = User.objects.get(username="testplayer")
event = Event.objects.get(year=2023, month=8)
trnm = Tournament.objects.get(event=event)
# Create a team and player
team_obj = Team.objects.create(name="La Team Test", tournament=None)
team_obj = Team.objects.create(name="La Team Test Player", tournament=trnm)
play_obj = Player.objects.create(team=team_obj, user=user_obj)

Player.objects.get(id=play_obj.id)
Expand All @@ -787,8 +760,10 @@ def test_player_team_deletion(self):
def test_user_deletion(self):
"""Verify that a Player registration is deleted along with its user"""
user_obj = User.objects.get(username="testplayer")
event = Event.objects.get(year=2023, month=8)
trnm = Tournament.objects.get(event=event)
# Create a Player registration
team_obj = Team.objects.create(name="La Team Test", tournament=None)
team_obj = Team.objects.create(name="La Team Test User", tournament=trnm)
play_obj = Player.objects.create(team=team_obj, user=user_obj)

# Test
Expand Down Expand Up @@ -1167,8 +1142,13 @@ def test_one_manager_many_teams_diff_event_diff_tournament_diff_team(self):
def test_manager_team_deletion(self):
"""Verify the behaviour of a Manager when their team gets deleted"""
user_obj = User.objects.get(username="testplayer")
event = Event.objects.create(
name="InsaLan Test", year=2023, month=8, description=""
)
game = Game.objects.create(name="Test Game")
trnm = Tournament.objects.create(game=game, event=event)
# Create a team and player
team_obj = Team.objects.create(name="La Team Test", tournament=None)
team_obj = Team.objects.create(name="La Team Test", tournament=trnm)
play_obj = Manager.objects.create(team=team_obj, user=user_obj)

Manager.objects.get(id=play_obj.id)
Expand All @@ -1181,8 +1161,14 @@ def test_manager_team_deletion(self):
def test_user_deletion(self):
"""Verify that a Manager registration is deleted along with its user"""
user_obj = User.objects.get(username="testplayer")
# Create a Manager registration
team_obj = Team.objects.create(name="La Team Test", tournament=None)
event = Event.objects.create(
name="InsaLan Test", year=2023, month=8, description=""
)
game = Game.objects.create(name="Test Game")
trnm = Tournament.objects.create(
game=game, event=event
) # Create a Manager registration
team_obj = Team.objects.create(name="La Team Test", tournament=trnm)
man_obj = Manager.objects.create(team=team_obj, user=user_obj)

# Test
Expand Down

0 comments on commit 67060c7

Please sign in to comment.