from django.contrib.auth.models import User
from django.db import models
from django.utils.crypto import get_random_string
from simple_history.models import HistoricalRecords


class Team(models.Model):
    tid = models.CharField(max_length=32,
                           primary_key=True,
                           verbose_name="Team ID",
                           help_text="Alphanumeric name for the team.")
    name = models.CharField(max_length=100,
                            null=True,
                            verbose_name="Name",
                            help_text="Name of the team.")
    points = models.IntegerField(verbose_name="Points",
                                 help_text="The current amount of points of this group.",
                                 default=0)
    modified_reason = models.TextField(verbose_name="Modified Reason",
                                       help_text="The reason of last modification.",
                                       null=True)
    history = HistoricalRecords()

    def __str__(self):
        return str(self.name) + " (" + str(self.tid) + ")"


class Player(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.OneToOneField(User, verbose_name="User", help_text="The user of this player.")
    team = models.ForeignKey(Team,
                             verbose_name="Team",
                             help_text="The team this player belongs to.")
    points_acquired = models.IntegerField(verbose_name="Acquired Points",
                                          help_text="The amount of points acquired by this player.",
                                          default=0)
    modified_reason = models.TextField(verbose_name="Modified Reason",
                                       help_text="The reason of last modification.",
                                       null=True)
    history = HistoricalRecords()

    def __str__(self):
        return (str(self.user.get_full_name()) + " (" +
                str(self.user.get_username()) + "), " + self.team.name)


class Card(models.Model):
    cid = models.CharField(max_length=16,
                           primary_key=True,
                           default=get_random_string(16),
                           verbose_name="Card ID",
                           help_text="The unique ID for the points card.")
    value = models.IntegerField(default="1",
                                verbose_name="Value",
                                help_text="The value of the points card.")
    active = models.BooleanField(default=True, verbose_name="Active?")
    retrieved = models.BooleanField(default=False, verbose_name="Retrieved")
    name = models.CharField(
        max_length=32,
        verbose_name="Name",
        help_text="Name of the card.",
        null=True,
        blank=True)
    long_desc = models.TextField(verbose_name="Descriptions",
                                 help_text="Long descriptions about the card.",
                                 null=True,
                                 blank=True)
    modified_reason = models.TextField(
        verbose_name="Modified Reason",
        help_text="The reason of last modification.",
        null=True,
        blank=True)
    history = HistoricalRecords()

    def __str__(self):
        if self.active and not self.retrieved:
            return (str(self.name) + " (" + str(self.cid) + "),"
                    " active, " + str(self.value) + " points.")
        elif not self.active and self.retrieved:
            return (str(self.name) + " (" + str(self.cid) + "),"
                    "retrieved, " + str(self.value) + " points.")
        else:
            return (str(self.name) + " (" + str(self.cid) + "),"
                    " inactive, " + str(self.value) + " points.")


def is_player(user):
    return not user.is_staff

@@ -1116,959 +1116,8 @@
 %22%0A%0A%0A
-class Player(models.Model):%0A    id = models.AutoField(primary_key=True)%0A    user = models.OneToOneField(User, verbose_name=%22User%22, help_text=%22The user of this player.%22)%0A    team = models.ForeignKey(Team,%0A                             verbose_name=%22Team%22,%0A                             help_text=%22The team this player belongs to.%22)%0A    points_acquired = models.IntegerField(verbose_name=%22Acquired Points%22,%0A                                          help_text=%22The amount of points acquired by this player.%22,%0A                                          default=0)%0A    modified_reason = models.TextField(verbose_name=%22Modified Reason%22,%0A                                       help_text=%22The reason of last modification.%22,%0A                                       null=True)%0A    history = HistoricalRecords()%0A%0A    def __str__(self):%0A        return (str(self.user.get_full_name()) + %22 (%22 +%0A                str(self.user.get_username()) + %22), %22 + self.team.name)%0A%0A%0A
 clas
@@ -2855,16 +2855,1041 @@
 ts.%22)%0A%0A%0A
+class Player(models.Model):%0A    id = models.AutoField(primary_key=True)%0A    user = models.OneToOneField(User, verbose_name=%22User%22, help_text=%22The user of this player.%22)%0A    team = models.ForeignKey(Team,%0A                             verbose_name=%22Team%22,%0A                             help_text=%22The team this player belongs to.%22)%0A    points_acquired = models.IntegerField(verbose_name=%22Acquired Points%22,%0A                                          help_text=%22The amount of points acquired by this player.%22,%0A                                          default=0)%0A    modified_reason = models.TextField(verbose_name=%22Modified Reason%22,%0A                                       help_text=%22The reason of last modification.%22,%0A                                       null=True)%0A    captured_card = models.ManyToManyField(Card, related_name=%22captured%22)%0A    history = HistoricalRecords()%0A%0A    def __str__(self):%0A        return (str(self.user.get_full_name()) + %22 (%22 +%0A                str(self.user.get_username()) + %22), %22 + self.team.name)%0A%0A%0A
 def is_p

add column to player, reference to some cards. The card in captured_card column means which has been captured by player