<commit_before>from django.contrib.contenttypes.models import ContentType
from django.db import transaction
from oclapi.models import VERSION_TYPE

__author__ = 'misternando'


class ConceptContainerMixin(object):
    """
    This class abstracts away some of the functionality of resources that contain Concepts.
    ConceptCollections and Sources are 2 concrete examples of ConceptContainers.
    """
    @property
    def num_versions(self):
        return self.get_version_model().objects.filter(versioned_object_id=self.id).count()

    @classmethod
    def persist_new(cls, obj, **kwargs):
        errors = dict()
        parent_resource = kwargs.pop('parent_resource')
        user = kwargs.pop('owner')
        mnemonic = obj.mnemonic
        parent_resource_type = ContentType.objects.get_for_model(parent_resource)
        if cls.objects.filter(parent_type__pk=parent_resource_type.id, parent_id=parent_resource.id, mnemonic=mnemonic).exists():
            errors['mnemonic'] = '%s with mnemonic %s already exists for parent resource %s.' % (cls, mnemonic, parent_resource.mnemonic)
            return errors
        obj.parent = parent_resource
        obj.owner = user
        with transaction.commit_on_success():
            obj.save(**kwargs)
            version_model = cls.get_version_model()
            version = version_model.for_base_object(obj, 'INITIAL')
            version.released = True
            version.save()
        return errors

    @classmethod
    def persist_changes(cls, obj, **kwargs):
        errors = dict()
        parent_resource = kwargs.pop('parent_resource')
        mnemonic = obj.mnemonic
        parent_resource_type = ContentType.objects.get_for_model(parent_resource)
        matching_sources = cls.objects.filter(parent_type__pk=parent_resource_type.id, parent_id=parent_resource.id, mnemonic=mnemonic)<commit_msg>A concept version should always be a Mongo ObjectId<commit_after>from django.contrib.contenttypes.models import ContentType
from django.db import transaction
from oclapi.models import VERSION_TYPE

__author__ = 'misternando'


class ConceptContainerMixin(object):
    """
    This class abstracts away some of the functionality of resources that contain Concepts.
    ConceptCollections and Sources are 2 concrete examples of ConceptContainers.
    """
    @property
    def num_versions(self):
        return self.get_version_model().objects.filter(versioned_object_id=self.id).count()

    @classmethod
    def persist_new(cls, obj, **kwargs):
        errors = dict()
        parent_resource = kwargs.pop('parent_resource')
        user = kwargs.pop('owner')
        mnemonic = obj.mnemonic
        parent_resource_type = ContentType.objects.get_for_model(parent_resource)
        if cls.objects.filter(parent_type__pk=parent_resource_type.id, parent_id=parent_resource.id, mnemonic=mnemonic).exists():
            errors['mnemonic'] = '%s with mnemonic %s already exists for parent resource %s.' % (cls, mnemonic, parent_resource.mnemonic)
            return errors
        obj.parent = parent_resource
        obj.owner = user
        with transaction.commit_on_success():
            obj.save(**kwargs)
            version_model = cls.get_version_model()
            version = version_model.for_base_object(obj, 'INITIAL')
            version.released = True
            version.save()
            version.mnemonic = version.id
            version.save()
        return errors

    @classmethod
    def persist_changes(cls, obj, **kwargs):
        errors = dict()
        parent_resource = kwargs.pop('parent_resource')
        mnemonic = obj.mnemonic
        parent_resource_type = ContentType.objects.get_for_model(parent_resource)
        matching_sources = cls.objects.filter(parent_type__pk=parent_resource_type.id, parent_id=parent_resource.id, mnemonic=mnemonic)
{'fraction_non_alphanumeric': 0.08747721947409529, 'fraction_numerical': 0.000520697734964853, 'mean_word_length': 3.2879464285714284, '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': '5430396', 'n_tokens_mistral': 1096, 'n_tokens_neox': 1069, 'n_words': 234}