<commit_before>from django.db.backends import utils
from django.db import models
from django.apps import apps

from elasticsearch_dsl import field

from six import iteritems


class ElasticSearchForeignKey(object):

    def __init__(self, model):
        self.model = shallow_class_factory(model)
        self.instance = None
        self.name = None

    def set(self, name, data):
        if data:
            self.instance = self.model(**data)
            self.name = name

    def get(self, parent_class):
        return self.instance


class ElasticSearchRelatedManager(object):
    def __init__(self, model):
        self.model = shallow_class_factory(model)
        self.instances = None
        self.name = None

    def set(self, name, data):
        self.instances = [self.model(**item_data) for item_data in data]
        self.name = name

    def get(self, parent_class):
        return self

    def all(self):
        return self.instances

    def count(self):
        return len(self.instances)


class Test:

    def get(self):
        pass

    attr = property(get, set)


def shallow_class_factory(model):
    if model._deferred:
        model = model._meta.proxy_for_model
    name = "{}_ElasticSearchResult".format(model.__name__)
    name = utils.truncate_name(name, 80, 32)
    # try to get the model from the django registry
    try:
        return apps.get_model(model._meta.app_label, name)
    # get the object's type - hopefully a django model
    except LookupError:
        class Meta(object):
            proxy = True
            app_label = model._meta.app_label

        overrides = {
            "save": None,
            "Meta": Meta,
            "__module__": model.__module__,
            "_deferred": True,
        }

        for attname, es_field in iteritems(model.search_objects.mapping.properties._params["properties"]):
            if type(es_field) == field.Nested:
                # This is a nested object!
                dj_field = model._meta.get_field(attname)

                if isinstance(dj_field, models.ManyToManyField):
                    mock_fkey = ElasticSearchRelatedManager(dj_field.rel.to)
                    overrides[attname] = property(mock_fkey.get, mock_fkey.set)

                if isinstance(dj_field, models.ForeignKey):
                    # Let's add a fake foreignkey attribute
                    mock_fkey = ElasticSearchForeignKey(dj_field.rel.to)
                    overrides[attname] = property(mock_fkey.get, mock_fkey.set)

        return type(str(name), (model,), overrides)
<commit_msg>OH HELL YEAH BUDDY<commit_after>from django.db.backends import utils
from django.db import models
from django.apps import apps

from elasticsearch_dsl import field

from six import iteritems


class ElasticSearchForeignKey(object):

    def __init__(self, field_name, model):
        self.model = shallow_class_factory(model)
        self.field_name = field_name

    def __get__(self, instance, objtype=None):
        return instance.__dict__.get(self.field_name)

    def __set__(self, instance, value):
        instance.__dict__[self.field_name] = self.model(**value)


class ElasticSearchRelatedManager(object):

    def __init__(self, instances):
        self.instances = instances

    def count(self):
        return len(self.instances)

    def all(self):
        return self.instances


class ElasticSearchManyField(object):

    def __init__(self, field_name, model):
        self.model = shallow_class_factory(model)
        self.field_name = field_name

    def __get__(self, instance, objtype=None):
        if instance is None:
            return property()
        return instance.__dict__.get(self.field_name, ElasticSearchRelatedManager([]))

    def __set__(self, instance, value):
        instance.__dict__[self.field_name] = ElasticSearchRelatedManager([self.model(**item_data) for item_data in value])


class Test:

    def get(self):
        pass

    attr = property(get, set)


def shallow_class_factory(model):
    if model._deferred:
        model = model._meta.proxy_for_model
    name = "{}_ElasticSearchResult".format(model.__name__)
    name = utils.truncate_name(name, 80, 32)
    # try to get the model from the django registry
    try:
        return apps.get_model(model._meta.app_label, name)
    # get the object's type - hopefully a django model
    except LookupError:
        class Meta(object):
            proxy = True
            app_label = model._meta.app_label

        overrides = {
            "save": None,
            "Meta": Meta,
            "__module__": model.__module__,
            "_deferred": True,
        }

        for attname, es_field in iteritems(model.search_objects.mapping.properties._params["properties"]):
            if type(es_field) == field.Nested:
                # This is a nested object!
                dj_field = model._meta.get_field(attname)

                if isinstance(dj_field, models.ManyToManyField):
                    overrides[attname] = ElasticSearchManyField(attname, dj_field.rel.to)

                if isinstance(dj_field, models.ForeignKey):
                    # Let's add a fake foreignkey attribute
                    overrides[attname] = ElasticSearchForeignKey(attname, dj_field.rel.to)

        return type(str(name), (model,), overrides)

{'fraction_non_alphanumeric': 0.0900883957118676, 'fraction_numerical': 0.0015046078615760768, 'mean_word_length': 2.4644951140065148, 'pattern_counts': {'":': 8, '<': 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': '6910217', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1495, 'n_words': 369}