class ProblemSerializer(BaseProblemSerializer): # 问题序列化器 template = serializers.SerializerMethodField("get_public_template") class Meta: model = Problem exclude = ("test_case_score", "test_case_id", "visible", "is_public", "spj_code", "spj_version", "spj_compile_ok")
class ProblemSafeSerializer(BaseProblemSerializer): # 问题安全序列化器 template = serializers.SerializerMethodField("get_public_template") class Meta: model = Problem exclude = ("test_case_score", "test_case_id", "visible", "is_public", "spj_code", "spj_version", "spj_compile_ok", "difficulty", "submission_number", "accepted_number", "statistic_info")
class UserAdminSerializer(serializers.ModelSerializer): real_name = serializers.SerializerMethodField() class Meta: model = User fields = ["id", "username", "email", "admin_type", "problem_permission", "real_name", "create_time", "last_login", "two_factor_auth", "open_api", "is_disabled"] def get_real_name(self, obj): return obj.userprofile.real_name
class UserProfileSerializer(serializers.ModelSerializer): user = UserSerializer() real_name = serializers.SerializerMethodField() class Meta: model = UserProfile fields = "__all__" def __init__(self, *args, **kwargs): self.show_real_name = kwargs.pop("show_real_name", False) super(UserProfileSerializer, self).__init__(*args, **kwargs) def get_real_name(self, obj): return obj.real_name if self.show_real_name else None
class ACMContestRankSerializer(serializers.ModelSerializer): # 比赛排名 user = serializers.SerializerMethodField() class Meta: model = ACMContestRank fields = "__all__" def __init__(self, *args, **kwargs): self.is_contest_admin = kwargs.pop("is_contest_admin", False) super().__init__(*args, **kwargs) def get_user(self, obj): return UsernameSerializer(obj.user, need_real_name=self.is_contest_admin).data
class SubmissionListSerializer(serializers.ModelSerializer): # 提交信息列表序列化器 problem = serializers.SlugRelatedField(read_only=True, slug_field="_id") show_link = serializers.SerializerMethodField() def __init__(self, *args, **kwargs): self.user = kwargs.pop("user", None) super().__init__(*args, **kwargs) class Meta: model = Submission exclude = ("info", "contest", "code", "ip") def get_show_link(self, obj): # 没传user或为匿名user if self.user is None or not self.user.is_authenticated(): return False return obj.check_user_permission(self.user)
class ExportProblemSerializer(serializers.ModelSerializer): # 导出问题:id、描述、输入输出描述、测试样例分数、提示、特殊评判、通过模板、来源、标签 display_id = serializers.SerializerMethodField() 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): # 获得显示id return obj._id def _html_format_value(self, value): # HTML格式的值 return {"format": "html", "value": value} def get_description(self, obj): # 获得描述 return self._html_format_value(obj.description) def get_input_description(self, obj): # 获得输入描述 return self._html_format_value(obj.input_description) def get_output_description(self, obj): # 获取输出描述 return self._html_format_value(obj.output_description) def get_hint(self, obj): # 获取提示信息 return self._html_format_value(obj.hint) 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] def get_spj(self, obj): # 获取特殊评判的测试 return {"code": obj.spj_code, "language": obj.spj_language} if obj.spj else None 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}" class Meta: model = Problem fields = ("display_id", "title", "description", "tags", "input_description", "output_description", "test_case_score", "hint", "time_limit", "memory_limit", "samples", "template", "spj", "rule_type", "source", "template")