<commit_before>import constants
import reversion
import uuid

from datetime import datetime

from django.db import models
from django.db.models.query_utils import Q
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from core.models import BaseModel
from source.models import Source
from ckeditor.fields import RichTextField


def get_str_uuid():
    return str(uuid.uuid4())


def this_year():
    return datetime.now().year


class ContractManager(models.Manager):
    """
        Custom manager for filtering active contracts
    """

    def valid(self):
        return self.get_queryset().filter(
            Q(state=constants.CONTRACT_STATE_VALID) &
            Q(Q(valid_to__gte=datetime.now()) | Q(valid_to=None))
        )


@reversion.register(exclude=('last_changed',))
class Contract(BaseModel):
    source = models.ForeignKey(Source)
    state = models.CharField(choices=constants.CONTRACT_STATES,
                             default=constants.CONTRACT_STATE_NEGOTIATION,
                             max_length=15)

    valid_from = models.DateField(null=True, blank=True)
    valid_to = models.DateField(null=True, blank=True)
    year = models.PositiveIntegerField(_('Year'), default=this_year())

    contract_file = models.FileField(null=True, blank=True,
                                     upload_to='contracts')
    contract_type = models.CharField(choices=constants.CONTRACT_TYPE_CHOICES,
                                     max_length=12)
    contract_number = models.IntegerField(null=True, blank=True,
                                          unique_for_year='created')

    in_communication = models.BooleanField(
        help_text=_('Does the publisher responds to the emails?'),
        default=False)

    description = models.TextField(null=True, blank=True)
    objects = ContractManager()

    def __unicode__(self):
        return _('{number}/{year}: {type}').format(
            number=self.contract_number or ' - ',
            year=self.year,
            type=self.get_contract_type_display())

    def publisher_responds(self):
        return (self.in_communication or
                self.state != constants.CONTRACT_STATE_NEGOTIATION)

    def get_absolute_url(self):
        return reverse('contracts:detail', args=[str(self.id)])

    def is_valid(self):
        """
        Checks that contract is valid and that it did not expire.
        If it is expired self.valid will be set to false.
        """
        if self.state == constants.CONTRACT_STATE_VALID:
            expired = (self.valid_to < datetime.now()
                       if self.valid_to else False)
            if expired:
                self.state = constants.CONTRACT_STATE_EXPIRED
                self.save()
            return not expired
        return False

    def get_style(self):
        """
        Helper class to get css class according to status of the contract
        """
        if self.is_valid():
            return 'success'
        return 'danger'

    @classmethod
    def new_contract_number(cls):
        return cls.objects.filter(year=this_year()).aggregate(
            max_id=models.Max('contract_number')).get('max_id', 0) + 1


@reversion.register(exclude=('last_changed',))
class EmailNegotiation(models.Model):
    """
        This model represents an email that is going to be sent to the
        publisher, its content will be pre-filled with html template.
    """
    contract = models.ForeignKey(Contract)
    sent = models.BooleanField(default=False)

    title = models.CharField(max_length=64)

    scheduled_date = models.DateField(_('When to send this message'))
    content = RichTextField()
    template = models.CharField(max_length=64)

    class Meta:
        ordering = ('scheduled_date', )
<commit_msg>Contract translations<commit_after>import constants
import reversion
import uuid

from datetime import datetime

from django.db import models
from django.db.models.query_utils import Q
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from core.models import BaseModel
from source.models import Source
from ckeditor.fields import RichTextField


def get_str_uuid():
    return str(uuid.uuid4())


def this_year():
    return datetime.now().year


class ContractManager(models.Manager):
    """
        Custom manager for filtering active contracts
    """

    def valid(self):
        return self.get_queryset().filter(
            Q(state=constants.CONTRACT_STATE_VALID) &
            Q(Q(valid_to__gte=datetime.now()) | Q(valid_to=None))
        )


@reversion.register(exclude=('last_changed',))
class Contract(BaseModel):
    source = models.ForeignKey(Source)
    state = models.CharField(_('State'),
                             hoices=constants.CONTRACT_STATES,
                             default=constants.CONTRACT_STATE_NEGOTIATION,
                             max_length=15)

    valid_from = models.DateField(_('Valid from'), null=True, blank=True)
    valid_to = models.DateField(_('Valid to'), null=True, blank=True)
    year = models.PositiveIntegerField(_('Year'), default=this_year())

    contract_file = models.FileField(_('Contract file'), null=True, blank=True,
                                     upload_to='contracts')
    contract_type = models.CharField(_('Contract type'),
                                     choices=constants.CONTRACT_TYPE_CHOICES,
                                     max_length=12)
    contract_number = models.IntegerField(_('Contract number'),
                                          null=True, blank=True,
                                          unique_for_year='created')

    in_communication = models.BooleanField(
        _('In communication'),
        help_text=_('Does the publisher responds to the emails?'),
        default=False)

    description = models.TextField(_('Description'), null=True, blank=True)
    objects = ContractManager()

    def __unicode__(self):
        return _('{number}/{year}: {type}').format(
            number=self.contract_number or ' - ',
            year=self.year,
            type=self.get_contract_type_display())

    def publisher_responds(self):
        return (self.in_communication or
                self.state != constants.CONTRACT_STATE_NEGOTIATION)

    def get_absolute_url(self):
        return reverse('contracts:detail', args=[str(self.id)])

    def is_valid(self):
        """
        Checks that contract is valid and that it did not expire.
        If it is expired self.valid will be set to false.
        """
        if self.state == constants.CONTRACT_STATE_VALID:
            expired = (self.valid_to < datetime.now()
                       if self.valid_to else False)
            if expired:
                self.state = constants.CONTRACT_STATE_EXPIRED
                self.save()
            return not expired
        return False

    def get_style(self):
        """
        Helper class to get css class according to status of the contract
        """
        if self.is_valid():
            return 'success'
        return 'danger'

    @classmethod
    def new_contract_number(cls):
        return cls.objects.filter(year=this_year()).aggregate(
            max_id=models.Max('contract_number')).get('max_id', 0) + 1


@reversion.register(exclude=('last_changed',))
class EmailNegotiation(models.Model):
    """
        This model represents an email that is going to be sent to the
        publisher, its content will be pre-filled with html template.
    """
    contract = models.ForeignKey(Contract)
    sent = models.BooleanField(default=False)

    title = models.CharField(max_length=64)

    scheduled_date = models.DateField(_('When to send this message'))
    content = RichTextField()
    template = models.CharField(max_length=64)

    class Meta:
        ordering = ('scheduled_date', )

{'fraction_non_alphanumeric': 0.09723980754621424, 'fraction_numerical': 0.002785515320334262, 'mean_word_length': 2.520053475935829, 'pattern_counts': {'":': 0, '<': 5, '<?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': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2942456', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2107, 'n_words': 501}