OnlineJudge/account/serializers.py

144 lines
5.1 KiB
Python
Raw Normal View History

2017-04-30 13:58:34 +00:00
from django import forms
2017-12-01 09:19:31 +00:00
from utils.api import serializers, UsernameSerializer
2017-04-30 13:58:34 +00:00
from .models import AdminType, ProblemPermission, User, UserProfile
class UserLoginSerializer(serializers.Serializer):
2017-10-01 21:16:14 +00:00
username = serializers.CharField()
password = serializers.CharField()
2017-10-27 10:36:29 +00:00
tfa_code = serializers.CharField(required=False, allow_blank=True)
2017-08-19 09:25:39 +00:00
class UsernameOrEmailCheckSerializer(serializers.Serializer):
2017-10-01 21:16:14 +00:00
username = serializers.CharField(required=False)
email = serializers.EmailField(required=False)
2017-08-19 09:25:39 +00:00
2017-08-20 00:35:59 +00:00
2015-08-03 10:45:26 +00:00
class UserRegisterSerializer(serializers.Serializer):
2017-10-01 21:16:14 +00:00
username = serializers.CharField(max_length=32)
password = serializers.CharField(min_length=6)
email = serializers.EmailField(max_length=64)
captcha = serializers.CharField()
2015-08-03 10:45:26 +00:00
class UserChangePasswordSerializer(serializers.Serializer):
2015-08-16 04:14:56 +00:00
old_password = serializers.CharField()
2017-10-01 21:16:14 +00:00
new_password = serializers.CharField(min_length=6)
2017-10-27 10:36:29 +00:00
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)
2017-11-18 00:07:03 +00:00
class GenerateUserSerializer(serializers.Serializer):
prefix = serializers.CharField(max_length=16, allow_blank=True)
suffix = serializers.CharField(max_length=16, allow_blank=True)
number_from = serializers.IntegerField()
number_to = serializers.IntegerField()
password_length = serializers.IntegerField(max_value=16, default=8)
2017-11-18 00:07:03 +00:00
2017-11-22 12:06:16 +00:00
class ImportUserSeralizer(serializers.Serializer):
users = serializers.ListField(
child=serializers.ListField(child=serializers.CharField(max_length=64)))
2017-11-30 03:35:48 +00:00
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 UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["id", "username", "email", "admin_type", "problem_permission",
"create_time", "last_login", "two_factor_auth", "open_api", "is_disabled"]
2017-04-30 13:58:34 +00:00
class UserProfileSerializer(serializers.ModelSerializer):
2017-07-04 02:26:02 +00:00
user = UserSerializer()
real_name = serializers.SerializerMethodField()
2017-04-30 13:58:34 +00:00
class Meta:
model = UserProfile
2017-10-06 09:46:14 +00:00
fields = "__all__"
2017-04-30 13:58:34 +00:00
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
2017-04-30 13:58:34 +00:00
class EditUserSerializer(serializers.Serializer):
id = serializers.IntegerField()
2017-10-01 21:16:14 +00:00
username = serializers.CharField(max_length=32)
real_name = serializers.CharField(max_length=32, allow_blank=True, allow_null=True)
2017-10-01 21:16:14 +00:00
password = serializers.CharField(min_length=6, allow_blank=True, required=False, default=None)
email = serializers.EmailField(max_length=64)
2016-10-29 18:17:35 +00:00
admin_type = serializers.ChoiceField(choices=(AdminType.REGULAR_USER, AdminType.ADMIN, AdminType.SUPER_ADMIN))
2017-02-10 02:38:32 +00:00
problem_permission = serializers.ChoiceField(choices=(ProblemPermission.NONE, ProblemPermission.OWN,
ProblemPermission.ALL))
open_api = serializers.BooleanField()
two_factor_auth = serializers.BooleanField()
is_disabled = serializers.BooleanField()
2017-04-18 03:57:57 +00:00
2017-04-30 13:58:34 +00:00
class EditUserProfileSerializer(serializers.Serializer):
real_name = serializers.CharField(max_length=32, allow_null=True, required=False)
2017-12-01 09:19:31 +00:00
avatar = serializers.CharField(max_length=256, allow_blank=True, required=False)
blog = serializers.URLField(max_length=256, allow_blank=True, required=False)
mood = serializers.CharField(max_length=256, allow_blank=True, required=False)
2021-08-02 04:48:26 +00:00
github = serializers.URLField(max_length=256, allow_blank=True, required=False)
2017-12-01 09:19:31 +00:00
school = serializers.CharField(max_length=64, allow_blank=True, required=False)
major = serializers.CharField(max_length=64, allow_blank=True, required=False)
2018-07-15 04:43:44 +00:00
language = serializers.CharField(max_length=32, allow_blank=True, required=False)
2017-04-30 13:58:34 +00:00
2017-04-18 03:57:57 +00:00
class ApplyResetPasswordSerializer(serializers.Serializer):
email = serializers.EmailField()
2017-10-01 21:16:14 +00:00
captcha = serializers.CharField()
2017-04-18 07:19:26 +00:00
class ResetPasswordSerializer(serializers.Serializer):
2017-10-01 21:16:14 +00:00
token = serializers.CharField()
password = serializers.CharField(min_length=6)
captcha = serializers.CharField()
2017-04-18 07:19:26 +00:00
class SSOSerializer(serializers.Serializer):
2017-10-01 21:16:14 +00:00
token = serializers.CharField()
2017-04-18 07:19:26 +00:00
class TwoFactorAuthCodeSerializer(serializers.Serializer):
code = serializers.IntegerField()
2017-04-30 13:58:34 +00:00
class ImageUploadForm(forms.Form):
image = forms.FileField()
2017-08-20 12:32:07 +00:00
2019-03-12 06:40:47 +00:00
class FileUploadForm(forms.Form):
file = forms.FileField()
2017-08-20 12:32:07 +00:00
class RankInfoSerializer(serializers.ModelSerializer):
user = UsernameSerializer()
class Meta:
model = UserProfile
2017-12-01 09:19:31 +00:00
fields = "__all__"