<commit_before>from datetime import datetime, time
import random

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models import Count
from django.db.transaction import atomic
from django.template.defaultfilters import slugify
from django.utils import timezone

from pybb.models import *

from ultimate.utils.email_groups import *


def generateLeagueCoverImagePath(instance, filename):
    return 'images/{}/{}/{}/{}'.format(instance.year, instance.season.slug, instance.night_slug, filename)


class Field(models.Model):
    FIELD_TYPE_INDOOR = 'indoor'
    FIELD_TYPE_OUTDOOR = 'outdoor'
    FIELD_TYPE_CHOICES = (
        (FIELD_TYPE_INDOOR, 'Indoor'),
        (FIELD_TYPE_OUTDOOR, 'Outdoor'),
    )

    id = models.AutoField(primary_key=True)
    name = models.TextField()
    layout_link = models.TextField(blank=True)
    address = models.TextField(blank=True)
    driving_link = models.TextField(blank=True)
    note = models.TextField(blank=True)
    type = models.CharField(max_length=32, choices=FIELD_TYPE_CHOICES)

    class Meta:
        db_table = u'field'
        ordering = ['name']

    def __unicode__(self):
        return self.name

    @property
    def display_type(self):
        return dict(self.FIELD_TYPE_CHOICES)[self.type]


class FieldNames(models.Model):
    FIELD_TYPE_GRASS = 'grass'
    FIELD_TYPE_TURF = 'turf'
    FIELD_TYPE_CHOICES = (
        (FIELD_TYPE_GRASS, 'Grass'),
        (FIELD_TYPE_TURF, 'Turf'),
    )

    id = models.AutoField(primary_key=True)
    name = models.TextField()
    field = models.ForeignKey('leagues.Field')
    type = models.CharField(max_length=32, choices=FIELD_TYPE_CHOICES)

    class Meta:
        db_table = u'field_names'
        verbose_name_plural = 'field names'
        ordering = ['field__name', 'name']

    def __unicode__(self):
        return '%s %s' % (self.field.name, self.name)


class Season(models.Model):
    name = models.CharField(max_length=32)
    slug = models.SlugField()

    order = models.IntegerField(null=True, default=None)

    class Meta:
        ordering = ['order', 'name']

    def __unicode__(self):
        return '{}'.format(self.name)

    def save(self):
        if not self.slug:
            self.slug = slugify(self.name)

        super(Season, self).save()


