<commit_before>from __future__ import unicode_literals

from django.db import models
from django.db.models.signals import m2m_changed, pre_save
from django.core.exceptions import ValidationError
from driver27 import punctuation
from slugify import slugify

try:
    from django_countries.fields import CountryField
except ImportError:
    raise (
        'You are using the `driver27` app which requires the `django-countries` module.'
        'Be sure to add `django_countries` to your INSTALLED_APPS for `driver27` to work properly.'
    )

class Driver(models.Model):
    last_name = models.CharField(max_length=50)
    first_name = models.CharField(max_length=25)
    year_of_birth = models.IntegerField()
    country = CountryField()
    competitions = models.ManyToManyField('Competition', through='Contender', related_name='drivers')

    def save(self, *args, **kwargs):
        if self.year_of_birth < 1900 or self.year_of_birth > 2099:
            raise ValidationError('Year_of_birth must be between 1900 and 2099')
        super(Driver, self).save(*args, **kwargs)

    def __unicode__(self):
        return ', '.join((self.last_name, self.first_name))

    class Meta:
        unique_together = ('last_name', 'first_name')
        ordering = ['last_name', 'first_name']

class Contender(models.Model):
    driver = models.ForeignKey(Driver, related_name='career')
    competition = models.ForeignKey('Competition', related_name='contenders')
    teams = models.ManyToManyField('Team', through='Seat', related_name='contenders')

    def get_season(self, season):
        contender_season = ContenderSeason(self, season)
        contender_season.set_teams()
        return contender_season

    @property
    def teams_verbose(self):
        teams = self.teams
        return ', '.join([team.name for team in teams.all()]) if teams.count() else None

    def __unicode__(self):
        return '%s %s %s' % (self.driver, 'in', self.competition)

    class Meta:
        unique_together = ('driver', 'competition')
        ordering = ['competition__name', 'driver__last_name', 'driver__first_name']

class Seat(models.Model):
    team = models.ForeignKey('Team', related_name='seats')
    contender = models.ForeignKey('Contender', related_name='seats')
    current = models.BooleanField(default=False)
    seasons = models.ManyToManyField('Season', related_name='seats', blank=True, default=None)

    def save(self, *args, **kwargs):
        if self.contender.competition not in self.team.competitions.all():
            raise ValidationError(
                "%s is not a team of %s" % (self.team, self.contender.competition)
            )
        if self.current:
            current_count = Seat.objects.filter(contender=self.contender, current=True)
            if self.pk:
                current_count = current_count.exclude(pk=self.pk)
            if current_count.count():
                self.current = False
        super(Seat, self).save(*args, **kwargs)

    def __unicode__(self):
        return '%s %s %s' % (self.contender.driver, 'in', self.team)

    class Meta:
        unique_together = ('team', 'contender')
        ordering = ['contender__driver__last_name', 'team']


def seat_season(sender, instance, action, pk_set, **kwargs):
    """ Signal in DriverCompetitionTeam.seasons to avoid seasons which not is in competition"""
    contender_competition = instance.contender.competition
    accepted_seasons = [season.pk for season in contender_competition.seasons.all()]
    for pk in pk_set:
        if pk not in accepted_seasons:
            pk_season = Season.objects.get(pk=pk)
            raise ValidationError(
                '%s is not a/an %s season' % (pk_season, contender_competition)
            )

m2m_changed.connect(seat_season, sender=Seat.seasons.through)




class Team(models.Model):
    name = models.CharField(max_length=75, verbose_name='team', unique=True)
    full_name = models.CharField(max_length=200, unique=True)
    competitions = models.ManyToManyField('Competition', related_name='teams')
    country = CountryField()

    def __unicode__(self):
        return self.name

    class Meta:<commit_msg>fix pk_set error in seat_season (tox: py27-d17)<commit_after>from __future__ import unicode_literals

