<commit_before>from django import forms
from django.contrib.auth.models import User
from .models import Submission, StudyProgram


def validate_authors(authors, assignment):
    if assignment.max_authors < len(authors):
        raise forms.ValidationError(
            "At most {0} authors are allowed.".format(assignment.max_authors))
    for author in authors:
        if not assignment.can_create_submission(author):
            raise forms.ValidationError(
                "{0} is not allowed to submit solutions for this assignment.".format(author))


class SubmissionWithGroups(forms.ModelForm):

    class Meta:
        model = Submission
        fields = ('authors', 'notes')

    def __init__(self, current_user, ass, *args, **kwargs):
        '''
        Adjusts the list of choices for the authors.'''
        super(SubmissionWithGroups, self).__init__(*args, **kwargs)

        self.assignment = ass

        # remove all users already having a submission for the assignment
        # + the current user
        havingSubmissions = []
        for submission in ass.submissions.all().exclude(state=Submission.WITHDRAWN):
            for author in submission.authors.all():
                havingSubmissions.append(author.pk)
        # The submitter should still be in the list (see #13),
        # but the course owner should not (see #56)
        allowed_authors = User.objects.exclude(
            pk__in=havingSubmissions).exclude(is_active=False)
        # But the course owner must be in the list when creating
        # a test submission (see #203)
        if current_user.pk != ass.course.owner.pk:
            allowed_authors = allowed_authors.exclude(pk=ass.course.owner.pk)
        self.fields['authors'].queryset = allowed_authors

    def clean_authors(self):
        data = self.cleaned_data['authors']
        validate_authors(data, self.assignment)
        return data


class SubmissionWithoutGroups(forms.ModelForm):

    class Meta:
        model = Submission
        fields = ('notes',)

    def __init__(self, current_user, ass, *args, **kwargs):
        super(SubmissionWithoutGroups, self).__init__(*args, **kwargs)

        self.assignment = ass

    def clean_authors(self):
        data = self.cleaned_data['authors']
        validate_authors(data, self.assignment)
        return data


class SubmissionWithoutGroupsWithFileForm(SubmissionWithoutGroups):
    attachment = forms.FileField()


class SubmissionWithoutGroupsWithoutFileForm(SubmissionWithoutGroups):
    pass


class SubmissionWithGroupsWithFileForm(SubmissionWithGroups):
    attachment = forms.FileField()


class SubmissionWithGroupsWithoutFileForm(SubmissionWithGroups):
    pass


class SubmissionFileUpdateForm(forms.ModelForm):

    attachment = forms.FileField()

    class Meta:
        model = Submission
        fields = ('notes',)


def getSubmissionForm(assignment):
    if assignment.max_authors > 1:
        if assignment.has_attachment:
            return SubmissionWithGroupsWithFileForm
        else:
            return SubmissionWithGroupsWithoutFileForm
    else:
        if assignment.has_attachment:
            return SubmissionWithoutGroupsWithFileForm
        else:
            return SubmissionWithoutGroupsWithoutFileForm


class SettingsForm(forms.ModelForm):
    email = forms.CharField(max_length=75, required=True)
    first_name = forms.CharField(max_length=30, required=True)
    last_name = forms.CharField(max_length=30, required=True)
    username = forms.CharField(max_length=30, required=True)<commit_msg>Smaller notes field for submissions<commit_after>from django import forms
from django.contrib.auth.models import User
from .models import Submission, StudyProgram


def validate_authors(authors, assignment):
    if assignment.max_authors < len(authors):
        raise forms.ValidationError(
            "At most {0} authors are allowed.".format(assignment.max_authors))
    for author in authors:
        if not assignment.can_create_submission(author):
            raise forms.ValidationError(
                "{0} is not allowed to submit solutions for this assignment.".format(author))


class SubmissionWithGroups(forms.ModelForm):

    notes = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}))

    class Meta:
        model = Submission
        fields = ('authors', 'notes')

    def __init__(self, current_user, ass, *args, **kwargs):
        '''
        Adjusts the list of choices for the authors.'''
        super(SubmissionWithGroups, self).__init__(*args, **kwargs)

        self.assignment = ass

        # remove all users already having a submission for the assignment
        # + the current user
        havingSubmissions = []
        for submission in ass.submissions.all().exclude(state=Submission.WITHDRAWN):
            for author in submission.authors.all():
                havingSubmissions.append(author.pk)
        # The submitter should still be in the list (see #13),
        # but the course owner should not (see #56)
        allowed_authors = User.objects.exclude(
            pk__in=havingSubmissions).exclude(is_active=False)
        # But the course owner must be in the list when creating
        # a test submission (see #203)
        if current_user.pk != ass.course.owner.pk:
            allowed_authors = allowed_authors.exclude(pk=ass.course.owner.pk)
        self.fields['authors'].queryset = allowed_authors

    def clean_authors(self):
        data = self.cleaned_data['authors']
        validate_authors(data, self.assignment)
        return data


class SubmissionWithoutGroups(forms.ModelForm):

    notes = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}))

    class Meta:
        model = Submission
        fields = ('notes',)

    def __init__(self, current_user, ass, *args, **kwargs):
        super(SubmissionWithoutGroups, self).__init__(*args, **kwargs)

        self.assignment = ass

    def clean_authors(self):
        data = self.cleaned_data['authors']
        validate_authors(data, self.assignment)
        return data


class SubmissionWithoutGroupsWithFileForm(SubmissionWithoutGroups):
    attachment = forms.FileField()


class SubmissionWithoutGroupsWithoutFileForm(SubmissionWithoutGroups):
    pass


class SubmissionWithGroupsWithFileForm(SubmissionWithGroups):
    attachment = forms.FileField()


class SubmissionWithGroupsWithoutFileForm(SubmissionWithGroups):
    pass


class SubmissionFileUpdateForm(forms.ModelForm):

    attachment = forms.FileField()
    notes = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}))

    class Meta:
        model = Submission
        fields = ('notes',)


def getSubmissionForm(assignment):
    if assignment.max_authors > 1:
        if assignment.has_attachment:
            return SubmissionWithGroupsWithFileForm
        else:
            return SubmissionWithGroupsWithoutFileForm
    else:
        if assignment.has_attachment:
            return SubmissionWithoutGroupsWithFileForm
        else:
            return SubmissionWithoutGroupsWithoutFileForm


class SettingsForm(forms.ModelForm):
    email = forms.CharField(max_length=75, required=True)
    first_name = forms.CharField(max_length=30, required=True)
    last_name = forms.CharField(max_length=30, required=True)
    username = forms.CharField(max_length=30, required=True)
{'fraction_non_alphanumeric': 0.08914100486223663, 'fraction_numerical': 0.006077795786061589, 'mean_word_length': 3.2217787913340934, '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': '2324813', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1940, 'n_words': 489}