OnlineJudge/submission/serializers.py
2018-05-17 01:22:18 +08:00

52 lines
1.6 KiB
Python

from .models import Submission
from utils.api import serializers
from judge.languages import language_names
class CreateSubmissionSerializer(serializers.Serializer):
problem_id = serializers.IntegerField()
language = serializers.ChoiceField(choices=language_names)
code = serializers.CharField(max_length=1024 * 1024)
contest_id = serializers.IntegerField(required=False)
captcha = serializers.CharField(required=False)
class ShareSubmissionSerializer(serializers.Serializer):
id = serializers.CharField()
shared = serializers.BooleanField()
class SubmissionModelSerializer(serializers.ModelSerializer):
class Meta:
model = Submission
fields = "__all__"
# 不显示submission info的serializer, 用于ACM rule_type
class SubmissionSafeModelSerializer(serializers.ModelSerializer):
problem = serializers.SlugRelatedField(read_only=True, slug_field="_id")
class Meta:
model = Submission
exclude = ("info", "contest", "ip")
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)