<commit_before>from django.contrib.contenttypes.generic import GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db.models.query import QuerySet

from armstrong.core.arm_wells.querysets import GenericForeignKeyQuerySet

from .models import RelatedContent
from .managers import RelatedContentManager

class BoundRelatedContentAccessor(object):
    def __init__(self, instance, model, from_field, to_field, *args, **kwargs):
        self.instance = instance
        self.model = model
        self.from_field = from_field
        self.to_field = to_field
        self.params = None

    def get_query_set_params(self):
        if self.params:
            return self.params
        for field in self.model._meta.virtual_fields:
            if field.name == self.from_field:
                self.params = {
                    field.ct_field: ContentType.objects.get_for_model(self.instance).pk,
                    field.fk_field: self.instance.pk
                }
                break
        else:
            self.params = {self.from_field: self.instance}
        return self.params

    def get_query_set(self):
        qs = QuerySet(self.model)
        return qs.filter(**self.get_query_set_params())

    def by_type(self, type):
        qs = self.get_query_set().filter(related_type__title=type)
        return GenericForeignKeyQuerySet(qs, gfk=self.to_field)

    def __getitem__(self, index):
        """
        See ``RelatedContentQuerySet.__getitem__``
        """
        if isinstance(index, basestring):
            return self.by_type(index)
        else:
            raise IndexError("RelatedFields can only be filtered by related_type title")


class NonAssignableError(RuntimeError):
    pass


class RelatedM2MDescriptor(object):
    def __init__(self, model=RelatedContent, from_field="source_object", to_field="destination_object"):<commit_msg>add an all method to the RelatedObjectsField<commit_after>from django.contrib.contenttypes.generic import GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db.models.query import QuerySet

from armstrong.core.arm_wells.querysets import GenericForeignKeyQuerySet

from .models import RelatedContent
from .managers import RelatedContentManager

class BoundRelatedContentAccessor(object):
    def __init__(self, instance, model, from_field, to_field, *args, **kwargs):
        self.instance = instance
        self.model = model
        self.from_field = from_field
        self.to_field = to_field
        self.params = None

    def get_query_set_params(self):
        if self.params:
            return self.params
        for field in self.model._meta.virtual_fields:
            if field.name == self.from_field:
                self.params = {
                    field.ct_field: ContentType.objects.get_for_model(self.instance).pk,
                    field.fk_field: self.instance.pk
                }
                break
        else:
            self.params = {self.from_field: self.instance}
        return self.params

    def get_query_set(self):
        qs = QuerySet(self.model)
        return qs.filter(**self.get_query_set_params())

    def all(self):
        qs = self.get_query_set()
        return GenericForeignKeyQuerySet(qs, gfk=self.to_field)

    def by_type(self, type):
        qs = self.get_query_set().filter(related_type__title=type)
        return GenericForeignKeyQuerySet(qs, gfk=self.to_field)

    def __getitem__(self, index):
        """
        See ``RelatedContentQuerySet.__getitem__``
        """
        if isinstance(index, basestring):
            return self.by_type(index)
        else:
            raise IndexError("RelatedFields can only be filtered by related_type title")


class NonAssignableError(RuntimeError):
    pass


class RelatedM2MDescriptor(object):
    def __init__(self, model=RelatedContent, from_field="source_object", to_field="destination_object"):
{'fraction_non_alphanumeric': 0.08445006321112516, 'fraction_numerical': 0.0005056890012642225, 'mean_word_length': 2.7820267686424476, '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': '5923473', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1088, 'n_words': 238}