<commit_before>#Copyright 2012 Newcastle University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
from django import forms
from django.forms.models import inlineformset_factory
from django.forms.widgets import SelectMultiple
from django.core.exceptions import ValidationError

import zipfile
import os
import tempfile

from editor.models import Exam, Question, ExamQuestion, QuestionAccess, ExamAccess, QuestionHighlight, ExamHighlight, Theme, Extension
from django.contrib.auth.models import User

class FixedSelectMultiple(SelectMultiple):
    def value_from_datadict(self,data,files,name):
        name += '[]'
        v = super(FixedSelectMultiple,self).value_from_datadict(data,files,name)
        return v

class TagField(forms.CharField):
    def clean(self,value):
        tags_string = super(TagField,self).clean(value)
        if len(tags_string.strip()):
            tags = tags_string.split(',')
            return [t.strip() for t in tags]
        else:
            return []

class QuestionSearchForm(forms.Form):
    query = forms.CharField(initial='', required=False)
    author = forms.CharField(initial='', required=False)
    progress = forms.ChoiceField(initial='',choices = [('','Any')]+Question.PROGRESS_CHOICES, required=False)
    filter_copies = forms.BooleanField(initial=False)
    tags = TagField(initial='', required=False, widget=forms.TextInput(attrs={'placeholder': 'Tags separated by commas'}))

class QuestionAccessForm(forms.ModelForm):
    class Meta:
        model = QuestionAccess
        exclude = []

class QuestionSetAccessForm(forms.ModelForm):
    class Meta:
        model = Question
        fields = ['public_access']

    def is_valid(self):
        v = super(QuestionSetAccessForm,self).is_valid()
        for f in self.user_access_forms:
            if not f.is_valid():
                return False
        return v
    
    def clean(self):
        cleaned_data = super(QuestionSetAccessForm,self).clean()

        user_ids = self.data.getlist('user_ids[]')
        access_levels = self.data.getlist('access_levels[]')
        self.user_access_forms = []

        for i,(user,access_level) in enumerate(zip(user_ids,access_levels)):
            f = QuestionAccessForm({'user':user,'access':access_level,'question':self.instance.pk})
            f.full_clean()
            self.user_access_forms.append(f)
            for key,messages in f.errors.items():
                self._errors[('user %i: ' % i)+key]=messages

        return cleaned_data

    def save(self):
        self.instance.access_rights.clear()
        for f in self.user_access_forms:
            f.save()
        return super(QuestionSetAccessForm,self).save()

class ExamSearchForm(forms.Form):
    query = forms.CharField(initial='', required=False)
    author = forms.CharField(initial='', required=False)

class ExamAccessForm(forms.ModelForm):
    class Meta:
        model = ExamAccess

class ExamSetAccessForm(forms.ModelForm):
    class Meta:
        model = Exam
        fields = ['public_access']

    def is_valid(self):
        v = super(ExamSetAccessForm,self).is_valid()
        for f in self.user_access_forms:
            if not f.is_valid():
                return False
        return v
    
    def clean(self):
        cleaned_data = super(ExamSetAccessForm,self).clean()

        user_ids = self.data.getlist('user_ids[]')
        access_levels = self.data.getlist('access_levels[]')
        self.user_access_forms = []

        for i,(user,access_level) in enumerate(zip(user_ids,access_levels)):
            f = ExamAccessForm({'user':user,'access':access_level,'exam':self.instance.pk})
            f.full_clean()
            self.user_access_forms.append(f)
            for key,messages in f.errors.items():
                self._errors[('user %i: ' % i)+key]=messages

        return cleaned_data

    def save(self):
        self.instance.access_rights.clear()
        for f in self.user_access_forms:
            f.save()
        return super(ExamSetAccessForm,self).save()
        
class QuestionForm(forms.ModelForm):
    
    """Form for a question."""

    class Meta:
        model = Question
        exclude = ('name','author','tags','public_access','copy_of')

class QuestionHighlightForm(forms.ModelForm):
    note = forms.CharField(widget=forms.Textarea(attrs={'data-bind':'text:note'}), label='Write a note explaining why you\'re highlighting this question.')

    class Meta:
        model = QuestionHighlight
        fields = ['note']
        
class NewQuestionForm(forms.ModelForm):
    
    """Form for a new question only, not including some fields."""
    
    class Meta:
        model = Question
        fields = ('name','author')
        
        
class ExamForm(forms.ModelForm):
    
    """Form for an exam."""
    
    class Meta:
        model = Exam
        exclude = ('name','author','public_access')
        
        
class NewExamForm(forms.ModelForm):
    
    """Form for a new exam only, not including some fields."""
    
    class Meta:
        model = Exam
        fields = ('name','author')

class ExamQuestionForm(forms.ModelForm):
    
    """Form linking exams and questions."""
    
    qn_order = forms.IntegerField(label='Order')
    
    class Meta:
        model = ExamQuestion

ExamQuestionFormSet = inlineformset_factory(Exam, ExamQuestion, form=ExamQuestionForm)

class ExamHighlightForm(forms.ModelForm):
    note = forms.CharField(widget=forms.Textarea(attrs={'data-bind':'text:note'}), label='Write a note explaining why you\'re highlighting this exam.')<commit_msg>add `exclude` field to some forms<commit_after>#Copyright 2012 Newcastle University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
from django import forms
from django.forms.models import inlineformset_factory
from django.forms.widgets import SelectMultiple
from django.core.exceptions import ValidationError