class League(models.Model):
    LEAGUE_STATE_CANCELLED = 'cancelled'
    LEAGUE_STATE_CLOSED = 'closed'
    LEAGUE_STATE_HIDDEN = 'hidden'
    LEAGUE_STATE_OPEN = 'open'
    LEAGUE_STATE_PREVIEW = 'preview'
    LEAGUE_STATE_CHOICES = (
        (LEAGUE_STATE_CANCELLED, 'Cancelled - visible to all, registration closed to all'),
        (LEAGUE_STATE_CLOSED, 'Closed - visible to all, registration closed to all'),
        (LEAGUE_STATE_HIDDEN, 'Hidden - hidden to all, registration closed to all'),
        (LEAGUE_STATE_OPEN, 'Open - visible to all, registration conditionally open to all'),
        (LEAGUE_STATE_PREVIEW, 'Preview - visible only to admins, registration conditionally open only to admins'),
    )

    LEAGUE_GENDER_MENS = 'mens'
    LEAGUE_GENDER_MIXED = 'mixed'
    LEAGUE_GENDER_OPEN = 'open'
    LEAGUE_GENDER_WOMENS = 'womens'
    LEAGUE_GENDER_CHOICES = (
        (LEAGUE_GENDER_MENS, 'Men\'s'),
        (LEAGUE_GENDER_MIXED, 'Mixed'),
        (LEAGUE_GENDER_OPEN, 'Open'),
        (LEAGUE_GENDER_WOMENS, 'Women\'s'),
    )

    LEAGUE_LEVEL_COMPETITIVE = 'comp'
    LEAGUE_LEVEL_RECREATIONAL = 'rec'
    LEAGUE_LEVEL_YOUTH = 'youth'
    LEAGUE_LEVEL_CHOICES = (
        (LEAGUE_LEVEL_COMPETITIVE, 'Competitive'),
        (LEAGUE_LEVEL_RECREATIONAL, 'Recreational'),
        (LEAGUE_LEVEL_YOUTH, 'Youth'),
    )

    LEAGUE_TYPE_EVENT = 'event'
    LEAGUE_TYPE_LEAGUE = 'league'
    LEAGUE_TYPE_TOURNAMENT = 'tournament'
    LEAGUE_TYPE_CHOICES = (
        (LEAGUE_TYPE_EVENT, 'Event'),
        (LEAGUE_TYPE_LEAGUE, 'League'),
        (LEAGUE_TYPE_TOURNAMENT, 'Tournament'),
    )

    year = models.IntegerField(help_text='four digit year, e.g. 2013')
    season = models.ForeignKey('leagues.Season')
    night = models.CharField(max_length=32, help_text='lower case, no special characters, e.g. "sunday", "tuesday and thursday", "end of season tournament"')
    night_slug = models.SlugField()

    gender = models.CharField(max_length=32, choices=LEAGUE_GENDER_CHOICES)
    level = models.CharField(max_length=32, choices=LEAGUE_LEVEL_CHOICES)
    type = models.CharField(max_length=32, choices=LEAGUE_TYPE_CHOICES)

    tagline = models.TextField(blank=True, help_text='short tagline for description fields, e.g. SEO, Facebook, etc.')
    summary_info = models.TextField(help_text='notes for league, e.g. 50-50 league format, showcase league notes')
    detailed_info = models.TextField(help_text='details page text, use HTML')

    times = models.TextField(help_text='start to end time, e.g. 6:00-8:00pm')
    start_time = models.TimeField(null=True, help_text='start time for league')
    end_time = models.TimeField(null=True, help_text='end time for league')
    num_time_slots = models.IntegerField(default=1, help_text='number of time slots')
    schedule_note = models.TextField(blank=True, help_text='note to appear under the schedule')

    num_games_per_week = models.IntegerField(default=1, help_text='number of games per week, used to calculate number of games for a league')
    reg_start_date = models.DateTimeField(help_text='date and time that registration process is open (not currently automated)')
    price_increase_start_date = models.DateTimeField(help_text='date and time when cost increases for league')
    waitlist_start_date = models.DateTimeField(help_text='date and time that waitlist is started (regardless of number of registrations)')
    league_start_date = models.DateField(help_text='date of first game')
    league_end_date = models.DateField(help_text='date of last game')

    max_players = models.IntegerField(help_text='max players for league, extra registrations will be placed on waitlist')
    baggage = models.IntegerField(help_text='max baggage group size')
    min_age = models.IntegerField(help_text='minimum age (in years)', default=0)

    paypal_cost = models.IntegerField(help_text='base cost of league if paying by PayPal')
    checks_accepted = models.BooleanField(default=True)
    check_cost_increase = models.IntegerField(help_text='amount to be added to paypal_cost if paying by check')
    late_cost_increase = models.IntegerField(help_text='amount to be added to paypal_cost if paying after price_increase_start_date')
    mail_check_address = models.TextField(help_text='treasurer mailing address')
    coupons_accepted = models.BooleanField(default=True)

    fields = models.ManyToManyField(Field, through='leagues.LeagueFields', help_text='Select the fields these games will be played at, use the green "+" icon if we\'re playing at a new field.')

    division_email = models.CharField(max_length=64, blank=True, null=True, help_text='email address for just this league')
    division_email_group_id = models.CharField(max_length=128, blank=True, null=True)
    division_captains_email = models.CharField(max_length=64, blank=True, null=True, help_text='email address for league captains')
    division_captains_email_group_id = models.CharField(max_length=128, blank=True, null=True)

    state = models.CharField(max_length=32, choices=LEAGUE_STATE_CHOICES, help_text='state of league, changes whether registration is open or league is visible')

    image_cover = models.ImageField(upload_to=generateLeagueCoverImagePath, blank=True, null=True)

    class Meta:
        db_table = u'league'
        ordering = ['-year', '-season__order', 'league_start_date']

    def get_absolute_url(self):
        return reverse('league_summary', args=[self.year, self.season.slug, self.night_slug, ])

    def __unicode__(self):
        return ('%s %d %s' % (self.season, self.year, self.night))

    def save(self):
        if not self.night_slug:
            self.night_slug = slugify(self.night)

        super(League, self).save()

    @property
    def display_gender(self):
        return dict(self.LEAGUE_GENDER_CHOICES)[self.gender]

    @property
    def display_level(self):
        return dict(self.LEAGUE_LEVEL_CHOICES)[self.level]

    @property
    def display_state(self):
        return dict(self.LEAGUE_STATE_CHOICES)[self.state]

    @property
    def display_type(self):
        return dict(self.LEAGUE_TYPE_CHOICES)[self.type]

    @property
    def night_title(self):
        return ('%s' % (self.night)).replace('_', ' ')

    @property
    def season_title(self):
        return ('%s' % (self.season)).replace('_', ' ')

    @property
    def season_year(self):
        return '{} {}'.format(self.season, self.year)

    @property
    def gender_title(self):
        return ('%s' % (self.gender)).replace('_', ' ')

    @property
    def league_start_datetime(self):
        start_time = time(0, 0, 0)
        if self.start_time:
            start_time = self.start_time

        return datetime.combine(self.league_start_date, start_time)

    @property
    def league_end_datetime(self):
        end_time = time(23, 59, 59)
        if self.end_time:
            end_time = self.end_time

        return datetime.combine(self.league_end_date, end_time)

    @property
    def paypal_price(self):
        if self.paypal_cost == 0 or timezone.now() < self.price_increase_start_date:
            return self.paypal_cost

        return self.paypal_cost + self.late_cost_increase

    @property
    def check_price(self):
        if self.paypal_cost + self.check_cost_increase == 0 or timezone.now() < self.price_increase_start_date:
            return self.paypal_cost + self.check_cost_increase

        return self.paypal_cost + self.check_cost_increase + self.late_cost_increase

    @property
    def is_cancelled(self):
        return self.state == self.LEAGUE_STATE_CANCELLED

    @property
    def is_closed(self):
        return self.state == self.LEAGUE_STATE_CLOSED<commit_msg>fix price calculation<commit_after>from datetime import datetime, time
