| 965 | |
| 966 | |
| 967 | class RePasswordSerializer(serializers.Serializer): |
| 968 | email = serializers.EmailField( |
| 969 | required=True, |
| 970 | label=_("Email"), |
| 971 | validators=[validators.EmailValidator(message=ExceptionCodeConstants.EMAIL_FORMAT_ERROR.value.message, |
| 972 | code=ExceptionCodeConstants.EMAIL_FORMAT_ERROR.value.code)]) |
| 973 | |
| 974 | code = serializers.CharField(required=True, label=_("Code")) |
| 975 | password = serializers.CharField( |
| 976 | required=True, |
| 977 | label=_("Password"), |
| 978 | max_length=20, |
| 979 | min_length=6, |
| 980 | validators=[ |
| 981 | validators.RegexValidator( |
| 982 | regex=PASSWORD_REGEX, |
| 983 | message=_( |
| 984 | "The password must be 6-20 characters long and must be a combination of letters, numbers, and special characters." |
| 985 | ) |
| 986 | ) |
| 987 | ] |
| 988 | ) |
| 989 | re_password = serializers.CharField( |
| 990 | required=True, |
| 991 | label=_("Re Password"), |
| 992 | validators=[ |
| 993 | validators.RegexValidator( |
| 994 | regex=PASSWORD_REGEX, |
| 995 | message=_( |
| 996 | "The confirmation password must be 6-20 characters long and must be a combination of letters, numbers, and special characters." |
| 997 | ) |
| 998 | ) |
| 999 | ] |
| 1000 | ) |
| 1001 | |
| 1002 | class Meta: |
| 1003 | model = User |
| 1004 | fields = '__all__' |
| 1005 | |
| 1006 | def is_valid(self, *, raise_exception=False): |
| 1007 | super().is_valid(raise_exception=True) |
| 1008 | email = self.data.get("email") |
| 1009 | cache_code = cache.get(get_key(email + ':reset_password'), version=version) |
| 1010 | if self.data.get('password') != self.data.get('re_password'): |
| 1011 | raise AppApiException(ExceptionCodeConstants.PASSWORD_NOT_EQ_RE_PASSWORD.value.code, |
| 1012 | ExceptionCodeConstants.PASSWORD_NOT_EQ_RE_PASSWORD.value.message) |
| 1013 | if cache_code != self.data.get('code'): |
| 1014 | raise AppApiException(ExceptionCodeConstants.CODE_ERROR.value.code, |
| 1015 | ExceptionCodeConstants.CODE_ERROR.value.message) |
| 1016 | return True |
| 1017 | |
| 1018 | def reset_password(self): |
| 1019 | """ |
| 1020 | 修改密码 |
| 1021 | :return: 是否成功 |
| 1022 | """ |
| 1023 | if self.is_valid(): |
| 1024 | email = self.data.get("email") |