<commit_before>import datetime

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.core.urlresolvers import reverse
from django.db import models
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _

from iris.conf import settings


class TopicManager(models.Manager):

    def with_participant(self, obj):
        content_type = ContentType.objects.get_for_model(obj)
        return self.filter(
            participants__content_type__pk=content_type.id,
            participants__object_id=obj.id,
        )


class Topic(models.Model):
    """A topic of conversation."""

    subject = models.CharField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(blank=True, null=True, db_index=True)

    creator_content_type = models.ForeignKey(ContentType, blank=True, null=True)
    creator_object_id = models.PositiveIntegerField(blank=True, null=True)
    creator = generic.GenericForeignKey("creator_content_type", "creator_object_id")

    objects = TopicManager()

    class Meta:
        ordering = ('modified', )
        permissions = (
            ('view_topic', 'Can view topic(s).'),
            ('join_topic', 'Can participate in topic(s).'),
            ('add_to_topic', 'Can add items to topic(s).'),
        )

    def __unicode__(self):
        return self.subject

    def get_absolute_url(self):
        return reverse('iris_topic_slug', kwargs=dict(
            topic_id=self.id,
            slug=slugify(self.subject),
        ))

    def add_item(self, creator, obj):
        """Add the object as an item, returning a saved item."""
        item = Item(
            topic=self,
            creator=creator,
            content=obj,
        )
        item.save()
        return item

    def add_participant(self, creator, obj):
        """Add the object as a participant, returning the ParticipantJoin item created."""
        participant = Participant(
            topic=self,
            content=obj,
        )
        participant.save()
        joined = ParticipantJoin(
            participant=participant,
        )
        joined.save()
        return self.add_item(creator, joined)

    def get_participant(self, obj):
        """Get participation information for the given object."""
        content_type = ContentType.objects.get_for_model(obj)
        try:
            return Participant.objects.get(
                topic=self,
                content_type__pk=content_type.id,
                object_id=obj.id,
            )
        except Participant.DoesNotExist:
            return None

    def has_participant(self, obj):
        if not isinstance(obj, models.Model):
            return False
        participant = self.get_participant(obj)
        return participant is not None

    def item_last_read_by(self, obj):
        content_type = ContentType.objects.get_for_model(obj)
        participant = Participant.objects.get(
            topic=self,
            content_type__pk=content_type.id,
            object_id=obj.id,
        )
        return participant.item_last_read


class Item(models.Model):
    """An item in a conversation."""

    topic = models.ForeignKey('Topic', related_name='items', db_index=True)
    created = models.DateTimeField(default=datetime.datetime.now, db_index=True) # don't use auto_now_add since we sometimes want to override this on create

    content_type = models.ForeignKey(ContentType, blank=True, null=True, related_name='+')
    object_id = models.PositiveIntegerField(blank=True, null=True)
    content = generic.GenericForeignKey("content_type", "object_id")

    creator_content_type = models.ForeignKey(ContentType, blank=True, null=True, related_name='+')
    creator_object_id = models.PositiveIntegerField(blank=True, null=True)
    creator = generic.GenericForeignKey("creator_content_type", "creator_object_id")
<commit_msg>Add a participants_of_type helper function to Topic model instances.<commit_after>import datetime

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.core.urlresolvers import reverse
from django.db import models
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _

from iris.conf import settings


class TopicManager(models.Manager):

    def with_participant(self, obj):
        content_type = ContentType.objects.get_for_model(obj)
        return self.filter(
            participants__content_type__pk=content_type.id,
            participants__object_id=obj.id,
        )


class Topic(models.Model):
    """A topic of conversation."""

    subject = models.CharField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(blank=True, null=True, db_index=True)

    creator_content_type = models.ForeignKey(ContentType, blank=True, null=True)
    creator_object_id = models.PositiveIntegerField(blank=True, null=True)
    creator = generic.GenericForeignKey("creator_content_type", "creator_object_id")

    objects = TopicManager()

    class Meta:
        ordering = ('modified', )
        permissions = (
            ('view_topic', 'Can view topic(s).'),
            ('join_topic', 'Can participate in topic(s).'),
            ('add_to_topic', 'Can add items to topic(s).'),
        )

    def __unicode__(self):
        return self.subject

    def get_absolute_url(self):
        return reverse('iris_topic_slug', kwargs=dict(
            topic_id=self.id,
            slug=slugify(self.subject),
        ))

    def add_item(self, creator, obj):
        """Add the object as an item, returning a saved item."""
        item = Item(
            topic=self,
            creator=creator,
            content=obj,
        )
        item.save()
        return item

    def add_participant(self, creator, obj):
        """Add the object as a participant, returning the ParticipantJoin item created."""
        participant = Participant(
            topic=self,
            content=obj,
        )
        participant.save()
        joined = ParticipantJoin(
            participant=participant,
        )
        joined.save()
        return self.add_item(creator, joined)

    def get_participant(self, obj):
        """Get participation information for the given object."""
        content_type = ContentType.objects.get_for_model(obj)
        try:
            return Participant.objects.get(
                topic=self,
                content_type=content_type,
                object_id=obj.id,
            )
        except Participant.DoesNotExist:
            return None

    def has_participant(self, obj):
        if not isinstance(obj, models.Model):
            return False
        participant = self.get_participant(obj)
        return participant is not None

    def item_last_read_by(self, obj):
        content_type = ContentType.objects.get_for_model(obj)
        participant = Participant.objects.get(
            topic=self,
            content_type=content_type,
            object_id=obj.id,
        )
        return participant.item_last_read

    def participants_of_type(self, model_class):
        content_type = ContentType.objects.get_for_model(model_class)
        return self.participants.filter(
            content_type=content_type,
        )


class Item(models.Model):
    """An item in a conversation."""

    topic = models.ForeignKey('Topic', related_name='items', db_index=True)
    created = models.DateTimeField(default=datetime.datetime.now, db_index=True) # don't use auto_now_add since we sometimes want to override this on create

    content_type = models.ForeignKey(ContentType, blank=True, null=True, related_name='+')
    object_id = models.PositiveIntegerField(blank=True, null=True)
    content = generic.GenericForeignKey("content_type", "object_id")

    creator_content_type = models.ForeignKey(ContentType, blank=True, null=True, related_name='+')
    creator_object_id = models.PositiveIntegerField(blank=True, null=True)
    creator = generic.GenericForeignKey("creator_content_type", "creator_object_id")

{'fraction_non_alphanumeric': 0.09401190331592373, 'fraction_numerical': 0.0007287744443094862, 'mean_word_length': 2.878473857748469, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '567110', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2282, 'n_words': 508}