import random

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models import Count
from django.db.transaction import atomic
from django.template.defaultfilters import slugify
from django.utils import timezone

from pybb.models import *

from ultimate.utils.email_groups import *


def generateLeagueCoverImagePath(instance, filename):
    return 'images/{}/{}/{}/{}'.format(instance.year, instance.season.slug, instance.night_slug, filename)


class Field(models.Model):
    FIELD_TYPE_INDOOR = 'indoor'
    FIELD_TYPE_OUTDOOR = 'outdoor'
    FIELD_TYPE_CHOICES = (
        (FIELD_TYPE_INDOOR, 'Indoor'),
        (FIELD_TYPE_OUTDOOR, 'Outdoor'),
    )

    id = models.AutoField(primary_key=True)
    name = models.TextField()
    layout_link = models.TextField(blank=True)
    address = models.TextField(blank=True)
    driving_link = models.TextField(blank=True)
    note = models.TextField(blank=True)
    type = models.CharField(max_length=32, choices=FIELD_TYPE_CHOICES)

    class Meta:
        db_table = u'field'
        ordering = ['name']

    def __unicode__(self):
        return self.name

    @property
    def display_type(self):
        return dict(self.FIELD_TYPE_CHOICES)[self.type]


class FieldNames(models.Model):
    FIELD_TYPE_GRASS = 'grass'
    FIELD_TYPE_TURF = 'turf'
    FIELD_TYPE_CHOICES = (
        (FIELD_TYPE_GRASS, 'Grass'),
        (FIELD_TYPE_TURF, 'Turf'),
    )

    id = models.AutoField(primary_key=True)
    name = models.TextField()
    field = models.ForeignKey('leagues.Field')
    type = models.CharField(max_length=32, choices=FIELD_TYPE_CHOICES)

    class Meta:
        db_table = u'field_names'
        verbose_name_plural = 'field names'
        ordering = ['field__name', 'name']

    def __unicode__(self):
        return '%s %s' % (self.field.name, self.name)


