OnlineJudge/problem/serializers.py

111 lines
3.8 KiB
Python
Raw Normal View History

2017-01-25 16:29:00 +08:00
from django import forms
2017-02-02 16:59:15 +08:00
from judge.languages import language_names, spj_language_names
from utils.api import DateTimeTZField, UsernameSerializer, serializers
from .models import Problem, ProblemRuleType, ProblemTag
2017-01-25 16:29:00 +08:00
class TestCaseUploadForm(forms.Form):
2017-01-25 16:39:16 +08:00
spj = forms.CharField(max_length=12)
2017-01-25 16:29:00 +08:00
file = forms.FileField()
2017-02-02 16:59:15 +08:00
class CreateSampleSerializer(serializers.Serializer):
input = serializers.CharField()
output = serializers.CharField()
class CreateTestCaseScoreSerializer(serializers.Serializer):
input_name = serializers.CharField(max_length=32)
2017-02-04 00:22:54 +08:00
output_name = serializers.CharField(max_length=32)
2017-02-02 16:59:15 +08:00
score = serializers.IntegerField(min_value=0)
2017-02-06 15:46:17 +08:00
class CreateProblemCodeTemplateSerializer(serializers.Serializer):
pass
2017-02-02 16:59:15 +08:00
class Difficulty(object):
LOW = "Low"
MID = "Mid"
HIGH = "High"
2017-02-04 00:10:44 +08:00
class CreateOrEditProblemSerializer(serializers.Serializer):
2017-02-09 16:47:08 +08:00
_id = serializers.CharField(max_length=32, allow_blank=True, allow_null=True)
2017-02-02 16:59:15 +08:00
title = serializers.CharField(max_length=128)
description = serializers.CharField()
input_description = serializers.CharField()
output_description = serializers.CharField()
2017-02-04 12:23:36 +08:00
samples = serializers.ListField(child=CreateSampleSerializer(), allow_empty=False)
2017-02-10 15:13:03 +08:00
test_case_id = serializers.CharField(min_length=32, max_length=32)
2017-02-04 12:23:36 +08:00
test_case_score = serializers.ListField(child=CreateTestCaseScoreSerializer(), allow_empty=False)
2017-02-02 16:59:15 +08:00
time_limit = serializers.IntegerField(min_value=1, max_value=1000 * 60)
memory_limit = serializers.IntegerField(min_value=1, max_value=1024)
2017-02-06 15:46:17 +08:00
languages = serializers.MultipleChoiceField(choices=language_names)
template = serializers.DictField(child=serializers.CharField(min_length=1))
2017-02-02 16:59:15 +08:00
rule_type = serializers.ChoiceField(choices=[ProblemRuleType.ACM, ProblemRuleType.OI])
spj = serializers.BooleanField()
2017-02-04 00:10:44 +08:00
spj_language = serializers.ChoiceField(choices=spj_language_names, allow_blank=True, allow_null=True)
spj_code = serializers.CharField(allow_blank=True, allow_null=True)
2017-02-02 16:59:15 +08:00
visible = serializers.BooleanField()
difficulty = serializers.ChoiceField(choices=[Difficulty.LOW, Difficulty.MID, Difficulty.HIGH])
2017-02-04 12:23:36 +08:00
tags = serializers.ListField(child=serializers.CharField(max_length=32), allow_empty=False)
2017-02-04 00:10:44 +08:00
hint = serializers.CharField(allow_blank=True, allow_null=True)
source = serializers.CharField(max_length=256, allow_blank=True, allow_null=True)
class CreateProblemSerializer(CreateOrEditProblemSerializer):
pass
class EditProblemSerializer(CreateOrEditProblemSerializer):
id = serializers.IntegerField()
2017-02-02 16:59:15 +08:00
2017-02-17 20:14:03 +08:00
class CreateContestProblemSerializer(CreateOrEditProblemSerializer):
contest_id = serializers.IntegerField()
2017-02-02 16:59:15 +08:00
class TagSerializer(serializers.ModelSerializer):
class Meta:
model = ProblemTag
2017-10-06 17:46:14 +08:00
fields = "__all__"
2017-02-02 16:59:15 +08:00
2017-08-23 17:01:55 +08:00
class BaseProblemSerializer(serializers.ModelSerializer):
2017-02-02 16:59:15 +08:00
samples = serializers.JSONField()
test_case_score = serializers.JSONField()
languages = serializers.JSONField()
2017-02-06 15:46:17 +08:00
template = serializers.JSONField()
2017-02-03 23:56:56 +08:00
tags = serializers.SlugRelatedField(many=True, slug_field="name", read_only=True)
2017-02-02 16:59:15 +08:00
create_time = DateTimeTZField()
last_update_time = DateTimeTZField()
created_by = UsernameSerializer()
2017-08-16 15:33:27 +08:00
statistic_info = serializers.JSONField()
2017-02-02 16:59:15 +08:00
2017-08-23 17:01:55 +08:00
class ProblemAdminSerializer(BaseProblemSerializer):
2017-02-02 16:59:15 +08:00
class Meta:
model = Problem
2017-10-06 17:46:14 +08:00
fields = "__all__"
2017-08-23 17:01:55 +08:00
class ContestProblemAdminSerializer(BaseProblemSerializer):
class Meta:
2017-09-24 09:48:17 +08:00
model = Problem
2017-10-16 09:45:29 +08:00
fields = "__all__"
2017-08-23 17:01:55 +08:00
class ProblemSerializer(BaseProblemSerializer):
class Meta:
model = Problem
2017-09-24 09:48:17 +08:00
exclude = ("contest", "test_case_score", "test_case_id", "visible", "is_public")
2017-08-23 17:01:55 +08:00
2017-08-23 17:01:55 +08:00
class ContestProblemSerializer(BaseProblemSerializer):
class Meta:
2017-09-24 09:48:17 +08:00
model = Problem
2017-08-23 17:01:55 +08:00
exclude = ("test_case_score", "test_case_id", "visible", "is_public")