<commit_before>from django import forms
from django.contrib import admin
from django.contrib.admin import SimpleListFilter
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.db.models import Count

import utils
from models import (Group, GroupAlias,
                    Language, LanguageAlias,
                    Skill, SkillAlias)


class EmptyGroupFilter(SimpleListFilter):
    title = 'empty_group'
    parameter_name = 'empty_group'

    def lookups(self, request, model_admin):
        return (('False', 'Empty'),
                ('True', 'Not empty'))

    def queryset(self, request, queryset):
        if self.value() is None:
            return queryset
        value = self.value() == 'True'
        queryset = (queryset.annotate(no_profiles=Count('userprofile')))
        if value:
            return queryset.filter(no_profiles__gt=0)
        return queryset.filter(no_profiles=0)


class CurratedGroupFilter(SimpleListFilter):
    """Admin filter for curated groups."""
    title = 'curated'
    parameter_name = 'curated'

    def lookups(self, request, model_admin):
        return (('False', 'Curated'),
                ('True', 'Not curated'))

    def queryset(self, request, queryset):
        if self.value() is None:
            return queryset
        value = self.value() == 'True'
        return queryset.filter(steward__isnull=value)


class GroupBaseEditAdminForm(forms.ModelForm):
    merge_with_groups = forms.ModelMultipleChoiceField(
        required=False, queryset = None,
        widget=FilteredSelectMultiple('Merge', False))

    def __init__(self, *args, **kwargs):
        queryset = self._meta.model.objects.exclude(pk=kwargs['instance'].id)
        self.base_fields['merge_with_groups'].queryset = queryset
        super(GroupBaseEditAdminForm, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        utils.merge_groups(self.instance,
                           self.cleaned_data.get('merge_with_groups', []))
        return super(GroupBaseEditAdminForm, self).save(*args, **kwargs)


class GroupBaseAdmin(admin.ModelAdmin):
    """GroupBase Admin."""
    save_on_top = True
    search_fields = ['name']
    list_display = ['name', 'no_members']
    list_display_links = ['name']
    list_filter = [EmptyGroupFilter]

    def get_form(self, request, obj=None, **kwargs):
        defaults = {}
        if obj is None:
            defaults['form'] = self.add_form
        defaults.update(kwargs)
        return super(GroupBaseAdmin, self).get_form(request, obj, **defaults)

    def queryset(self, request):
        return (super(GroupBaseAdmin, self)
                .queryset(request).annotate(no_members=Count('userprofile')))

    def no_members(self, obj):
        """Return number of members in group."""
        return obj.no_members
    no_members.admin_order_field = 'no_members'


class GroupAliasInline(admin.StackedInline):
    model = GroupAlias


class GroupAddAdminForm(forms.ModelForm):

    class Meta:
        model = Group


class GroupEditAdminForm(GroupBaseEditAdminForm):

    class Meta:
        model = Group


class GroupAdmin(GroupBaseAdmin):
    """Group Admin."""
    form = GroupEditAdminForm
    add_form = GroupAddAdminForm
    inlines = [GroupAliasInline]
    list_display = ['name', 'steward', 'wiki', 'website', 'irc_channel',
                    'no_members']
    raw_id_fields = ['steward']
    list_filter = [CurratedGroupFilter, EmptyGroupFilter]


class SkillAliasInline(admin.StackedInline):
    model = SkillAlias


class SkillAddAdminForm(forms.ModelForm):

    class Meta:
        model = Skill


class SkillEditAdminForm(GroupBaseEditAdminForm):

    class Meta:
        model = Skill


class SkillAdmin(GroupBaseAdmin):
    form = SkillEditAdminForm
    add_form = SkillAddAdminForm
    inlines = [SkillAliasInline]


class LanguageAliasInline(admin.StackedInline):<commit_msg>[Bug 846678] change no_members to member_count for improved clarity<commit_after>from django import forms
from django.contrib import admin
from django.contrib.admin import SimpleListFilter
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.db.models import Count

import utils
from models import (Group, GroupAlias,
                    Language, LanguageAlias,
                    Skill, SkillAlias)


class EmptyGroupFilter(SimpleListFilter):
    title = 'empty_group'
    parameter_name = 'empty_group'

    def lookups(self, request, model_admin):
        return (('False', 'Empty'),
                ('True', 'Not empty'))

    def queryset(self, request, queryset):
        if self.value() is None:
            return queryset
        value = self.value() == 'True'
        queryset = (queryset.annotate(no_profiles=Count('userprofile')))
        if value:
            return queryset.filter(no_profiles__gt=0)
        return queryset.filter(no_profiles=0)


class CurratedGroupFilter(SimpleListFilter):
    """Admin filter for curated groups."""
    title = 'curated'
    parameter_name = 'curated'

    def lookups(self, request, model_admin):
        return (('False', 'Curated'),
                ('True', 'Not curated'))

    def queryset(self, request, queryset):
        if self.value() is None:
            return queryset
        value = self.value() == 'True'
        return queryset.filter(steward__isnull=value)


class GroupBaseEditAdminForm(forms.ModelForm):
    merge_with_groups = forms.ModelMultipleChoiceField(
        required=False, queryset = None,
        widget=FilteredSelectMultiple('Merge', False))

    def __init__(self, *args, **kwargs):
        queryset = self._meta.model.objects.exclude(pk=kwargs['instance'].id)
        self.base_fields['merge_with_groups'].queryset = queryset
        super(GroupBaseEditAdminForm, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        utils.merge_groups(self.instance,
                           self.cleaned_data.get('merge_with_groups', []))
        return super(GroupBaseEditAdminForm, self).save(*args, **kwargs)


class GroupBaseAdmin(admin.ModelAdmin):
    """GroupBase Admin."""
    save_on_top = True
    search_fields = ['name']
    list_display = ['name', 'member_count']
    list_display_links = ['name']
    list_filter = [EmptyGroupFilter]

    def get_form(self, request, obj=None, **kwargs):
        defaults = {}
        if obj is None:
            defaults['form'] = self.add_form
        defaults.update(kwargs)
        return super(GroupBaseAdmin, self).get_form(request, obj, **defaults)

    def queryset(self, request):
        return (super(GroupBaseAdmin, self)
                .queryset(request).annotate(member_count=Count('userprofile')))

    def member_count(self, obj):
        """Return number of members in group."""
        return obj.member_count
    member_count.admin_order_field = 'member_count'


class GroupAliasInline(admin.StackedInline):
    model = GroupAlias


class GroupAddAdminForm(forms.ModelForm):

    class Meta:
        model = Group


class GroupEditAdminForm(GroupBaseEditAdminForm):

    class Meta:
        model = Group


class GroupAdmin(GroupBaseAdmin):
    """Group Admin."""
    form = GroupEditAdminForm
    add_form = GroupAddAdminForm
    inlines = [GroupAliasInline]
    list_display = ['name', 'steward', 'wiki', 'website', 'irc_channel',
                    'member_count']
    raw_id_fields = ['steward']
    list_filter = [CurratedGroupFilter, EmptyGroupFilter]


class SkillAliasInline(admin.StackedInline):
    model = SkillAlias


class SkillAddAdminForm(forms.ModelForm):

    class Meta:
        model = Skill


class SkillEditAdminForm(GroupBaseEditAdminForm):

    class Meta:
        model = Skill


class SkillAdmin(GroupBaseAdmin):
    form = SkillEditAdminForm
    add_form = SkillAddAdminForm
    inlines = [SkillAliasInline]


class LanguageAliasInline(admin.StackedInline):
{'fraction_non_alphanumeric': 0.10730593607305935, 'fraction_numerical': 0.0012683916793505834, 'mean_word_length': 3.196381053751996, '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': '3668261', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2245, 'n_words': 509}