OnlineJudge/problem/serializers.py

278 lines
10 KiB
Python
Raw Normal View History

import re
2017-01-25 16:29:00 +08:00
from django import forms
from options.options import SysOptions
2017-12-01 17:19:31 +08:00
from utils.api import UsernameSerializer, serializers
from utils.constants import Difficulty
from utils.serializers import LanguageNameMultiChoiceField, SPJLanguageNameChoiceField, LanguageNameChoiceField
2017-02-02 16:59:15 +08:00
from .models import Problem, ProblemRuleType, ProblemTag, ProblemIOMode
2017-11-26 13:42:38 +08:00
from .utils import parse_problem_template
2017-02-02 16:59:15 +08:00
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(trim_whitespace=False)
output = serializers.CharField(trim_whitespace=False)
2017-02-02 16:59:15 +08:00
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
class ProblemIOModeSerializer(serializers.Serializer):
io_mode = serializers.ChoiceField(choices=ProblemIOMode.choices())
input = serializers.CharField()
output = serializers.CharField()
def validate(self, attrs):
if attrs["input"] == attrs["output"]:
raise serializers.ValidationError("Invalid io mode")
for item in (attrs["input"], attrs["output"]):
if not re.match("^[a-zA-Z0-9.]+$", item):
raise serializers.ValidationError("Invalid io file name format")
return attrs
2019-03-26 10:51:49 +08:00
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)
2018-05-17 01:22:18 +08:00
title = serializers.CharField(max_length=1024)
2017-02-02 16:59:15 +08:00
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)
test_case_id = serializers.CharField(max_length=32)
test_case_score = serializers.ListField(child=CreateTestCaseScoreSerializer(), allow_empty=True)
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)
languages = LanguageNameMultiChoiceField()
2017-02-06 15:46:17 +08:00
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])
io_mode = ProblemIOModeSerializer()
2017-02-02 16:59:15 +08:00
spj = serializers.BooleanField()
spj_language = SPJLanguageNameChoiceField(allow_blank=True, allow_null=True)
2017-02-04 00:10:44 +08:00
spj_code = serializers.CharField(allow_blank=True, allow_null=True)
2017-11-18 08:07:03 +08:00
spj_compile_ok = serializers.BooleanField(default=False)
2017-02-02 16:59:15 +08:00
visible = serializers.BooleanField()
difficulty = serializers.ChoiceField(choices=Difficulty.choices())
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)
2019-03-13 17:57:41 +08:00
share_submission = serializers.BooleanField()
2017-02-04 00:10:44 +08:00
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()
class EditContestProblemSerializer(CreateOrEditProblemSerializer):
id = serializers.IntegerField()
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-11-16 22:12:17 +08:00
class CompileSPJSerializer(serializers.Serializer):
spj_language = SPJLanguageNameChoiceField()
2017-11-16 22:12:17 +08:00
spj_code = serializers.CharField()
2017-08-23 17:01:55 +08:00
class BaseProblemSerializer(serializers.ModelSerializer):
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
created_by = UsernameSerializer()
def get_public_template(self, obj):
ret = {}
for lang, code in obj.template.items():
ret[lang] = parse_problem_template(code)["template"]
return ret
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 ProblemSerializer(BaseProblemSerializer):
template = serializers.SerializerMethodField("get_public_template")
2017-11-26 13:42:38 +08:00
2017-08-23 17:01:55 +08:00
class Meta:
model = Problem
2017-12-01 17:19:31 +08:00
exclude = ("test_case_score", "test_case_id", "visible", "is_public",
"spj_code", "spj_version", "spj_compile_ok")
2017-10-27 18:36:29 +08:00
2017-12-01 17:19:31 +08:00
class ProblemSafeSerializer(BaseProblemSerializer):
template = serializers.SerializerMethodField("get_public_template")
2017-10-27 18:36:29 +08:00
class Meta:
model = Problem
2017-12-01 17:19:31 +08:00
exclude = ("test_case_score", "test_case_id", "visible", "is_public",
"spj_code", "spj_version", "spj_compile_ok",
"difficulty", "submission_number", "accepted_number", "statistic_info")
2017-11-07 19:04:41 +08:00
class ContestProblemMakePublicSerializer(serializers.Serializer):
id = serializers.IntegerField()
display_id = serializers.CharField(max_length=32)
2017-11-26 13:42:38 +08:00
class ExportProblemSerializer(serializers.ModelSerializer):
display_id = serializers.SerializerMethodField()
2017-11-26 13:42:38 +08:00
description = serializers.SerializerMethodField()
input_description = serializers.SerializerMethodField()
output_description = serializers.SerializerMethodField()
test_case_score = serializers.SerializerMethodField()
hint = serializers.SerializerMethodField()
spj = serializers.SerializerMethodField()
template = serializers.SerializerMethodField()
source = serializers.SerializerMethodField()
tags = serializers.SlugRelatedField(many=True, slug_field="name", read_only=True)
def get_display_id(self, obj):
return obj._id
def _html_format_value(self, value):
return {"format": "html", "value": value}
2017-11-26 13:42:38 +08:00
def get_description(self, obj):
return self._html_format_value(obj.description)
2017-11-26 13:42:38 +08:00
def get_input_description(self, obj):
return self._html_format_value(obj.input_description)
2017-11-26 13:42:38 +08:00
def get_output_description(self, obj):
return self._html_format_value(obj.output_description)
2017-11-26 13:42:38 +08:00
def get_hint(self, obj):
return self._html_format_value(obj.hint)
2017-11-26 13:42:38 +08:00
def get_test_case_score(self, obj):
return [{"score": item["score"] if obj.rule_type == ProblemRuleType.OI else 100,
"input_name": item["input_name"], "output_name": item["output_name"]}
for item in obj.test_case_score]
2017-11-26 13:42:38 +08:00
def get_spj(self, obj):
return {"code": obj.spj_code,
"language": obj.spj_language} if obj.spj else None
2017-11-26 13:42:38 +08:00
def get_template(self, obj):
ret = {}
for k, v in obj.template.items():
ret[k] = parse_problem_template(v)
return ret
def get_source(self, obj):
return obj.source or f"{SysOptions.website_name} {SysOptions.website_base_url}"
2017-11-26 13:42:38 +08:00
class Meta:
model = Problem
fields = ("display_id", "title", "description", "tags",
2017-11-26 13:42:38 +08:00
"input_description", "output_description",
"test_case_score", "hint", "time_limit", "memory_limit", "samples",
"template", "spj", "rule_type", "source", "template")
class AddContestProblemSerializer(serializers.Serializer):
contest_id = serializers.IntegerField()
problem_id = serializers.IntegerField()
display_id = serializers.CharField()
class ExportProblemRequestSerialzier(serializers.Serializer):
problem_id = serializers.ListField(child=serializers.IntegerField(), allow_empty=False)
class UploadProblemForm(forms.Form):
file = forms.FileField()
class FormatValueSerializer(serializers.Serializer):
format = serializers.ChoiceField(choices=["html", "markdown"])
value = serializers.CharField(allow_blank=True)
class TestCaseScoreSerializer(serializers.Serializer):
score = serializers.IntegerField(min_value=1)
input_name = serializers.CharField(max_length=32)
output_name = serializers.CharField(max_length=32)
class TemplateSerializer(serializers.Serializer):
prepend = serializers.CharField()
template = serializers.CharField()
append = serializers.CharField()
class SPJSerializer(serializers.Serializer):
code = serializers.CharField()
language = SPJLanguageNameChoiceField()
class AnswerSerializer(serializers.Serializer):
code = serializers.CharField()
language = LanguageNameChoiceField()
class ImportProblemSerializer(serializers.Serializer):
display_id = serializers.CharField(max_length=128)
title = serializers.CharField(max_length=128)
description = FormatValueSerializer()
input_description = FormatValueSerializer()
output_description = FormatValueSerializer()
hint = FormatValueSerializer()
test_case_score = serializers.ListField(child=TestCaseScoreSerializer(), allow_null=True)
time_limit = serializers.IntegerField(min_value=1, max_value=60000)
memory_limit = serializers.IntegerField(min_value=1, max_value=10240)
samples = serializers.ListField(child=CreateSampleSerializer())
template = serializers.DictField(child=TemplateSerializer())
spj = SPJSerializer(allow_null=True)
rule_type = serializers.ChoiceField(choices=ProblemRuleType.choices())
source = serializers.CharField(max_length=200, allow_blank=True, allow_null=True)
answers = serializers.ListField(child=AnswerSerializer())
tags = serializers.ListField(child=serializers.CharField())
class FPSProblemSerializer(serializers.Serializer):
class UnitSerializer(serializers.Serializer):
unit = serializers.ChoiceField(choices=["MB", "s", "ms"])
value = serializers.IntegerField(min_value=1, max_value=60000)
title = serializers.CharField(max_length=128)
description = serializers.CharField()
input = serializers.CharField()
output = serializers.CharField()
hint = serializers.CharField(allow_blank=True, allow_null=True)
time_limit = UnitSerializer()
memory_limit = UnitSerializer()
samples = serializers.ListField(child=CreateSampleSerializer())
source = serializers.CharField(max_length=200, allow_blank=True, allow_null=True)
spj = SPJSerializer(allow_null=True)
template = serializers.ListField(child=serializers.DictField(), allow_empty=True, allow_null=True)
append = serializers.ListField(child=serializers.DictField(), allow_empty=True, allow_null=True)
prepend = serializers.ListField(child=serializers.DictField(), allow_empty=True, allow_null=True)