<commit_before>from operator import itemgetter, attrgetter
from django.conf import settings
from django.contrib import admin, messages
from django.conf.urls import patterns, url
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.db.models import TextField, ManyToManyField, Q
from django.forms import ModelForm, ModelMultipleChoiceField, TextInput
from django.http import HttpResponseRedirect, HttpResponseForbidden, Http404
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.utils.html import format_html
from django.utils.safestring import mark_safe

from judge.models import Language, Profile, Problem, ProblemGroup, ProblemType, Submission, Comment, \
    MiscConfig, Judge, NavigationBar, Contest, ContestParticipation, ContestProblem, Organization, BlogPost, \
    ContestProfile
from judge.widgets import CheckboxSelectMultipleWithSelectAll, AdminPagedownWidget, MathJaxAdminPagedownWidget


class ContestProfileInlineForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(ContestProfileInlineForm, self).__init__(*args, **kwargs)
        self.fields['current'].queryset = self.instance.history.select_related('contest').all()
        self.fields['current'].label_from_instance = lambda obj: obj.contest.name


class ContestProfileInline(admin.StackedInline):
    fields = ('current',)
    model = ContestProfile
    form = ContestProfileInlineForm
    can_delete = False


class ProfileAdmin(admin.ModelAdmin):
    fields = ('user', 'name', 'about', 'organization', 'timezone', 'language', 'ace_theme', 'last_access', 'ip')
    list_display = ('admin_user_admin', 'email', 'timezone_full', 'language', 'last_access', 'ip')
    ordering = ('user__username',)
    search_fields = ('user__username', 'name', 'ip', 'user__email')
    list_filter = ('language', 'timezone')
    actions = ('recalculate_points',)
    inlines = [ContestProfileInline]
    actions_on_top = True
    actions_on_bottom = True

    def admin_user_admin(self, obj):
        return obj.long_display_name
    admin_user_admin.admin_order_field = 'user__username'
    admin_user_admin.short_description = 'User'

    def email(self, obj):
        return obj.user.email
    email.admin_order_field = 'user__email'
    email.short_description = 'Email'

    def timezone_full(self, obj):
        return obj.timezone

    timezone_full.admin_order_field = 'timezone'
    timezone_full.short_description = 'Timezone'

    def recalculate_points(self, request, queryset):
        count = 0
        for profile in queryset:
            profile.calculate_points()
            count += 1
        self.message_user(request, "%d user%s have scores recalculated." % (count, 's'[count == 1:]))

    recalculate_points.short_description = 'Recalculate scores'


class ProblemAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('code', 'name', 'is_public', 'authors', 'description')
        }),
        ('Taxonomy', {'fields': ('types', 'group')}),
        ('Points', {'fields': (('points', 'partial'), 'short_circuit')}),
        ('Limits', {'fields': ('time_limit', 'memory_limit')}),
        ('Language', {'fields': ('allowed_languages',)})
    )
    list_display = ['code', 'name', 'show_authors', 'points', 'is_public']
    filter_horizontal = ['authors']
    ordering = ['code']
    if settings.ENABLE_FTS:
        search_fields = ('@code', '@name', '@description')
    else:
        search_fields = ('^code', 'name')
    actions = ['make_public', 'make_private']
    list_per_page = 500
    list_max_show_all = 1000
    actions_on_top = True
    actions_on_bottom = True

    if MathJaxAdminPagedownWidget is not None:
        formfield_overrides = {
            TextField: {'widget': MathJaxAdminPagedownWidget},
        }

    def show_authors(self, obj):
        return ', '.join(map(attrgetter('user.username'), obj.authors.select_related('user')))
    show_authors.short_description = 'Authors'

    def make_public(self, request, queryset):
        count = queryset.update(is_public=True)
        self.message_user(request, "%d problem%s successfully marked as public." % (count, 's'[count == 1:]))

    make_public.short_description = 'Mark problems as public'

    def make_private(self, request, queryset):
        count = queryset.update(is_public=False)
        self.message_user(request, "%d problem%s successfully marked as private." % (count, 's'[count == 1:]))

    make_private.short_description = 'Mark problems as private'

    def get_queryset(self, request):
        if request.user.has_perm('judge.edit_all_problem'):
            return Problem.objects.all()<commit_msg>Screw you django, can't use one index.<commit_after>from operator import itemgetter, attrgetter
