<commit_before>from django.conf import settings
from django.contrib.contenttypes.fields import (
    GenericForeignKey, GenericRelation,
)
from django.core.urlresolvers import reverse, reverse_lazy
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext, ugettext_lazy as _

from core.models import BigForeignKey
from core.utils import format_html_lazy


class PrimarySpeaker:
    """A wapper representing the submitter of the proposal as a speaker.

    This class is meant to be compatible with ``AdditionalSpeaker``, and used
    along side with instances of that class.
    """
    def __init__(self, proposal):
        super().__init__()
        self._proposal = proposal
        self._user = proposal.submitter

    def __repr__(self):
        return '<PrimarySpeaker: {name}>'.format(name=self.user.speaker_name)

    def __eq__(self, other):
        return (
            isinstance(other, PrimarySpeaker) and self.user == other.user
            and self.proposal == other.proposal
        )

    @property
    def user(self):
        return self._user

    @property
    def proposal(self):
        return self._proposal

    @property
    def cancelled(self):
        return False

    def get_status_display(self):
        return ugettext('Proposal author')


class AdditionalSpeaker(models.Model):

    user = BigForeignKey(
        to=settings.AUTH_USER_MODEL,
        verbose_name=_('user'),
    )

    proposal_type = models.ForeignKey(
        to='contenttypes.ContentType',
        verbose_name=_('proposal model type'),
    )
    proposal_id = models.BigIntegerField(
        verbose_name=_('proposal ID'),
    )
    proposal = GenericForeignKey('proposal_type', 'proposal_id')

    SPEAKING_STATUS_PENDING = 'pending'
    SPEAKING_STATUS_ACCEPTED = 'accepted'
    SPEAKING_STATUS_DECLINED = 'declined'
    SPEAKING_STATUS = (
        (SPEAKING_STATUS_PENDING,  _('Pending')),
        (SPEAKING_STATUS_ACCEPTED, _('Accepted')),
        (SPEAKING_STATUS_DECLINED, _('Declined')),
    )
    status = models.CharField(
        max_length=8,
        choices=SPEAKING_STATUS,
        default=SPEAKING_STATUS_PENDING,
    )

    cancelled = models.BooleanField(
        verbose_name=_('cancelled'),
        default=False,
        db_index=True,
    )

    class Meta:
        unique_together = ['user', 'proposal_type', 'proposal_id']
        ordering = ['proposal_type', 'proposal_id', 'user__speaker_name']
        verbose_name = _('additional speaker')
        verbose_name_plural = _('additional speakers')

    def __str__(self):
        return '{name} ({status})'.format(
            name=self.user.speaker_name,
            status=self.get_status_display(),
        )


class ProposalQuerySet(models.QuerySet):
    def filter_viewable(self, user):
        return self.filter(
            Q(submitter=user)
            | Q(additionalspeaker_set__in=user.additionalspeaker_set.all())
        )


class AbstractProposal(models.Model):

    submitter = BigForeignKey(
        to=settings.AUTH_USER_MODEL,
        verbose_name=_('submitter'),
    )<commit_msg>Exclude cancelled speaking requests from viewable<commit_after>from django.conf import settings
from django.contrib.contenttypes.fields import (
    GenericForeignKey, GenericRelation,
)
from django.core.urlresolvers import reverse, reverse_lazy
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext, ugettext_lazy as _

from core.models import BigForeignKey
from core.utils import format_html_lazy


class PrimarySpeaker:
    """A wapper representing the submitter of the proposal as a speaker.

    This class is meant to be compatible with ``AdditionalSpeaker``, and used
    along side with instances of that class.
    """
    def __init__(self, proposal):
        super().__init__()
        self._proposal = proposal
        self._user = proposal.submitter

    def __repr__(self):
        return '<PrimarySpeaker: {name}>'.format(name=self.user.speaker_name)

    def __eq__(self, other):
        return (
            isinstance(other, PrimarySpeaker) and self.user == other.user
            and self.proposal == other.proposal
        )

    @property
    def user(self):
        return self._user

    @property
    def proposal(self):
        return self._proposal

    @property
    def cancelled(self):
        return False

    def get_status_display(self):
        return ugettext('Proposal author')


class AdditionalSpeaker(models.Model):

    user = BigForeignKey(
        to=settings.AUTH_USER_MODEL,
        verbose_name=_('user'),
    )

    proposal_type = models.ForeignKey(
        to='contenttypes.ContentType',
        verbose_name=_('proposal model type'),
    )
    proposal_id = models.BigIntegerField(
        verbose_name=_('proposal ID'),
    )
    proposal = GenericForeignKey('proposal_type', 'proposal_id')

    SPEAKING_STATUS_PENDING = 'pending'
    SPEAKING_STATUS_ACCEPTED = 'accepted'
    SPEAKING_STATUS_DECLINED = 'declined'
    SPEAKING_STATUS = (
        (SPEAKING_STATUS_PENDING,  _('Pending')),
        (SPEAKING_STATUS_ACCEPTED, _('Accepted')),
        (SPEAKING_STATUS_DECLINED, _('Declined')),
    )
    status = models.CharField(
        max_length=8,
        choices=SPEAKING_STATUS,
        default=SPEAKING_STATUS_PENDING,
    )

    cancelled = models.BooleanField(
        verbose_name=_('cancelled'),
        default=False,
        db_index=True,
    )

    class Meta:
        unique_together = ['user', 'proposal_type', 'proposal_id']
        ordering = ['proposal_type', 'proposal_id', 'user__speaker_name']
        verbose_name = _('additional speaker')
        verbose_name_plural = _('additional speakers')

    def __str__(self):
        return '{name} ({status})'.format(
            name=self.user.speaker_name,
            status=self.get_status_display(),
        )


class ProposalQuerySet(models.QuerySet):
    def filter_viewable(self, user):
        return self.filter(
            Q(submitter=user)
            | Q(additionalspeaker_set__in=user.cospeaking_info_set.all())
        )


class AbstractProposal(models.Model):

    submitter = BigForeignKey(
        to=settings.AUTH_USER_MODEL,
        verbose_name=_('submitter'),
    )
{'fraction_non_alphanumeric': 0.09777070063694268, 'fraction_numerical': 0.0003184713375796178, 'mean_word_length': 2.990470139771283, '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': '7522649', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1867, 'n_words': 389}