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
|
2015-06-29 04:43:17 +00:00
|
|
|
|
2017-04-30 13:58:34 +00:00
|
|
|
from .models import AdminType, ProblemPermission, User, UserProfile
|
2015-08-07 12:25:58 +00:00
|
|
|
|
2015-06-29 04:43:17 +00:00
|
|
|
|
|
|
|
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)
|
2015-08-03 08:52:31 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-08-03 11:54:55 +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)
|
2015-08-03 11:54:55 +00:00
|
|
|
|
2015-08-07 12:25:58 +00:00
|
|
|
|
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()
|
2017-11-24 15:30:17 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2015-08-07 12:25:58 +00:00
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2017-09-12 03:45:17 +00:00
|
|
|
fields = ["id", "username", "email", "admin_type", "problem_permission",
|
2016-06-23 04:19:16 +00:00
|
|
|
"create_time", "last_login", "two_factor_auth", "open_api", "is_disabled"]
|
2015-08-07 12:25:58 +00:00
|
|
|
|
|
|
|
|
2017-04-30 13:58:34 +00:00
|
|
|
class UserProfileSerializer(serializers.ModelSerializer):
|
2017-07-04 02:26:02 +00:00
|
|
|
user = UserSerializer()
|
2017-12-10 01:58:08 +00:00
|
|
|
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
|
|
|
|
2017-12-10 01:58:08 +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
|
|
|
|
2015-08-07 12:25:58 +00:00
|
|
|
class EditUserSerializer(serializers.Serializer):
|
|
|
|
id = serializers.IntegerField()
|
2017-10-01 21:16:14 +00:00
|
|
|
username = serializers.CharField(max_length=32)
|
2018-01-04 12:13:50 +00:00
|
|
|
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))
|
2016-06-23 04:19:16 +00:00
|
|
|
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):
|
2017-11-06 11:05:21 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2017-11-01 14:25:14 +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__"
|