<commit_before>import os

from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext as _
from django.utils.text import Truncator, slugify


class TimestampedModel(models.Model):
    created_on = models.DateTimeField(_('created on'), auto_now_add=True)
    modified_on = models.DateTimeField(_('modified on'), auto_now=True)

    class Meta:
        abstract = True


class Category(models.Model):
    title = models.CharField(_('title'), max_length=100)
    parent = models.ForeignKey('self', blank=True, null=True)
    slug = models.SlugField()

    class Meta:
        verbose_name = u'category'
        verbose_name_plural = u'categories'

    @property
    def full_title(self):
        """
            Returns a '>' separated list of the current category's parents
            title + the current category's title.
        """
        parents = [category.title for category in self.parents()]

        if parents:
            return u"{parents} > {title}".format(
                parents=" > ".join(parents),
                title=self.title,
            )
        else:
            return self.title

    def __unicode__(self):
        return self.full_title

    def _generate_slug(self):
        return "/".join(
            [slugify(parent.title) for parent in self.parents()] + [slugify(self.title)]
        ).lower()

    def parents(self):
        """ Returns a list of all the current category's parents."""
        parents = []

        if self.parent == None:
            return []

        category = self
        while category.parent != None:
            parents.append(category.parent)
            category = category.parent

        return parents

    def is_root(self):
        """ Returns True if this category has no parent. """
        return self.parent == None

    def root_parent(self, category=None):
        """ Gets the topmost parent of the current category. """
        if not category:
            category = self

        if category.is_root():
            return category
        else:
            return self.root_parent(category.parent)


class PostQuerySet(models.query.QuerySet):
    def in_category(self, category_slug):
        return self.filter(category__slug=category_slug)

    def recent(self, limit=None):
        queryset = self.all()
        if limit:
            queryset = queryset[:limit]

        return queryset

    def random(self, limit=None):
        queryset = self.recent().order_by('?')
        if limit:
            queryset = queryset[:limit]

        return queryset


class PostManager(models.Manager):
    def get_query_set(self):
        return PostQuerySet(Post)

    def __getattr__(self, attr, *args):
        try:<commit_msg>Refactors Category methods.<commit_after>import os

from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext as _
from django.utils.text import Truncator, slugify


class TimestampedModel(models.Model):
    created_on = models.DateTimeField(_('created on'), auto_now_add=True)
    modified_on = models.DateTimeField(_('modified on'), auto_now=True)

    class Meta:
        abstract = True


class Category(models.Model):
    title = models.CharField(_('title'), max_length=100)
    parent = models.ForeignKey('self', blank=True, null=True)
    slug = models.SlugField()

    class Meta:
        verbose_name = u'category'
        verbose_name_plural = u'categories'

    def save(self, *args, **kwargs):
        self.slug = self._generate_slug()
        super(Category, self).save(*args, **kwargs)

    def __unicode__(self):
        return " > ".join([category.title for category in self.hierarchy()])

    def _generate_slug(self):
        return "/".join([slugify(category.title) for category in self.hierarchy()]).lower()

    @property
    def is_root(self):
        """ Returns True if this category has no parent. """
        return self.parent == None

    def parents(self):
        """ Returns a list of all the current category's parents."""
        parents = []

        if self.parent == None:
            return []

        category = self
        while category.parent != None:
            parents.append(category.parent)
            category = category.parent

        return parents[::-1]

    def hierarchy(self):
        return self.parents() + [self]

    def root_parent(self, category=None):
        """ Returns the topmost parent of the current category. """
        return filter(lambda c: c.is_root, self.hierarchy())[0]


class PostQuerySet(models.query.QuerySet):
    def in_category(self, category_slug):
        return self.filter(category__slug=category_slug)

    def recent(self, limit=None):
        queryset = self.all()
        if limit:
            queryset = queryset[:limit]

        return queryset

    def random(self, limit=None):
        queryset = self.recent().order_by('?')
        if limit:
            queryset = queryset[:limit]

        return queryset


class PostManager(models.Manager):
    def get_query_set(self):
        return PostQuerySet(Post)

    def __getattr__(self, attr, *args):
        try:
{'fraction_non_alphanumeric': 0.10757049053688683, 'fraction_numerical': 0.001544998068752414, 'mean_word_length': 2.4946018893387314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6091859', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1415, 'n_words': 380}