1212import re
1313from collections import defaultdict
1414from itertools import product
15+
16+ from django .core .cache import cache
1517from django .core .mail .backends .smtp import EmailBackend
1618from django .db import transaction
1719from django .db .models import Q , QuerySet
1820from rest_framework import serializers
1921import uuid_utils .compat as uuid
22+
23+ from common .constants .cache_version import Cache_Version
2024from common .constants .exception_code_constants import ExceptionCodeConstants
2125from common .constants .permission_constants import RoleConstants , Auth
2226from common .database_model_manage .database_model_manage import DatabaseModelManage
3640 r"(?![0-9_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9_!@#$%^&*`~.()-+=]{6,20}$"
3741)
3842
43+ version , get_key = Cache_Version .SYSTEM .value
44+
3945
4046class UserProfileResponse (serializers .ModelSerializer ):
4147 is_edit_password = serializers .BooleanField (required = True , label = _ ('Is Edit Password' ))
@@ -481,14 +487,13 @@ class Meta:
481487 def is_valid (self , * , raise_exception = False ):
482488 super ().is_valid (raise_exception = True )
483489 email = self .data .get ("email" )
484- # TODO 删除缓存
485- # cache_code = user_cache.get(email + ':reset_password')
490+ cache_code = cache .get (get_key (email + ':reset_password' ), version = version )
486491 if self .data .get ('password' ) != self .data .get ('re_password' ):
487492 raise AppApiException (ExceptionCodeConstants .PASSWORD_NOT_EQ_RE_PASSWORD .value .code ,
488493 ExceptionCodeConstants .PASSWORD_NOT_EQ_RE_PASSWORD .value .message )
489- # if cache_code != self.data.get('code'):
490- # raise AppApiException(ExceptionCodeConstants.CODE_ERROR.value.code,
491- # ExceptionCodeConstants.CODE_ERROR.value.message)
494+ if cache_code != self .data .get ('code' ):
495+ raise AppApiException (ExceptionCodeConstants .CODE_ERROR .value .code ,
496+ ExceptionCodeConstants .CODE_ERROR .value .message )
492497 return True
493498
494499 def reset_password (self ):
@@ -502,7 +507,7 @@ def reset_password(self):
502507 password = password_encrypt (self .data .get ('password' )))
503508 code_cache_key = email + ":reset_password"
504509 # 删除验证码缓存
505- # user_cache .delete(code_cache_key)
510+ cache .delete (code_cache_key , version = version )
506511 return True
507512
508513
@@ -531,7 +536,7 @@ def is_valid(self, *, raise_exception=False):
531536 raise ExceptionCodeConstants .EMAIL_IS_EXIST .value .to_app_api_exception ()
532537 code_cache_key = self .data .get ('email' ) + ":" + self .data .get ("type" )
533538 code_cache_key_lock = code_cache_key + "_lock"
534- ttl = None # user_cache .ttl(code_cache_key_lock)
539+ ttl = cache .ttl (code_cache_key_lock )
535540 if ttl is not None :
536541 raise AppApiException (500 , _ ("Do not send emails again within {seconds} seconds" ).format (
537542 seconds = int (ttl .total_seconds ())))
@@ -558,10 +563,10 @@ def send(self):
558563 code_cache_key = email + ":" + state
559564 code_cache_key_lock = code_cache_key + "_lock"
560565 # 设置缓存
561- # user_cache .set(code_cache_key_lock, code, timeout=datetime.timedelta(minutes=1))
566+ cache .set (get_key ( code_cache_key_lock ) , code , timeout = datetime .timedelta (minutes = 1 ), version = version )
562567 system_setting = QuerySet (SystemSetting ).filter (type = SettingType .EMAIL .value ).first ()
563568 if system_setting is None :
564- # user_cache .delete(code_cache_key_lock)
569+ cache .delete (get_key ( code_cache_key_lock ), version = version )
565570 raise AppApiException (1004 ,
566571 _ ("The email service has not been set up. Please contact the administrator to set up the email service in [Email Settings]." ))
567572 try :
@@ -581,9 +586,9 @@ def send(self):
581586 from_email = system_setting .meta .get ('from_email' ),
582587 recipient_list = [email ], fail_silently = False , connection = connection )
583588 except Exception as e :
584- # user_cache .delete(code_cache_key_lock)
589+ cache .delete (get_key ( code_cache_key_lock ) )
585590 raise AppApiException (500 , f"{ str (e )} " + _ ("Email sending failed" ))
586- # user_cache .set(code_cache_key, code, timeout=datetime.timedelta(minutes=30))
591+ cache .set (get_key ( code_cache_key ) , code , timeout = datetime .timedelta (minutes = 30 ), version = version )
587592 return True
588593
589594
@@ -609,8 +614,7 @@ class CheckCodeSerializer(serializers.Serializer):
609614
610615 def is_valid (self , * , raise_exception = False ):
611616 super ().is_valid ()
612- #TODO 这里的缓存 需要重新设计
613- value = None #user_cache.get(self.data.get("email") + ":" + self.data.get("type"))
617+ value = cache .get (get_key (self .data .get ("email" ) + ":" + self .data .get ("type" )), version = version )
614618 if value is None or value != self .data .get ("code" ):
615619 raise ExceptionCodeConstants .CODE_ERROR .value .to_app_api_exception ()
616620 return True
0 commit comments