<commit_before>import logging
logger = logging.getLogger(__name__)

from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
from django.conf import settings
from django.db import models
from django.db.models.signals import post_save
from django.utils.encoding import python_2_unicode_compatible


from markitup.fields import MarkupField
from wafer.menu import MenuError, refresh_menu_cache


@python_2_unicode_compatible
class File(models.Model):
    """A file for use in page markup."""
    name = models.CharField(max_length=255)
    description = models.TextField()
    item = models.FileField(upload_to='pages_files')

    def __str__(self):
        return u'%s' % (self.name,)


@python_2_unicode_compatible
class Page(models.Model):
    """An extra page for the site."""
    name = models.CharField(max_length=255)
    slug = models.SlugField(help_text=_("Last component of the page URL"))
    parent = models.ForeignKey('self', null=True, blank=True)
    content = MarkupField(
        help_text=_("Markdown contents for the page."))
    include_in_menu = models.BooleanField(
        help_text=_("Whether to include in menus."),
        default=False)
    exclude_from_static = models.BooleanField(
        help_text=_("Whether to exclude this page from the static version of"
                    " the site (Container pages, etc.)"),
        default=False)
    files = models.ManyToManyField(
        File, related_name="pages", blank=True,
        help_text=_("Images and other files for use in"
                    " the content markdown field."))

    people = models.ManyToManyField(settings.AUTH_USER_MODEL,
        related_name='pages', blank=True,
        help_text=_("People associated with this page for display in the"
                    " schedule (Session chairs, panelists, etc.)"))

    def __str__(self):
        return u'%s' % (self.name,)

    def get_path(self):
        path, parent = [self.slug], self.parent
        while parent is not None:
            path.insert(0, parent.slug)
            parent = parent.parent
        return path

    def get_absolute_url(self):
        url = "/".join(self.get_path())
        return reverse('wafer_page', args=(url,))

    get_absolute_url.short_description = 'page url'

    def get_in_schedule(self):
        if self.scheduleitem_set.all():
            return True
        return False

    def get_people_display_names(self):
        names = [person.userprofile.display_name()
                 for person in self.people.all()]
        if len(names) > 2:
            comma_names = ', '.join(names[:-1])
            return comma_names + ' and ' + names[-1]
        else:
            return ' and '.join(names)

    get_in_schedule.short_description = 'Added to schedule'
    get_in_schedule.boolean = True

    get_people_display_names.short_description = 'People'

    class Model:
        unique_together = (('parent', 'slug'),)

    def validate_unique(self, exclude=None):
        existing = Page.objects.filter(slug=self.slug, parent=self.parent)
        # We could be updating the page, so don't fail if the existing
        # entry is this page.
        if existing.count() > 1 or (existing.count() == 1 and
                                    existing.first().pk != self.pk):
            raise ValidationError(
                {
                    NON_FIELD_ERRORS: [
                        _("Duplicate parent/slug combination"),
                    ],
                })
        return super(Page, self).validate_unique(exclude)


def page_menus(root_menu):
    """Add page menus."""
    for page in Page.objects.filter(include_in_menu=True):
        path = page.get_path()
        menu = path[0] if len(path) > 1 else None
        try:
            root_menu.add_item(page.name, page.get_absolute_url(), menu=menu)<commit_msg>Add a circular reference check while we're here<commit_after>import logging
logger = logging.getLogger(__name__)

from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
from django.conf import settings
from django.db import models
from django.db.models.signals import post_save
from django.utils.encoding import python_2_unicode_compatible


from markitup.fields import MarkupField
from wafer.menu import MenuError, refresh_menu_cache


@python_2_unicode_compatible
class File(models.Model):
    """A file for use in page markup."""
    name = models.CharField(max_length=255)
    description = models.TextField()
    item = models.FileField(upload_to='pages_files')

    def __str__(self):
        return u'%s' % (self.name,)


@python_2_unicode_compatible
class Page(models.Model):
    """An extra page for the site."""
    name = models.CharField(max_length=255)
    slug = models.SlugField(help_text=_("Last component of the page URL"))
    parent = models.ForeignKey('self', null=True, blank=True)
    content = MarkupField(
        help_text=_("Markdown contents for the page."))
    include_in_menu = models.BooleanField(
        help_text=_("Whether to include in menus."),
        default=False)
    exclude_from_static = models.BooleanField(
        help_text=_("Whether to exclude this page from the static version of"
                    " the site (Container pages, etc.)"),
        default=False)
    files = models.ManyToManyField(
        File, related_name="pages", blank=True,
        help_text=_("Images and other files for use in"
                    " the content markdown field."))

    people = models.ManyToManyField(settings.AUTH_USER_MODEL,
        related_name='pages', blank=True,
        help_text=_("People associated with this page for display in the"
                    " schedule (Session chairs, panelists, etc.)"))

    def __str__(self):
        return u'%s' % (self.name,)

    def get_path(self):
        path, parent = [self.slug], self.parent
        while parent is not None:
            path.insert(0, parent.slug)
            parent = parent.parent
        return path

    def get_absolute_url(self):
        url = "/".join(self.get_path())
        return reverse('wafer_page', args=(url,))

    get_absolute_url.short_description = 'page url'

    def get_in_schedule(self):
        if self.scheduleitem_set.all():
            return True
        return False

    def get_people_display_names(self):
        names = [person.userprofile.display_name()
                 for person in self.people.all()]
        if len(names) > 2:
            comma_names = ', '.join(names[:-1])
            return comma_names + ' and ' + names[-1]
        else:
            return ' and '.join(names)

    get_in_schedule.short_description = 'Added to schedule'
    get_in_schedule.boolean = True

    get_people_display_names.short_description = 'People'

    class Model:
        unique_together = (('parent', 'slug'),)

    def clean(self):
        keys = [self.pk]
        parent = self.parent
        while parent is not None:
            if parent.pk in keys:
                raise ValidationError(
                    {
                        NON_FIELD_ERRORS: [
                            _("Circular reference in parent."),
                        ],
                    })
            keys.append(parent.pk)
            parent = parent.parent
        return super(Page, self).clean()

    def validate_unique(self, exclude=None):
        existing = Page.objects.filter(slug=self.slug, parent=self.parent)
        # We could be updating the page, so don't fail if the existing
        # entry is this page.
        if existing.count() > 1 or (existing.count() == 1 and
                                    existing.first().pk != self.pk):
            raise ValidationError(
                {
                    NON_FIELD_ERRORS: [
                        _("Duplicate parent/slug combination."),
                    ],
                })
        return super(Page, self).validate_unique(exclude)


def page_menus(root_menu):
    """Add page menus."""
    for page in Page.objects.filter(include_in_menu=True):
        path = page.get_path()
        menu = path[0] if len(path) > 1 else None
        try:
            root_menu.add_item(page.name, page.get_absolute_url(), menu=menu)
{'fraction_non_alphanumeric': 0.10118406889128095, 'fraction_numerical': 0.004066499222581031, 'mean_word_length': 2.4340862422997946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5965049', 'n_tokens_mistral': 2402, 'n_tokens_neox': 2313, 'n_words': 612}