class Season(models.Model):
    name = models.CharField(max_length=32)
    slug = models.SlugField()

    order = models.IntegerField(null=True, default=None)

    class Meta:
        ordering = ['order', 'name']

    def __unicode__(self):
        return '{}'.format(self.name)

    def save(self):
        if not self.slug:
            self.slug = slugify(self.name)

        super(Season, self).save()


class League(models.Model):
    LEAGUE_STATE_CANCELLED = 'cancelled'
    LEAGUE_STATE_CLOSED = 'closed'
    LEAGUE_STATE_HIDDEN = 'hidden'
    LEAGUE_STATE_OPEN = 'open'
    LEAGUE_STATE_PREVIEW = 'preview'
    LEAGUE_STATE_CHOICES = (
        (LEAGUE_STATE_CANCELLED, 'Cancelled - visible to all, registration closed to all'),
        (LEAGUE_STATE_CLOSED, 'Closed - visible to all, registration closed to all'),
        (LEAGUE_STATE_HIDDEN, 'Hidden - hidden to all, registration closed to all'),
        (LEAGUE_STATE_OPEN, 'Open - visible to all, registration conditionally open to all'),
        (LEAGUE_STATE_PREVIEW, 'Preview - visible only to admins, registration conditionally open only to admins'),
    )

    LEAGUE_GENDER_MENS = 'mens'
    LEAGUE_GENDER_MIXED = 'mixed'
    LEAGUE_GENDER_OPEN = 'open'
    LEAGUE_GENDER_WOMENS = 'womens'
    LEAGUE_GENDER_CHOICES = (
        (LEAGUE_GENDER_MENS, 'Men\'s'),
        (LEAGUE_GENDER_MIXED, 'Mixed'),
        (LEAGUE_GENDER_OPEN, 'Open'),
        (LEAGUE_GENDER_WOMENS, 'Women\'s'),
    )

    LEAGUE_LEVEL_COMPETITIVE = 'comp'
    LEAGUE_LEVEL_RECREATIONAL = 'rec'
    LEAGUE_LEVEL_YOUTH = 'youth'
    LEAGUE_LEVEL_CHOICES = (
        (LEAGUE_LEVEL_COMPETITIVE, 'Competitive'),
        (LEAGUE_LEVEL_RECREATIONAL, 'Recreational'),
        (LEAGUE_LEVEL_YOUTH, 'Youth'),
    )

    LEAGUE_TYPE_EVENT = 'event'
    LEAGUE_TYPE_LEAGUE = 'league'
    LEAGUE_TYPE_TOURNAMENT = 'tournament'
    LEAGUE_TYPE_CHOICES = (
        (LEAGUE_TYPE_EVENT, 'Event'),
        (LEAGUE_TYPE_LEAGUE, 'League'),
        (LEAGUE_TYPE_TOURNAMENT, 'Tournament'),
    )

    year = models.IntegerField(help_text='four digit year, e.g. 2013')
    season = models.ForeignKey('leagues.Season')
    night = models.CharField(max_length=32, help_text='lower case, no special characters, e.g. "sunday", "tuesday and thursday", "end of season tournament"')
    night_slug = models.SlugField()

    gender = models.CharField(max_length=32, choices=LEAGUE_GENDER_CHOICES)
    level = models.CharField(max_length=32, choices=LEAGUE_LEVEL_CHOICES)
    type = models.CharField(max_length=32, choices=LEAGUE_TYPE_CHOICES)

    tagline = models.TextField(blank=True, help_text='short tagline for description fields, e.g. SEO, Facebook, etc.')
    summary_info = models.TextField(help_text='notes for league, e.g. 50-50 league format, showcase league notes')
    detailed_info = models.TextField(help_text='details page text, use HTML')

    times = models.TextField(help_text='start to end time, e.g. 6:00-8:00pm')
    start_time = models.TimeField(null=True, help_text='start time for league')
    end_time = models.TimeField(null=True, help_text='end time for league')
    num_time_slots = models.IntegerField(default=1, help_text='number of time slots')
    schedule_note = models.TextField(blank=True, help_text='note to appear under the schedule')

    num_games_per_week = models.IntegerField(default=1, help_text='number of games per week, used to calculate number of games for a league')
    reg_start_date = models.DateTimeField(help_text='date and time that registration process is open (not currently automated)')
    price_increase_start_date = models.DateTimeField(help_text='date and time when cost increases for league')
    waitlist_start_date = models.DateTimeField(help_text='date and time that waitlist is started (regardless of number of registrations)')
    league_start_date = models.DateField(help_text='date of first game')
    league_end_date = models.DateField(help_text='date of last game')

    max_players = models.IntegerField(help_text='max players for league, extra registrations will be placed on waitlist')
    baggage = models.IntegerField(help_text='max baggage group size')
    min_age = models.IntegerField(help_text='minimum age (in years)', default=0)

    paypal_cost = models.IntegerField(help_text='base cost of league if paying by PayPal')
    checks_accepted = models.BooleanField(default=True)
    check_cost_increase = models.IntegerField(help_text='amount to be added to paypal_cost if paying by check')
    late_cost_increase = models.IntegerField(help_text='amount to be added to paypal_cost if paying after price_increase_start_date')
    mail_check_address = models.TextField(help_text='treasurer mailing address')
    coupons_accepted = models.BooleanField(default=True)

    fields = models.ManyToManyField(Field, through='leagues.LeagueFields', help_text='Select the fields these games will be played at, use the green "+" icon if we\'re playing at a new field.')

    division_email = models.CharField(max_length=64, blank=True, null=True, help_text='email address for just this league')
    division_email_group_id = models.CharField(max_length=128, blank=True, null=True)
    division_captains_email = models.CharField(max_length=64, blank=True, null=True, help_text='email address for league captains')
    division_captains_email_group_id = models.CharField(max_length=128, blank=True, null=True)

    state = models.CharField(max_length=32, choices=LEAGUE_STATE_CHOICES, help_text='state of league, changes whether registration is open or league is visible')

    image_cover = models.ImageField(upload_to=generateLeagueCoverImagePath, blank=True, null=True)

    class Meta:
        db_table = u'league'
        ordering = ['-year', '-season__order', 'league_start_date']

    def get_absolute_url(self):
        return reverse('league_summary', args=[self.year, self.season.slug, self.night_slug, ])

    def __unicode__(self):
        return ('%s %d %s' % (self.season, self.year, self.night))

    def save(self):
        if not self.night_slug:
            self.night_slug = slugify(self.night)

        super(League, self).save()

    @property
    def display_gender(self):
        return dict(self.LEAGUE_GENDER_CHOICES)[self.gender]

    @property
    def display_level(self):
        return dict(self.LEAGUE_LEVEL_CHOICES)[self.level]

    @property
    def display_state(self):
        return dict(self.LEAGUE_STATE_CHOICES)[self.state]

    @property
    def display_type(self):
        return dict(self.LEAGUE_TYPE_CHOICES)[self.type]

    @property
    def night_title(self):
        return ('%s' % (self.night)).replace('_', ' ')

    @property
    def season_title(self):
        return ('%s' % (self.season)).replace('_', ' ')

    @property
    def season_year(self):
        return '{} {}'.format(self.season, self.year)

    @property
    def gender_title(self):
        return ('%s' % (self.gender)).replace('_', ' ')

    @property
    def league_start_datetime(self):
        start_time = time(0, 0, 0)
        if self.start_time:
            start_time = self.start_time

        return datetime.combine(self.league_start_date, start_time)

    @property
    def league_end_datetime(self):
        end_time = time(23, 59, 59)
        if self.end_time:
            end_time = self.end_time

        return datetime.combine(self.league_end_date, end_time)

    @property
    def paypal_price(self):
        if timezone.now() < self.price_increase_start_date:
            return self.paypal_cost

        return self.paypal_cost + self.late_cost_increase

    @property
    def check_price(self):
        if timezone.now() < self.price_increase_start_date:
            return self.paypal_cost + self.check_cost_increase

        return self.paypal_cost + self.check_cost_increase + self.late_cost_increase

    @property
    def is_cancelled(self):
        return self.state == self.LEAGUE_STATE_CANCELLED

    @property
    def is_closed(self):
        return self.state == self.LEAGUE_STATE_CLOSED
{'fraction_non_alphanumeric': 0.10441926065224923, 'fraction_numerical': 0.005245706933240956, 'mean_word_length': 3.829827915869981, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6694902', 'n_tokens_mistral': 6719, 'n_tokens_neox': 6407, 'n_words': 1398}