<commit_before>from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from mptt.forms import TreeNodeChoiceField
from mptt.forms import TreeNodeMultipleChoiceField
from mptt.models import MPTTModel

from .models import Section

EMPTY_LABEL = u"---------"


class SectionTreeAdminMixin(object):
    """
    Provides a mixin for dealing with tree display of Section <select>s in
    admin forms
    """

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if issubclass(db_field.rel.to, MPTTModel):
            required = db_field.formfield().required
            return TreeNodeChoiceField(
                queryset=db_field.rel.to.objects.all(),
                required=required, empty_label=EMPTY_LABEL)
        return super(SectionTreeAdminMixin, self)\
            .formfield_for_foreignkey(db_field, request, **kwargs)

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if issubclass(db_field.rel.to, MPTTModel):
            required = db_field.formfield().required
            return TreeNodeMultipleChoiceField(
                empty_label=EMPTY_LABEL,
                queryset=db_field.rel.to.objects.all(),
                required=required)
        return super(SectionTreeAdminMixin, self)\
            .formfield_for_manytomany(db_field, request, **kwargs)


class SectionAdmin(MPTTModelAdmin):
    exclude = ("full_slug", )
    search_fields = ('title', 'summary')


admin.site.register(Section, SectionAdmin)
<commit_msg>Deprecate the `SectionTreeAdminMixin`. It's better `mptt` practice to use TreeForeignKey/TreeOneToOneField/TreeManyToManyField explicitly in the model definition.<commit_after>from django.contrib import admin
from mptt.admin import MPTTModelAdmin

from .models import Section


class SectionAdmin(MPTTModelAdmin):
    exclude = ("full_slug", )
    search_fields = ('title', 'summary')


admin.site.register(Section, SectionAdmin)


# DEPRECATED: To be removed in ArmSections 2.0
# (This also should have been in a `forms.py` file and renamed to
# `SectionTreeFormMixin` as it's not limited to Admin Forms.)
import warnings
from mptt.forms import TreeNodeChoiceField, TreeNodeMultipleChoiceField
from mptt.models import MPTTModel


class SectionTreeAdminMixin(object):
    """Form mixin for the tree display of Section <select>s"""

    def __init__(self, *args, **kwargs):
        msg = ("SectionTreeAdminMixin is deprecated and will be removed in "
               "ArmSections 2.0. Explicitly use mptt's TreeForeignKey/"
               "TreeOneToOneField/TreeManyToManyField in your model "
               "definitions.")
        warnings.warn(msg, DeprecationWarning, stacklevel=2)
        super(SectionTreeAdminMixin, self).__init__(*args, **kwargs)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if issubclass(db_field.rel.to, MPTTModel):
            required = db_field.formfield().required
            return TreeNodeChoiceField(
                queryset=db_field.rel.to.objects.all(),
                required=required)
        return super(SectionTreeAdminMixin, self)\
            .formfield_for_foreignkey(db_field, request, **kwargs)

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if issubclass(db_field.rel.to, MPTTModel):
            required = db_field.formfield().required
            return TreeNodeMultipleChoiceField(
                queryset=db_field.rel.to.objects.all(),
                required=required)
        return super(SectionTreeAdminMixin, self)\
            .formfield_for_manytomany(db_field, request, **kwargs)

{'fraction_non_alphanumeric': 0.09085872576177285, 'fraction_numerical': 0.0013850415512465374, 'mean_word_length': 3.278436018957346, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4721930', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1019, 'n_words': 247}