<commit_before>from operator import attrgetter

from django import forms
from django.conf import settings
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.contrib.auth.forms import AuthenticationForm
from django.db.models import Q
from django.forms import ModelForm, CharField, TextInput

from django_ace import AceWidget
from judge.models import Organization, Profile, Submission, Problem, PrivateMessage, fix_unicode, Language
from judge.widgets import MathJaxPagedownWidget, PagedownWidget

try:
    from django_select2.forms import Select2Widget
except ImportError:
    Select2Widget = None

use_select2 = Select2Widget is not None and 'django_select2' in settings.INSTALLED_APPS


class ProfileForm(ModelForm):
    class Meta:
        model = Profile
        fields = ['name', 'about', 'organizations', 'timezone', 'language', 'ace_theme']
        widgets = {'name': TextInput(attrs={'style': 'width: 100%; box-sizing: border-box'})}
        if Select2Widget is not None:
            widgets['timezone'] = Select2Widget(attrs={'style': 'width: 200px'})
            widgets['language'] = Select2Widget(attrs={'style': 'width: 300px'})
            widgets['ace_theme'] = Select2Widget(attrs={'style': 'width: 300px'})

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(ProfileForm, self).__init__(*args, **kwargs)
        if not user.has_perm('judge.edit_all_organization'):
            self.fields['organizations'].queryset = Organization.objects.filter(
                Q(is_open=True) | Q(id__in=user.profile.organizations.all())
            )

    def clean_name(self):
        return fix_unicode(self.cleaned_data['name'])


class ProblemSubmitForm(ModelForm):
    source = CharField(max_length=65536, widget=AceWidget(theme='twilight'))

    def __init__(self, *args, **kwargs):
        super(ProblemSubmitForm, self).__init__(*args, **kwargs)
        self.fields['problem'].empty_label = None
        self.fields['problem'].widget = forms.HiddenInput()
        self.fields['language'].empty_label = None
        self.fields['language'].label_from_instance = attrgetter('display_name')
        self.fields['language'].queryset = Language.objects.filter(judges__online=True).distinct()<commit_msg>Add ace widget for #355<commit_after>from operator import attrgetter

from django import forms
from django.conf import settings
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.contrib.auth.forms import AuthenticationForm
from django.db.models import Q
from django.forms import ModelForm, CharField, TextInput

from django_ace import AceWidget
from judge.models import Organization, Profile, Submission, Problem, PrivateMessage, fix_unicode, Language
from judge.widgets import MathJaxPagedownWidget, PagedownWidget

try:
    from django_select2.forms import Select2Widget
except ImportError:
    Select2Widget = None

use_select2 = Select2Widget is not None and 'django_select2' in settings.INSTALLED_APPS


class ProfileForm(ModelForm):
    class Meta:
        model = Profile
        fields = ['name', 'about', 'organizations', 'timezone', 'language', 'ace_theme', 'user_script']
        widgets = {'name': TextInput(attrs={'style': 'width: 100%; box-sizing: border-box'}),
                   'user_script': AceWidget(theme='github')}
        if Select2Widget is not None:
            widgets['timezone'] = Select2Widget(attrs={'style': 'width: 200px'})
            widgets['language'] = Select2Widget(attrs={'style': 'width: 300px'})
            widgets['ace_theme'] = Select2Widget(attrs={'style': 'width: 300px'})

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(ProfileForm, self).__init__(*args, **kwargs)
        if not user.has_perm('judge.edit_all_organization'):
            self.fields['organizations'].queryset = Organization.objects.filter(
                Q(is_open=True) | Q(id__in=user.profile.organizations.all())
            )

    def clean_name(self):
        return fix_unicode(self.cleaned_data['name'])


class ProblemSubmitForm(ModelForm):
    source = CharField(max_length=65536, widget=AceWidget(theme='twilight'))

    def __init__(self, *args, **kwargs):
        super(ProblemSubmitForm, self).__init__(*args, **kwargs)
        self.fields['problem'].empty_label = None
        self.fields['problem'].widget = forms.HiddenInput()
        self.fields['language'].empty_label = None
        self.fields['language'].label_from_instance = attrgetter('display_name')
        self.fields['language'].queryset = Language.objects.filter(judges__online=True).distinct()
{'fraction_non_alphanumeric': 0.12133620689655172, 'fraction_numerical': 0.01228448275862069, 'mean_word_length': 4.185474860335195, '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': '3984989', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1293, 'n_words': 300}