from django.db import models
from django.db.models.signals import m2m_changed, pre_save
from django.core.exceptions import ValidationError
from driver27 import punctuation
from slugify import slugify

try:
    from django_countries.fields import CountryField
except ImportError:
    raise (
        'You are using the `driver27` app which requires the `django-countries` module.'
        'Be sure to add `django_countries` to your INSTALLED_APPS for `driver27` to work properly.'
    )

class Driver(models.Model):
    last_name = models.CharField(max_length=50)
    first_name = models.CharField(max_length=25)
    year_of_birth = models.IntegerField()
    country = CountryField()
    competitions = models.ManyToManyField('Competition', through='Contender', related_name='drivers')

    def save(self, *args, **kwargs):
        if self.year_of_birth < 1900 or self.year_of_birth > 2099:
            raise ValidationError('Year_of_birth must be between 1900 and 2099')
        super(Driver, self).save(*args, **kwargs)

    def __unicode__(self):
        return ', '.join((self.last_name, self.first_name))

    class Meta:
        unique_together = ('last_name', 'first_name')
        ordering = ['last_name', 'first_name']

class Contender(models.Model):
    driver = models.ForeignKey(Driver, related_name='career')
    competition = models.ForeignKey('Competition', related_name='contenders')
    teams = models.ManyToManyField('Team', through='Seat', related_name='contenders')

    def get_season(self, season):
        contender_season = ContenderSeason(self, season)
        contender_season.set_teams()
        return contender_season

    @property
    def teams_verbose(self):
        teams = self.teams
        return ', '.join([team.name for team in teams.all()]) if teams.count() else None

    def __unicode__(self):
        return '%s %s %s' % (self.driver, 'in', self.competition)

    class Meta:
        unique_together = ('driver', 'competition')
        ordering = ['competition__name', 'driver__last_name', 'driver__first_name']

class Seat(models.Model):
    team = models.ForeignKey('Team', related_name='seats')
    contender = models.ForeignKey('Contender', related_name='seats')
    current = models.BooleanField(default=False)
    seasons = models.ManyToManyField('Season', related_name='seats', blank=True, default=None)

    def save(self, *args, **kwargs):
        if self.contender.competition not in self.team.competitions.all():
            raise ValidationError(
                "%s is not a team of %s" % (self.team, self.contender.competition)
            )
        if self.current:
            current_count = Seat.objects.filter(contender=self.contender, current=True)
            if self.pk:
                current_count = current_count.exclude(pk=self.pk)
            if current_count.count():
                self.current = False
        super(Seat, self).save(*args, **kwargs)

    def __unicode__(self):
        return '%s %s %s' % (self.contender.driver, 'in', self.team)

    class Meta:
        unique_together = ('team', 'contender')
        ordering = ['contender__driver__last_name', 'team']


def seat_season(sender, instance, action, pk_set, **kwargs):
    """ Signal in DriverCompetitionTeam.seasons to avoid seasons which not is in competition"""
    contender_competition = instance.contender.competition
    accepted_seasons = [season.pk for season in contender_competition.seasons.all()]
    if pk_set:
        for pk in pk_set:
            if pk not in accepted_seasons:
                pk_season = Season.objects.get(pk=pk)
                raise ValidationError(
                    '%s is not a/an %s season' % (pk_season, contender_competition)
                )

m2m_changed.connect(seat_season, sender=Seat.seasons.through)




class Team(models.Model):
    name = models.CharField(max_length=75, verbose_name='team', unique=True)
    full_name = models.CharField(max_length=200, unique=True)
    competitions = models.ManyToManyField('Competition', related_name='teams')
    country = CountryField()

    def __unicode__(self):
        return self.name

    class Meta:
{'fraction_non_alphanumeric': 0.11079376413185767, 'fraction_numerical': 0.008330358205402833, 'mean_word_length': 3.3953974895397487, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1411689', 'n_tokens_mistral': 2597, 'n_tokens_neox': 2461, 'n_words': 597}