class ContestEditForm(forms.ModelForm): class Meta: model = Contest exclude = ['problems', 'participants', "managers", "freeze_time", "freeze", 'standings_update_time', 'allowed_lang', 'authors', 'volunteers'] help_texts = { 'start_time': 'YYYY-MM-DD --:--(:--)', 'end_time': 'YYYY-MM-DD --:--(:--)', 'freeze_time': 'YYYY-MM-DD --:--(:--)', } widgets = { 'description': forms.Textarea(attrs={'class': 'markdown'}) } field_order = ['title', 'description', 'allowed_lang', 'contest_type', 'how_long', 'start_time', 'end_time', 'access_level', 'common_status_access_level', 'pdf_statement'] allowed_lang = CommaSeparatedMultipleChoiceField(choices=LANG_CHOICE) how_long = forms.ChoiceField(choices=( (3, '从开始时间到结束时间'), (1, '只设置开始时间'), (2, '只设置截止时间'), (0, '永远开放'), ), label='时间设置 (只对作业有效)') def __init__(self, *args, **kwargs): super(ContestEditForm, self).__init__(*args, **kwargs) contest = self.instance self.fields['allowed_lang'].initial = contest.allowed_lang how_long = 0 if self.instance.start_time is not None: how_long |= 1 if self.instance.end_time is not None: how_long |= 2 self.fields['how_long'].initial = how_long def clean(self): cleaned_data = super(ContestEditForm, self).clean() start_time = cleaned_data.get('start_time') end_time = cleaned_data.get('end_time') how_long = int(cleaned_data.get('how_long', 3)) if not how_long & 1: start_time = None if not how_long & 2: end_time = None if cleaned_data.get('contest_type') == 0: if not start_time or not end_time or (end_time - start_time).total_seconds() < 300 or ( end_time - start_time).total_seconds() > 86400 * 14: raise forms.ValidationError("一场比赛时长应该在 5 分钟到 14 天之间。", code='invalid') else: if cleaned_data['scoring_method'] not in ("oi", "acm",): raise forms.ValidationError("作业只支持 OI 和 ACM 赛制。", code='invalid') cleaned_data['start_time'] = start_time cleaned_data['end_time'] = end_time if cleaned_data.get('freeze'): freeze_time = cleaned_data.get('freeze_time') if not start_time <= freeze_time <= end_time: raise forms.ValidationError('封榜时间非法。', code='invalid') return cleaned_data
class SkillEditForm(forms.ModelForm): class Meta: model = Skill exclude = ['parent_id', 'problem_list'] problem_list = CommaSeparatedMultipleChoiceField(required=False) parent = forms.ChoiceField() def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) skill = self.instance self.fields['problem_list'].initial = skill.problem_list self.fields['problem_list'].choices = [(problem.pk, str(problem)) for problem in Problem.objects.only("title").all()] self.fields['parent'].initial = skill.parent_id self.fields['parent'].choices = [(-1, 'None')] + [(skill.pk, skill.name) for skill in Skill.objects.exclude(pk=self.instance.pk).exclude(parent_id=self.instance.pk)]
class ContestEditForm(forms.ModelForm): class Meta: model = Contest exclude = [ 'visible', 'standings_update_time', 'problems', 'participants', "managers", 'system_tested', 'standings_update_time', 'allowed_lang', 'authors' ] help_texts = { 'start_time': 'YYYY-MM-DD --:--(:--)', 'end_time': 'YYYY-MM-DD --:--(:--)', 'freeze_time': 'YYYY-MM-DD --:--(:--)', } widgets = {'description': forms.Textarea(attrs={'class': 'markdown'})} field_order = [ 'title', 'description', 'public', 'allowed_lang', 'start_time', 'end_time' ] allowed_lang = CommaSeparatedMultipleChoiceField(choices=LANG_CHOICE) def __init__(self, *args, **kwargs): super(ContestEditForm, self).__init__(*args, **kwargs) contest = self.instance self.fields['allowed_lang'].initial = contest.allowed_lang def clean(self): cleaned_data = super(ContestEditForm, self).clean() start_time = cleaned_data.get('start_time') end_time = cleaned_data.get('end_time') if not cleaned_data.get('always_running'): if not start_time or not end_time: raise forms.ValidationError( "For contests not always running, you must assign a start time and an end time", code="invalid") if start_time >= end_time: raise forms.ValidationError( "Start time should be earlier than end time.", code='invalid') if cleaned_data.get('freeze'): freeze_time = cleaned_data.get('freeze_time') if not (start_time <= freeze_time <= end_time): raise forms.ValidationError('Freeze time is not legal', code='invalid') return cleaned_data
class ProblemEditForm(forms.ModelForm): class Meta: model = Problem fields = [ 'title', 'alias', 'time_limit', 'memory_limit', 'description', 'input', 'output', 'hint', 'source' ] error_messages = {} widgets = { 'description': forms.Textarea(attrs={'class': 'markdown'}), 'input': forms.Textarea(attrs={'class': 'markdown'}), 'output': forms.Textarea(attrs={'class': 'markdown'}), 'hint': forms.Textarea(attrs={'class': 'markdown'}), } tags = CommaSeparatedMultipleChoiceField(required=False) def __init__(self, *args, **kwargs): super(ProblemEditForm, self).__init__(*args, **kwargs) new_order = [ 'title', 'alias', 'time_limit', 'memory_limit', 'description', 'input', 'output', 'hint', 'tags', 'source' ] self.fields = type(self.fields)((k, self.fields[k]) for k in new_order) if self.instance: self.fields['tags'].initial = ','.join(map(str, self.instance.tags)) self.fields['tags'].choices = [(i, i) for i in Tag.objects.all()] def clean(self): cleaned_data = super().clean() used_tag = [] if 'tags' in cleaned_data: for tag in cleaned_data['tags']: if Tag.objects.filter(name=tag).exists(): used_tag.append(tag) cleaned_data['tags'] = ', '.join(used_tag) if ',' not in cleaned_data['tags']: cleaned_data['tags'] = "\"%s\"" % cleaned_data['tags'] return cleaned_data