class ContributionForm(forms.ModelForm, BootstrapMixin): responsibility = forms.ChoiceField( widget=forms.RadioSelect(), choices=Contribution.RESPONSIBILITY_CHOICES) course = forms.ModelChoiceField(Course.objects.all(), disabled=True, required=False, widget=forms.HiddenInput()) questionnaires = QuestionnaireMultipleChoiceField( Questionnaire.objects.filter(is_for_contributors=True, obsolete=False), label=_("Questionnaires")) class Meta: model = Contribution fields = ('course', 'contributor', 'questionnaires', 'order', 'responsibility', 'comment_visibility', 'label') widgets = { 'order': forms.HiddenInput(), 'comment_visibility': forms.RadioSelect(choices=Contribution.COMMENT_VISIBILITY_CHOICES) } def __init__(self, *args, **kwargs): # work around https://code.djangoproject.com/ticket/25880 self.course = kwargs.pop('course', None) if self.course is None: assert 'instance' in kwargs self.course = kwargs['instance'].course super().__init__(*args, **kwargs) self.fields['contributor'].widget.attrs['class'] = 'form-control' self.fields['label'].widget.attrs['class'] = 'form-control' if self.instance.responsible: self.fields['responsibility'].initial = Contribution.IS_RESPONSIBLE elif self.instance.can_edit: self.fields['responsibility'].initial = Contribution.IS_EDITOR else: self.fields['responsibility'].initial = Contribution.IS_CONTRIBUTOR self.fields['questionnaires'].queryset = Questionnaire.objects.filter( is_for_contributors=True).filter( Q(obsolete=False) | Q(contributions__course=self.course)).distinct() def save(self, *args, **kwargs): responsibility = self.cleaned_data['responsibility'] is_responsible = responsibility == Contribution.IS_RESPONSIBLE is_editor = responsibility == Contribution.IS_EDITOR self.instance.responsible = is_responsible self.instance.can_edit = is_responsible or is_editor if is_responsible: self.instance.comment_visibility = Contribution.ALL_COMMENTS return super().save(*args, **kwargs)
class CourseForm(forms.ModelForm, BootstrapMixin): general_questions = QuestionnaireMultipleChoiceField(Questionnaire.objects.filter(is_for_contributors=False, obsolete=False), label=_("General questions")) semester = forms.ModelChoiceField(Semester.objects.all(), disabled=True, required=False, widget=forms.HiddenInput()) # the following field is needed, because the auto_now=True for last_modified_time makes the corresponding field # uneditable and so it can't be displayed in the model form # see https://docs.djangoproject.com/en/dev/ref/models/fields/#datefield for details last_modified_time_2 = forms.DateTimeField(label=_("Last modified"), required=False, localize=True, disabled=True) # last_modified_user would usually get a select widget but should here be displayed as a readonly CharField instead last_modified_user_2 = forms.CharField(label=_("Last modified by"), required=False, disabled=True) class Meta: model = Course fields = ('name_de', 'name_en', 'type', 'degrees', 'is_graded', 'is_required_for_reward', 'vote_start_date', 'vote_end_date', 'participants', 'general_questions', 'last_modified_time_2', 'last_modified_user_2', 'semester') localized_fields = ('vote_start_date', 'vote_end_date') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['general_questions'].queryset = Questionnaire.objects.filter(is_for_contributors=False).filter( Q(obsolete=False) | Q(contributions__course=self.instance)).distinct() self.fields['type'].widget = forms.Select(choices=[(a, a) for a in Course.objects.values_list('type', flat=True).order_by().distinct()]) if self.instance.general_contribution: self.fields['general_questions'].initial = [q.pk for q in self.instance.general_contribution.questionnaires.all()] self.fields['last_modified_time_2'].initial = self.instance.last_modified_time if self.instance.last_modified_user: self.fields['last_modified_user_2'].initial = self.instance.last_modified_user.full_name if self.instance.state in ['inEvaluation', 'evaluated', 'reviewed']: self.fields['vote_start_date'].disabled = True def clean(self): super().clean() vote_start_date = self.cleaned_data.get('vote_start_date') vote_end_date = self.cleaned_data.get('vote_end_date') if vote_start_date and vote_end_date: if vote_start_date >= vote_end_date: raise ValidationError(_("The first day of evaluation must be before the last one.")) def save(self, user, *args, **kw): self.instance.last_modified_user = user super().save(*args, **kw) self.instance.general_contribution.questionnaires = self.cleaned_data.get('general_questions') logger.info('Course "{}" (id {}) was edited by staff member {}.'.format(self.instance, self.instance.id, user.username))
class CourseForm(forms.ModelForm, BootstrapMixin): general_questions = QuestionnaireMultipleChoiceField( Questionnaire.objects.filter(is_for_contributors=False, obsolete=False), label=_(u"General questions")) class Meta: model = Course fields = ('name_de', 'name_en', 'vote_start_date', 'vote_end_date', 'kind', 'degree', 'general_questions') def __init__(self, *args, **kwargs): super(CourseForm, self).__init__(*args, **kwargs) self.fields['vote_start_date'].localize = True self.fields['vote_end_date'].localize = True self.fields['kind'].widget = forms.Select( choices=[(a, a) for a in Course.objects.values_list( 'kind', flat=True).order_by().distinct()]) self.fields['degree'].widget.attrs['readonly'] = True if self.instance.general_contribution: self.fields['general_questions'].initial = [ q.pk for q in self.instance.general_contribution.questionnaires.all() ] def clean_degree(self): return self.instance.degree def save(self, *args, **kw): user = kw.pop("user") super(CourseForm, self).save(*args, **kw) self.instance.general_contribution.questionnaires = self.cleaned_data.get( 'general_questions') self.instance.last_modified_user = user self.instance.save() def validate_unique(self): exclude = self._get_validation_exclusions() exclude.remove( 'semester') # allow checking against the missing attribute try: self.instance.validate_unique(exclude=exclude) except forms.ValidationError, e: self._update_errors(e.message_dict)
class CourseForm(forms.ModelForm, BootstrapMixin): general_questions = QuestionnaireMultipleChoiceField( queryset=None, label=_("General questions")) semester = forms.ModelChoiceField(Semester.objects.all(), disabled=True, required=False, widget=forms.HiddenInput()) class Meta: model = Course fields = ('name_de', 'name_en', 'vote_start_date', 'vote_end_date', 'type', 'degrees', 'general_questions', 'semester') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields[ 'general_questions'].queryset = Questionnaire.objects.filter( is_for_contributors=False).filter( (Q(staff_only=False) & Q(obsolete=False)) | Q(contributions__course=self.instance)).distinct() self.fields['vote_start_date'].localize = True self.fields['vote_end_date'].localize = True self.fields['type'].widget = forms.Select( choices=[(a, a) for a in Course.objects.values_list( 'type', flat=True).order_by().distinct()]) self.fields['degrees'].disabled = True self.fields['degrees'].help_text = "" if self.instance.general_contribution: self.fields['general_questions'].initial = [ q.pk for q in self.instance.general_contribution.questionnaires.all() ] def clean(self): super().clean() vote_start_date = self.cleaned_data.get('vote_start_date') vote_end_date = self.cleaned_data.get('vote_end_date') if vote_start_date and vote_end_date: if vote_start_date >= vote_end_date: raise ValidationError( _("The first day of evaluation must be before the last one." )) def clean_vote_start_date(self): vote_start_date = self.cleaned_data.get('vote_start_date') if vote_start_date and vote_start_date < datetime.date.today(): raise forms.ValidationError( _("The first day of evaluation must be in the future.")) return vote_start_date def clean_vote_end_date(self): vote_end_date = self.cleaned_data.get('vote_end_date') if vote_end_date and vote_end_date < datetime.date.today(): raise forms.ValidationError( _("The last day of evaluation must be in the future.")) return vote_end_date def save(self, *args, **kw): user = kw.pop("user") self.instance.last_modified_user = user super().save(*args, **kw) self.instance.general_contribution.questionnaires = self.cleaned_data.get( 'general_questions') logger.info('Course "{}" (id {}) was edited by contributor {}.'.format( self.instance, self.instance.id, user.username))