from django.conf import settings
from django.contrib import admin, messages
from django.conf.urls import patterns, url
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.db.models import TextField, ManyToManyField, Q
from django.forms import ModelForm, ModelMultipleChoiceField, TextInput
from django.http import HttpResponseRedirect, HttpResponseForbidden, Http404
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.utils.html import format_html
from django.utils.safestring import mark_safe

from judge.models import Language, Profile, Problem, ProblemGroup, ProblemType, Submission, Comment, \
    MiscConfig, Judge, NavigationBar, Contest, ContestParticipation, ContestProblem, Organization, BlogPost, \
    ContestProfile
from judge.widgets import CheckboxSelectMultipleWithSelectAll, AdminPagedownWidget, MathJaxAdminPagedownWidget


class ContestProfileInlineForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(ContestProfileInlineForm, self).__init__(*args, **kwargs)
        self.fields['current'].queryset = self.instance.history.select_related('contest').all()
        self.fields['current'].label_from_instance = lambda obj: obj.contest.name


class ContestProfileInline(admin.StackedInline):
    fields = ('current',)
    model = ContestProfile
    form = ContestProfileInlineForm
    can_delete = False


class ProfileAdmin(admin.ModelAdmin):
    fields = ('user', 'name', 'about', 'organization', 'timezone', 'language', 'ace_theme', 'last_access', 'ip')
    list_display = ('admin_user_admin', 'email', 'timezone_full', 'language', 'last_access', 'ip')
    ordering = ('user__username',)
    search_fields = ('user__username', 'name', 'ip', 'user__email')
    list_filter = ('language', 'timezone')
    actions = ('recalculate_points',)
    inlines = [ContestProfileInline]
    actions_on_top = True
    actions_on_bottom = True

    def admin_user_admin(self, obj):
        return obj.long_display_name
    admin_user_admin.admin_order_field = 'user__username'
    admin_user_admin.short_description = 'User'

    def email(self, obj):
        return obj.user.email
    email.admin_order_field = 'user__email'
    email.short_description = 'Email'

    def timezone_full(self, obj):
        return obj.timezone

    timezone_full.admin_order_field = 'timezone'
    timezone_full.short_description = 'Timezone'

    def recalculate_points(self, request, queryset):
        count = 0
        for profile in queryset:
            profile.calculate_points()
            count += 1
        self.message_user(request, "%d user%s have scores recalculated." % (count, 's'[count == 1:]))

    recalculate_points.short_description = 'Recalculate scores'


class ProblemAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('code', 'name', 'is_public', 'authors', 'description')
        }),
        ('Taxonomy', {'fields': ('types', 'group')}),
        ('Points', {'fields': (('points', 'partial'), 'short_circuit')}),
        ('Limits', {'fields': ('time_limit', 'memory_limit')}),
        ('Language', {'fields': ('allowed_languages',)})
    )
    list_display = ['code', 'name', 'show_authors', 'points', 'is_public']
    filter_horizontal = ['authors']
    ordering = ['code']
    search_fields = ('^code', 'name')
    actions = ['make_public', 'make_private']
    list_per_page = 500
    list_max_show_all = 1000
    actions_on_top = True
    actions_on_bottom = True

    def get_search_results(self, request, queryset, search_term):
        if settings.ENABLE_FTS:
            return queryset.search(search_term)
        return super(ProblemAdmin, self).get_search_results(request, queryset, search_term)

    if MathJaxAdminPagedownWidget is not None:
        formfield_overrides = {
            TextField: {'widget': MathJaxAdminPagedownWidget},
        }

    def show_authors(self, obj):
        return ', '.join(map(attrgetter('user.username'), obj.authors.select_related('user')))
    show_authors.short_description = 'Authors'

    def make_public(self, request, queryset):
        count = queryset.update(is_public=True)
        self.message_user(request, "%d problem%s successfully marked as public." % (count, 's'[count == 1:]))

    make_public.short_description = 'Mark problems as public'

    def make_private(self, request, queryset):
        count = queryset.update(is_public=False)
        self.message_user(request, "%d problem%s successfully marked as private." % (count, 's'[count == 1:]))

    make_private.short_description = 'Mark problems as private'

    def get_queryset(self, request):
        if request.user.has_perm('judge.edit_all_problem'):
            return Problem.objects.all()
{'fraction_non_alphanumeric': 0.12022367194780988, 'fraction_numerical': 0.0031065548306927616, 'mean_word_length': 4.161945483698557, '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': '2587606', 'n_tokens_mistral': 2879, 'n_tokens_neox': 2778, 'n_words': 660}