问题描述
我有以下课程:
class Password:
def __init__(self,encryptor: AbstractEncryptor,value: str = None):
self._encryptor = encryptor
self._value = value
def __repr__(self):
return '<Password object {}>'.format(self._value)
def __eq__(self,value: str) -> bool:
return self._encryptor.verify(value,self._value)
@property
def value(self) -> str:
return self._value
@classmethod
def validate_strength(cls,value: str) -> (bool,dict):
"""
Verify the strength of 'password',it returns a dict with
bool values for each validation.
A password is considered strong if:
8 characters length or more
1 digit or more
1 symbol or more
1 uppercase letter or more
1 lowercase letter or more
"""
if value is None:
return False,{}
length = cls._is_longer_than_7(value)
digit = cls._has_digit(value)
is_valid = all([length,digit])
error_dict = {
'length': length,'digit': digit
}
return is_valid,error_dict
@classmethod
def _is_longer_than_7(cls,value):
return not len(value) < 8
@classmethod
def _has_digit(cls,value):
return bool(re.search(r"\d",value))
@value.setter
def value(self,value: str):
valid_password,_ = self.validate_strength(value)
if not valid_password:
raise exceptions.PasswordStrengthError(
'A strong password should contain at least 8 characters,'
'1 digit,1 symbol,1 uppercase letter,and 1 lowercase '
'letter'
)
self._value = self._encryptor.encrypt(value)
和
class Credential:
def __init__(
self,username: str,password: str = None,encryptor: AbstractEncryptor = Encryptor,active: bool = True
):
self.username = username
self._password = Password(encryptor,password)
self._active = active
@property
def password(self):
return self._password.value
@password.setter
def password(self,value: str):
self._password.value = value
def __eq__(self,other: Credential) -> bool:
return (self.username == other.username and
self._password.value == other.password)
def verify_password(self,value: str) -> bool:
return self._password == value
def deactivate(self):
self.active = False
我正在尝试使用 sqlAlchemy 中的经典映射来映射 Credential
。
credentials = Table('credentials',Metadata,Column('id',Integer,primary_key=True,autoincrement=True),Column('username',String,index=True,unique=True,nullable=False),Column('password',Column('active',Boolean)
)
mapper(credential.Credential,credentials,properties={
'password': synonym('_password',map_column=True,descriptor=credential.Credential.password)
})
但问题是我无法让 sqlAlchemy 看到我在 getter 中定义的密码的实际字符串值。然后,当我尝试运行它时,它会引发 InterfaceError,因为它尝试保存 Password
对象而不是字符串。我需要让它保存 Password
的计算值。
PS:我知道可以使用 hybrid_property
来实现,但它似乎只在声明性映射中可用。有没有办法在经典映射中做到这一点?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)