<commit_before>import datetime
from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.template import RequestContext
from django.template.loader import render_to_string
from django.template import TemplateDoesNotExist
from django.utils.safestring import mark_safe

from .managers import WellManager

import itertools


class WellType(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField()

    def __unicode__(self):
        return self.title


class Well(models.Model):
    type = models.ForeignKey(WellType)
    pub_date = models.DateTimeField()
    expires = models.DateTimeField(null=True, blank=True)
    active = models.BooleanField(default=True)

    objects = WellManager()

    def __init__(self, *args, **kwargs):
        super(Well, self).__init__(*args, **kwargs)
        self.queryset = None
        self.well_content = []
        self.exclude_ids = []

    @property
    def title(self):
        return self.type.title

    def __unicode__(self):
        return "%s (%s - %s)" % (self.type, self.pub_date,
                                 self.expires or "Never")

    def save(self, *args, **kwargs):
        if not self.pub_date:
            self.pub_date = datetime.datetime.now()
        return super(Well, self).save(*args, **kwargs)

    def merge_with(self, queryset):
        self.queryset = queryset
        self.well_content = []
        self.exclude_ids = []
        return self

    def render(self, request=None, parent=None):
        if parent is None:
            parent = self
        kwargs = {'parent': parent,
                  'request': request}
        return mark_safe(''.join(n.render(**kwargs) for n in self))

    def initialize(self):
        if self.well_content:
            return

        def process_well(well, managers, ordering):
            for node in well.nodes.all().select_related():
                i = len(ordering)
                model_class = node.content_type.model_class()
                # we recurse into any child wells to flatten everything with
                # the minimum number of queries
                if model_class == Well:
                    managers, ordering = process_well(node.content_object,
                            managers, ordering)
                    continue
                key = "%s.%s" % (model_class.__module__, node.content_type.model)
                if not key in managers:
                    managers[key] = {
                            "name": node.content_type.model,
                            "manager": model_class.objects,
                            "object_ids": [],
                    }
                node_key = "%s.%i" % (node.content_type.model, node.object_id)
                ordering[node_key] = i, well
                managers[key]["object_ids"].append(node.object_id)
                if self.queryset is not None and self.queryset.model is model_class:
                    self.exclude_ids.append(node.object_id)
            return managers, ordering

        # well_managers {'module.model':name, manager, ids}
        self.exclude_ids = []
        well_managers, ordering = process_well(self, {}, {})
        self.well_content = [None] * len(ordering)
<commit_msg>use _default_manager so that custom managers don't break us<commit_after>import datetime
from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.template import RequestContext
from django.template.loader import render_to_string
from django.template import TemplateDoesNotExist
from django.utils.safestring import mark_safe

from .managers import WellManager

import itertools


class WellType(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField()

    def __unicode__(self):
        return self.title


class Well(models.Model):
    type = models.ForeignKey(WellType)
    pub_date = models.DateTimeField()
    expires = models.DateTimeField(null=True, blank=True)
    active = models.BooleanField(default=True)

    objects = WellManager()

    def __init__(self, *args, **kwargs):
        super(Well, self).__init__(*args, **kwargs)
        self.queryset = None
        self.well_content = []
        self.exclude_ids = []

    @property
    def title(self):
        return self.type.title

    def __unicode__(self):
        return "%s (%s - %s)" % (self.type, self.pub_date,
                                 self.expires or "Never")

    def save(self, *args, **kwargs):
        if not self.pub_date:
            self.pub_date = datetime.datetime.now()
        return super(Well, self).save(*args, **kwargs)

    def merge_with(self, queryset):
        self.queryset = queryset
        self.well_content = []
        self.exclude_ids = []
        return self

    def render(self, request=None, parent=None):
        if parent is None:
            parent = self
        kwargs = {'parent': parent,
                  'request': request}
        return mark_safe(''.join(n.render(**kwargs) for n in self))

    def initialize(self):
        if self.well_content:
            return

        def process_well(well, managers, ordering):
            for node in well.nodes.all().select_related():
                i = len(ordering)
                model_class = node.content_type.model_class()
                # we recurse into any child wells to flatten everything with
                # the minimum number of queries
                if model_class == Well:
                    managers, ordering = process_well(node.content_object,
                            managers, ordering)
                    continue
                key = "%s.%s" % (model_class.__module__, node.content_type.model)
                if not key in managers:
                    managers[key] = {
                            "name": node.content_type.model,
                            # _default_manager is undocumented. If django ever
                            # changes/documents a way to get the default
                            # manager, this will need to change too
                            "manager": model_class._default_manager,
                            "object_ids": [],
                    }
                node_key = "%s.%i" % (node.content_type.model, node.object_id)
                ordering[node_key] = i, well
                managers[key]["object_ids"].append(node.object_id)
                if self.queryset is not None and self.queryset.model is model_class:
                    self.exclude_ids.append(node.object_id)
            return managers, ordering

        # well_managers {'module.model':name, manager, ids}
        self.exclude_ids = []
        well_managers, ordering = process_well(self, {}, {})
        self.well_content = [None] * len(ordering)

{'fraction_non_alphanumeric': 0.09570001447806573, 'fraction_numerical': 0.000868683943825105, 'mean_word_length': 2.008710801393728, 'pattern_counts': {'":': 6, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1060560', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1803, 'n_words': 462}