<commit_before>import warnings

from cached_property import cached_property

# Create your models here.
from django.db.models import Prefetch
from django_extensions.db.models import TimeStampedModel
from django.utils.translation import ugettext_lazy as _

from django.db import models
from feder.cases.models import Case
from feder.records.registry import record_type_registry
from feder.cases.models import enforce_quarantined_queryset


class RecordQuerySet(models.QuerySet):
    def with_select_related_content(self):
        """
        Returns data using joins. Up to one query.
        :return: models.QuerySet
        """
        fields = [
            field.name
            for field in Record._meta.related_objects
            if issubclass(field.related_model, AbstractRecord)
        ]
        return self.select_related(*fields)

    def with_prefetch_related_content(self):
        """
        Returns data using prefetch. As many queries as different data types.
        :return:
        """
        fields = [
            field.related_name
            for field in Record._meta.related_objects
            if issubclass(field.related_model, AbstractRecord)
        ]
        return self.prefetch_related(*fields)

    def with_letter_prefetched(self, queryset=None):
        from feder.letters.models import Letter

        if not queryset:
            queryset = Letter.objects.all()
        return self.prefetch_related(
            Prefetch(lookup="letters_letter_related", queryset=queryset)<commit_msg>Avoid query evaluation in with_letter_prefetched<commit_after>import warnings

from cached_property import cached_property

# Create your models here.
from django.db.models import Prefetch
from django_extensions.db.models import TimeStampedModel
from django.utils.translation import ugettext_lazy as _

from django.db import models
from feder.cases.models import Case
from feder.records.registry import record_type_registry
from feder.cases.models import enforce_quarantined_queryset


class RecordQuerySet(models.QuerySet):
    def with_select_related_content(self):
        """
        Returns data using joins. Up to one query.
        :return: models.QuerySet
        """
        fields = [
            field.name
            for field in Record._meta.related_objects
            if issubclass(field.related_model, AbstractRecord)
        ]
        return self.select_related(*fields)

    def with_prefetch_related_content(self):
        """
        Returns data using prefetch. As many queries as different data types.
        :return:
        """
        fields = [
            field.related_name
            for field in Record._meta.related_objects
            if issubclass(field.related_model, AbstractRecord)
        ]
        return self.prefetch_related(*fields)

    def with_letter_prefetched(self, queryset=None):
        from feder.letters.models import Letter

        if queryset is None:
            queryset = Letter.objects.all()
        return self.prefetch_related(
            Prefetch(lookup="letters_letter_related", queryset=queryset)
{'fraction_non_alphanumeric': 0.0641399416909621, 'fraction_numerical': 0.0, 'mean_word_length': 2.7936117936117935, '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': '2229657', 'n_tokens_mistral': 811, 'n_tokens_neox': 789, 'n_words': 214}