OnlineJudge/account/serializers.py

118 lines
4.1 KiB
Python

from django import forms
from utils.api import DateTimeTZField, serializers, UsernameSerializer
from .models import AdminType, ProblemPermission, User, UserProfile
class UserLoginSerializer(serializers.Serializer):
username = serializers.CharField()
password = serializers.CharField()
tfa_code = serializers.CharField(required=False, allow_blank=True)
class UsernameOrEmailCheckSerializer(serializers.Serializer):
username = serializers.CharField(required=False)
email = serializers.EmailField(required=False)
class UserRegisterSerializer(serializers.Serializer):
username = serializers.CharField(max_length=32)
password = serializers.CharField(min_length=6)
email = serializers.EmailField(max_length=64)
captcha = serializers.CharField()
class UserChangePasswordSerializer(serializers.Serializer):
old_password = serializers.CharField()
new_password = serializers.CharField(min_length=6)
tfa_code = serializers.CharField(required=False, allow_blank=True)
class UserChangeEmailSerializer(serializers.Serializer):
password = serializers.CharField()
new_email = serializers.EmailField(max_length=64)
tfa_code = serializers.CharField(required=False, allow_blank=True)
class UserSerializer(serializers.ModelSerializer):
create_time = DateTimeTZField()
last_login = DateTimeTZField()
class Meta:
model = User
fields = ["id", "username", "email", "admin_type", "problem_permission",
"create_time", "last_login", "two_factor_auth", "open_api", "is_disabled"]
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer()
acm_problems_status = serializers.JSONField()
oi_problems_status = serializers.JSONField()
class Meta:
model = UserProfile
fields = "__all__"
class UserInfoSerializer(serializers.ModelSerializer):
acm_problems_status = serializers.JSONField()
oi_problems_status = serializers.JSONField()
class Meta:
model = UserProfile
class EditUserSerializer(serializers.Serializer):
id = serializers.IntegerField()
username = serializers.CharField(max_length=32)
password = serializers.CharField(min_length=6, allow_blank=True, required=False, default=None)
email = serializers.EmailField(max_length=64)
admin_type = serializers.ChoiceField(choices=(AdminType.REGULAR_USER, AdminType.ADMIN, AdminType.SUPER_ADMIN))
problem_permission = serializers.ChoiceField(choices=(ProblemPermission.NONE, ProblemPermission.OWN,
ProblemPermission.ALL))
open_api = serializers.BooleanField()
two_factor_auth = serializers.BooleanField()
is_disabled = serializers.BooleanField()
class EditUserProfileSerializer(serializers.Serializer):
real_name = serializers.CharField(max_length=32, allow_null=True, required=False)
avatar = serializers.CharField(max_length=256, allow_null=True, allow_blank=True, required=False)
blog = serializers.URLField(max_length=256, allow_null=True, allow_blank=True, required=False)
mood = serializers.CharField(max_length=256, allow_null=True, allow_blank=True, required=False)
github = serializers.CharField(max_length=64, allow_null=True, allow_blank=True, required=False)
school = serializers.CharField(max_length=64, allow_null=True, allow_blank=True, required=False)
major = serializers.CharField(max_length=64, allow_null=True, allow_blank=True, required=False)
class ApplyResetPasswordSerializer(serializers.Serializer):
email = serializers.EmailField()
captcha = serializers.CharField()
class ResetPasswordSerializer(serializers.Serializer):
token = serializers.CharField()
password = serializers.CharField(min_length=6)
captcha = serializers.CharField()
class SSOSerializer(serializers.Serializer):
appkey = serializers.CharField()
token = serializers.CharField()
class TwoFactorAuthCodeSerializer(serializers.Serializer):
code = serializers.IntegerField()
class ImageUploadForm(forms.Form):
image = forms.FileField()
class RankInfoSerializer(serializers.ModelSerializer):
user = UsernameSerializer()
class Meta:
model = UserProfile