# Path: foosball/games/models.py
# class Game(TimeStampedModel):
#     created_by = models.ForeignKey(
#         User, related_name="games_created", editable=False, null=True, verbose_name=_("created by")
#     )
#     modified_by = models.ForeignKey(
#         User, related_name="games_modified", editable=False, null=True, verbose_name=_("modified by")
#     )
# 
#     played_at = models.DateTimeField(
#         default=timezone.now,
#         verbose_name=_("played at"),
#         help_text=_("Time when the game was played")
#     )
# 
#     table = models.ForeignKey(Table, blank=True, null=True, related_name="games", verbose_name=_("table"))
# 
#     class Meta:
#         ordering = ['-played_at']
# 
#     def __str__(self):
#         return " - ".join(map(str, self.teams.all()))
# 
# class Team(models.Model):
#     UNKNOWN = 0
#     RED = 1
#     BLUE = 2
#     SIDE_CHOICES = (
#         (UNKNOWN, _("Unknown")),
#         (RED, _("Red")),
#         (BLUE, _("Blue")),
#     )
#     side = models.IntegerField(db_index=True, choices=SIDE_CHOICES, default=UNKNOWN, verbose_name=_("side"))
# 
#     game = models.ForeignKey(Game, related_name="teams", on_delete=models.CASCADE)
#     score = models.PositiveSmallIntegerField(
#         verbose_name=_("score"),
#         help_text=_("Team's score in the game"),
#         validators=[MaxValueValidator(limit_value=10)]
#     )
#     players = models.ManyToManyField(User)
# 
#     def __str__(self):
#         return "%s (%s)" % (
#             self.score,
#             ", ".join(map(str, self.players.all()))
#         )
# 
# class Table(models.Model):
#     name = models.CharField(unique=True, max_length=255, verbose_name=_("name"))
#     description = models.TextField(blank=True, verbose_name=_("description"))
# 
#     def __str__(self):
#         return self.name
# 
# Path: foosball/games/utils.py
# def clean_team_forms(team_form_1, team_form_2):
#     """
#     Extra validation for Team forms.
# 
#     Checks max number of players and that a player can only play in one team.
#     Adds error messages to the forms. Returns info whether all the extra
#     validations are valid.
# 
#     :type team_form_1: TeamModelForm
#     :type team_form_2: TeamModelForm
#     :rtype: bool
#     """
#     teams = [list(), list()]
#     valid = True
# 
#     for i, form in enumerate((team_form_1, team_form_2)):
#         players = form.cleaned_data.get('players')
#         if not players:
#             continue  # 'This field is required' error is shown here
#         if players.count() > 2:
#             form.add_error('players', _('Maximum number of players is two.'))
#             valid = False
#         teams[i] = list(players.all())
# 
#     if len(set(teams[0]).union(teams[1])) != len(teams[0] + teams[1]):
#         for form in (team_form_1, team_form_2):
#             form.add_error('players', _('Teams contain same players.'))
#         valid = False
# 
#     return valid


from django import forms
from django.contrib import admin
from .models import Game, Team, Table
from .utils import clean_team_forms




class TeamForm(forms.ModelForm):
    model = Team

    def has_changed(self):
        # force Teams to be saved also when they aren't modified,
        # prevents Games with less than two Teams
        return True


class TeamInlineFormSet(forms.models.BaseInlineFormSet):
    def clean(self):
        clean_team_forms(self.forms[0], self.forms[1])


class TeamInline(admin.TabularInline):

# Path: foosball/games/models.py
# class Game(TimeStampedModel):
#     created_by = models.ForeignKey(
#         User, related_name="games_created", editable=False, null=True, verbose_name=_("created by")
#     )
#     modified_by = models.ForeignKey(
#         User, related_name="games_modified", editable=False, null=True, verbose_name=_("modified by")
#     )
# 
#     played_at = models.DateTimeField(
#         default=timezone.now,
#         verbose_name=_("played at"),
#         help_text=_("Time when the game was played")
#     )
# 
#     table = models.ForeignKey(Table, blank=True, null=True, related_name="games", verbose_name=_("table"))
# 
#     class Meta:
#         ordering = ['-played_at']
# 
#     def __str__(self):
#         return " - ".join(map(str, self.teams.all()))
# 
# class Team(models.Model):
#     UNKNOWN = 0
#     RED = 1
#     BLUE = 2
#     SIDE_CHOICES = (
#         (UNKNOWN, _("Unknown")),
#         (RED, _("Red")),
#         (BLUE, _("Blue")),
#     )
#     side = models.IntegerField(db_index=True, choices=SIDE_CHOICES, default=UNKNOWN, verbose_name=_("side"))
# 
#     game = models.ForeignKey(Game, related_name="teams", on_delete=models.CASCADE)
#     score = models.PositiveSmallIntegerField(
#         verbose_name=_("score"),
#         help_text=_("Team's score in the game"),
#         validators=[MaxValueValidator(limit_value=10)]
#     )
#     players = models.ManyToManyField(User)
# 
#     def __str__(self):
#         return "%s (%s)" % (
#             self.score,
#             ", ".join(map(str, self.players.all()))
#         )
# 
# class Table(models.Model):
#     name = models.CharField(unique=True, max_length=255, verbose_name=_("name"))
#     description = models.TextField(blank=True, verbose_name=_("description"))
# 
#     def __str__(self):
#         return self.name
# 
# Path: foosball/games/utils.py
# def clean_team_forms(team_form_1, team_form_2):
#     """
#     Extra validation for Team forms.
# 
#     Checks max number of players and that a player can only play in one team.
#     Adds error messages to the forms. Returns info whether all the extra
#     validations are valid.
# 
#     :type team_form_1: TeamModelForm
#     :type team_form_2: TeamModelForm
#     :rtype: bool
#     """
#     teams = [list(), list()]
#     valid = True
# 
#     for i, form in enumerate((team_form_1, team_form_2)):
#         players = form.cleaned_data.get('players')
#         if not players:
#             continue  # 'This field is required' error is shown here
#         if players.count() > 2:
#             form.add_error('players', _('Maximum number of players is two.'))
#             valid = False
#         teams[i] = list(players.all())
# 
#     if len(set(teams[0]).union(teams[1])) != len(teams[0] + teams[1]):
#         for form in (team_form_1, team_form_2):
#             form.add_error('players', _('Teams contain same players.'))
#         valid = False
# 
#     return valid


# Path: foosball/games/admin.py
from django import forms
from django.contrib import admin
from .models import Game, Team, Table
from .utils import clean_team_forms





class TeamForm(forms.ModelForm):
    model = Team

    def has_changed(self):
        # force Teams to be saved also when they aren't modified,
        # prevents Games with less than two Teams
        return True


class TeamInlineFormSet(forms.models.BaseInlineFormSet):
    def clean(self):
        clean_team_forms(self.forms[0], self.forms[1])


class TeamInline(admin.TabularInline):

    model = Team