<commit_before>import itertools

from django.db import models
from django.dispatch import receiver
from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

from talks.events.models import Event, EventGroup


DEFAULT_COLLECTION_NAME = "My Collection"


class TalksUser(models.Model):

    user = models.OneToOneField(User)

    @property
    def default_collection(self):
        return self.get_or_create_default_collection()

    def get_or_create_default_collection(self):
        try:
            return CollectionFollow.objects.get(user=self,
                                                is_owner=True,
                                                is_main=True).collection
        except CollectionFollow.DoesNotExist:
            default_collection = Collection.objects.create(
                title=DEFAULT_COLLECTION_NAME)
            # Link the collection to the user
            CollectionFollow.objects.create(user=self,
                                            collection=default_collection,
                                            is_owner=True,
                                            is_main=True)
            return default_collection

    def __unicode__(self):
        return unicode(self.user)


class Collection(models.Model):

    slug = models.SlugField()
    title = models.CharField(max_length=250)

    # TODO list private or public/shared?
    # TODO qualify list? (e.g. "Talks I want to attend"?)

    def _get_items_by_model(self, model):
        """Used when selecting a particular type (specified in the `model` arg)
        of objects from our Collection.

            1) Get the ContentType for that `model`
            2) Filter to the CollectionItems of that ContentType and get all
               `object_id`s
            3) Select these `object_id`s from the `model`
        """
        content_type = ContentType.objects.get_for_model(Event)
        ids = self.collectionitem_set.filter(content_type=content_type
                                             ).values_list('object_id')
        return model.objects.filter(id__in=itertools.chain.from_iterable(ids))

    class ItemAlreadyInCollection(Exception):
        pass

    class InvalidItemType(Exception):
        pass

    def add_item(self, item):
        if isinstance(item, Event):
            # Adding an event
            content_type = ContentType.objects.get_for_model(Event)
        elif isinstance(item, EventGroup):
            # Adding event group
            content_type = ContentType.objects.get_for_model(EventGroup)
        else:
            raise self.InvalidItemType()
        try:
            self.collectionitem_set.get(content_type=content_type,
                                        object_id=item.id)
            raise self.ItemAlreadyInCollection()
        except CollectionItem.DoesNotExist:
            item = self.collectionitem_set.create(item=item)
            return item

    def get_events(self):
        return self._get_items_by_model(Event)

    def get_event_groups(self):
        return self._get_items_by_model(EventGroup)

    def __unicode__(self):
        return self.title


class CollectionFollow(models.Model):
    """User following a Collection
    """

    user = models.ForeignKey(TalksUser)
    collection = models.ForeignKey(Collection)
    is_owner = models.BooleanField(default=False)
    # Main/default collection of the user
    is_main = models.BooleanField(default=False)<commit_msg>Added remove_item on Collection model<commit_after>import itertools

from django.db import models
from django.dispatch import receiver
from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

from talks.events.models import Event, EventGroup


DEFAULT_COLLECTION_NAME = "My Collection"


class TalksUser(models.Model):

    user = models.OneToOneField(User)

    @property
    def default_collection(self):
        return self.get_or_create_default_collection()

    def get_or_create_default_collection(self):
        try:
            return CollectionFollow.objects.get(user=self,
                                                is_owner=True,
                                                is_main=True).collection
        except CollectionFollow.DoesNotExist:
            default_collection = Collection.objects.create(
                title=DEFAULT_COLLECTION_NAME)
            # Link the collection to the user
            CollectionFollow.objects.create(user=self,
                                            collection=default_collection,
                                            is_owner=True,
                                            is_main=True)
            return default_collection

    def __unicode__(self):
        return unicode(self.user)


class Collection(models.Model):

    slug = models.SlugField()
    title = models.CharField(max_length=250)

    # TODO list private or public/shared?
    # TODO qualify list? (e.g. "Talks I want to attend"?)

    def _get_items_by_model(self, model):
        """Used when selecting a particular type (specified in the `model` arg)
        of objects from our Collection.

            1) Get the ContentType for that `model`
            2) Filter to the CollectionItems of that ContentType and get all
               `object_id`s
            3) Select these `object_id`s from the `model`
        """
        content_type = ContentType.objects.get_for_model(model)
        ids = self.collectionitem_set.filter(content_type=content_type
                                             ).values_list('object_id')
        return model.objects.filter(id__in=itertools.chain.from_iterable(ids))

    class ItemAlreadyInCollection(Exception):
        pass

    class InvalidItemType(Exception):
        pass

    def add_item(self, item):
        if isinstance(item, Event):
            # Adding an event
            content_type = ContentType.objects.get_for_model(Event)
        elif isinstance(item, EventGroup):
            # Adding event group
            content_type = ContentType.objects.get_for_model(EventGroup)
        else:
            raise self.InvalidItemType()
        try:
            self.collectionitem_set.get(content_type=content_type,
                                        object_id=item.id)
            raise self.ItemAlreadyInCollection()
        except CollectionItem.DoesNotExist:
            item = self.collectionitem_set.create(item=item)
            return item

    def remove_item(self, item):
        if isinstance(item, Event):
            content_type = ContentType.objects.get_for_model(Event)
        elif isinstance(item, EventGroup):
            content_type = ContentType.objects.get_for_model(EventGroup)
        else:
            raise self.InvalidItemType()
        try:
            item = self.collectionitem_set.get(content_type=content_type,
                                               object_id=item.id)
            item.delete()
            return True
        except CollectionItem.DoesNotExist:
            return False

    def get_events(self):
        return self._get_items_by_model(Event)

    def get_event_groups(self):
        return self._get_items_by_model(EventGroup)

    def __unicode__(self):
        return self.title


class CollectionFollow(models.Model):
    """User following a Collection
    """

    user = models.ForeignKey(TalksUser)
    collection = models.ForeignKey(Collection)
    is_owner = models.BooleanField(default=False)
    # Main/default collection of the user
    is_main = models.BooleanField(default=False)
{'fraction_non_alphanumeric': 0.07023670935195964, 'fraction_numerical': 0.0015521924718665113, 'mean_word_length': 2.1494908350305497, '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': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1641569', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1898, 'n_words': 448}