@@ -607,20 +607,97 @@ def update_user_role(instance, user):
607607
608608
609609class RePasswordSerializer (serializers .Serializer ):
610- password = serializers .CharField (required = True , label = _ ("Password" ),
611- validators = [validators .RegexValidator (regex = re .compile (
612- "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z_!@#$%^&*`~.()-+=]+$)(?![a-z0-9]+$)(?![a-z_!@#$%^&*`~()-+=]+$)"
613- "(?![0-9_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9_!@#$%^&*`~.()-+=]{6,20}$" )
614- , message = _ (
615- "The confirmation password must be 6-20 characters long and must be a combination of letters, numbers, and special characters." ))])
616-
617- re_password = serializers .CharField (required = True , label = _ ("Confirm Password" ),
618- validators = [validators .RegexValidator (regex = re .compile (
619- "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z_!@#$%^&*`~.()-+=]+$)(?![a-z0-9]+$)(?![a-z_!@#$%^&*`~()-+=]+$)"
620- "(?![0-9_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9_!@#$%^&*`~.()-+=]{6,20}$" )
621- , message = _ (
622- "The confirmation password must be 6-20 characters long and must be a combination of letters, numbers, and special characters." ))]
623- )
610+ email = serializers .EmailField (
611+ required = True ,
612+ label = _ ("Email" ),
613+ validators = [validators .EmailValidator (message = ExceptionCodeConstants .EMAIL_FORMAT_ERROR .value .message ,
614+ code = ExceptionCodeConstants .EMAIL_FORMAT_ERROR .value .code )])
615+
616+ code = serializers .CharField (required = True , label = _ ("Code" ))
617+ password = serializers .CharField (
618+ required = True ,
619+ label = _ ("Password" ),
620+ max_length = 20 ,
621+ min_length = 6 ,
622+ validators = [
623+ validators .RegexValidator (
624+ regex = PASSWORD_REGEX ,
625+ message = _ (
626+ "The password must be 6-20 characters long and must be a combination of letters, numbers, and special characters."
627+ )
628+ )
629+ ]
630+ )
631+ re_password = serializers .CharField (
632+ required = True ,
633+ label = _ ("Re Password" ),
634+ validators = [
635+ validators .RegexValidator (
636+ regex = PASSWORD_REGEX ,
637+ message = _ (
638+ "The confirmation password must be 6-20 characters long and must be a combination of letters, numbers, and special characters."
639+ )
640+ )
641+ ]
642+ )
643+
644+ class Meta :
645+ model = User
646+ fields = '__all__'
647+
648+ def is_valid (self , * , raise_exception = False ):
649+ super ().is_valid (raise_exception = True )
650+ email = self .data .get ("email" )
651+ cache_code = cache .get (get_key (email + ':reset_password' ), version = version )
652+ if self .data .get ('password' ) != self .data .get ('re_password' ):
653+ raise AppApiException (ExceptionCodeConstants .PASSWORD_NOT_EQ_RE_PASSWORD .value .code ,
654+ ExceptionCodeConstants .PASSWORD_NOT_EQ_RE_PASSWORD .value .message )
655+ if cache_code != self .data .get ('code' ):
656+ raise AppApiException (ExceptionCodeConstants .CODE_ERROR .value .code ,
657+ ExceptionCodeConstants .CODE_ERROR .value .message )
658+ return True
659+
660+ def reset_password (self ):
661+ """
662+ 修改密码
663+ :return: 是否成功
664+ """
665+ if self .is_valid ():
666+ email = self .data .get ("email" )
667+ QuerySet (User ).filter (email = email ).update (
668+ password = password_encrypt (self .data .get ('password' )))
669+ code_cache_key = email + ":reset_password"
670+ cache .delete (get_key (code_cache_key ), version = version )
671+ return True
672+
673+
674+ class ResetCurrentUserPassword (serializers .Serializer ):
675+ password = serializers .CharField (
676+ required = True ,
677+ label = _ ("Password" ),
678+ max_length = 20 ,
679+ min_length = 6 ,
680+ validators = [
681+ validators .RegexValidator (
682+ regex = PASSWORD_REGEX ,
683+ message = _ (
684+ "The password must be 6-20 characters long and must be a combination of letters, numbers, and special characters."
685+ )
686+ )
687+ ]
688+ )
689+ re_password = serializers .CharField (
690+ required = True ,
691+ label = _ ("Re Password" ),
692+ validators = [
693+ validators .RegexValidator (
694+ regex = PASSWORD_REGEX ,
695+ message = _ (
696+ "The confirmation password must be 6-20 characters long and must be a combination of letters, numbers, and special characters."
697+ )
698+ )
699+ ]
700+ )
624701
625702 class Meta :
626703 model = User
0 commit comments