import zipfile
import os
import tempfile

from editor.models import Exam, Question, ExamQuestion, QuestionAccess, ExamAccess, QuestionHighlight, ExamHighlight, Theme, Extension
from django.contrib.auth.models import User

class FixedSelectMultiple(SelectMultiple):
    def value_from_datadict(self,data,files,name):
        name += '[]'
        v = super(FixedSelectMultiple,self).value_from_datadict(data,files,name)
        return v

class TagField(forms.CharField):
    def clean(self,value):
        tags_string = super(TagField,self).clean(value)
        if len(tags_string.strip()):
            tags = tags_string.split(',')
            return [t.strip() for t in tags]
        else:
            return []

class QuestionSearchForm(forms.Form):
    query = forms.CharField(initial='', required=False)
    author = forms.CharField(initial='', required=False)
    progress = forms.ChoiceField(initial='',choices = [('','Any')]+Question.PROGRESS_CHOICES, required=False)
    filter_copies = forms.BooleanField(initial=False)
    tags = TagField(initial='', required=False, widget=forms.TextInput(attrs={'placeholder': 'Tags separated by commas'}))

class QuestionAccessForm(forms.ModelForm):
    class Meta:
        model = QuestionAccess
        exclude = []

class QuestionSetAccessForm(forms.ModelForm):
    class Meta:
        model = Question
        fields = ['public_access']

    def is_valid(self):
        v = super(QuestionSetAccessForm,self).is_valid()
        for f in self.user_access_forms:
            if not f.is_valid():
                return False
        return v
    
    def clean(self):
        cleaned_data = super(QuestionSetAccessForm,self).clean()

        user_ids = self.data.getlist('user_ids[]')
        access_levels = self.data.getlist('access_levels[]')
        self.user_access_forms = []

        for i,(user,access_level) in enumerate(zip(user_ids,access_levels)):
            f = QuestionAccessForm({'user':user,'access':access_level,'question':self.instance.pk})
            f.full_clean()
            self.user_access_forms.append(f)
            for key,messages in f.errors.items():
                self._errors[('user %i: ' % i)+key]=messages

        return cleaned_data

    def save(self):
        self.instance.access_rights.clear()
        for f in self.user_access_forms:
            f.save()
        return super(QuestionSetAccessForm,self).save()

class ExamSearchForm(forms.Form):
    query = forms.CharField(initial='', required=False)
    author = forms.CharField(initial='', required=False)

class ExamAccessForm(forms.ModelForm):
    class Meta:
        model = ExamAccess
        exclude = []

class ExamSetAccessForm(forms.ModelForm):
    class Meta:
        model = Exam
        fields = ['public_access']

    def is_valid(self):
        v = super(ExamSetAccessForm,self).is_valid()
        for f in self.user_access_forms:
            if not f.is_valid():
                return False
        return v
    
    def clean(self):
        cleaned_data = super(ExamSetAccessForm,self).clean()

        user_ids = self.data.getlist('user_ids[]')
        access_levels = self.data.getlist('access_levels[]')
        self.user_access_forms = []

        for i,(user,access_level) in enumerate(zip(user_ids,access_levels)):
            f = ExamAccessForm({'user':user,'access':access_level,'exam':self.instance.pk})
            f.full_clean()
            self.user_access_forms.append(f)
            for key,messages in f.errors.items():
                self._errors[('user %i: ' % i)+key]=messages

        return cleaned_data

    def save(self):
        self.instance.access_rights.clear()
        for f in self.user_access_forms:
            f.save()
        return super(ExamSetAccessForm,self).save()
        
class QuestionForm(forms.ModelForm):
    
    """Form for a question."""

    class Meta:
        model = Question
        exclude = ('name','author','tags','public_access','copy_of')

class QuestionHighlightForm(forms.ModelForm):
    note = forms.CharField(widget=forms.Textarea(attrs={'data-bind':'text:note'}), label='Write a note explaining why you\'re highlighting this question.')

    class Meta:
        model = QuestionHighlight
        fields = ['note']
        
class NewQuestionForm(forms.ModelForm):
    
    """Form for a new question only, not including some fields."""
    
    class Meta:
        model = Question
        fields = ('name','author')
        
        
class ExamForm(forms.ModelForm):
    
    """Form for an exam."""
    
    class Meta:
        model = Exam
        exclude = ('name','author','public_access')
        
        
class NewExamForm(forms.ModelForm):
    
    """Form for a new exam only, not including some fields."""
    
    class Meta:
        model = Exam
        fields = ('name','author')

class ExamQuestionForm(forms.ModelForm):
    
    """Form linking exams and questions."""
    
    qn_order = forms.IntegerField(label='Order')
    
    class Meta:
        model = ExamQuestion
        exclude = []

ExamQuestionFormSet = inlineformset_factory(Exam, ExamQuestion, form=ExamQuestionForm)

class ExamHighlightForm(forms.ModelForm):
    note = forms.CharField(widget=forms.Textarea(attrs={'data-bind':'text:note'}), label='Write a note explaining why you\'re highlighting this exam.')
{'fraction_non_alphanumeric': 0.11131118022589621, 'fraction_numerical': 0.0013095432967752496, 'mean_word_length': 2.993137254901961, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2829793', 'n_tokens_mistral': 3497, 'n_tokens_neox': 3414, 'n_words': 840}