user: fix hasattr() on attributes (fixes #32040)
This commit is contained in:
parent
ff92cb1fb9
commit
56dd857a39
|
@ -60,10 +60,13 @@ class Attributes(object):
|
|||
self.owner.save(update_fields=update_fields)
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name not in get_attributes_map():
|
||||
raise AttributeError(name)
|
||||
atv = self.values.get(name)
|
||||
if self.verified and (not atv or not atv.verified):
|
||||
return None
|
||||
if atv:
|
||||
if not self.verified or atv.verified == self.verified:
|
||||
return atv.to_python()
|
||||
return atv.to_python()
|
||||
return None
|
||||
|
||||
|
||||
|
|
|
@ -188,3 +188,29 @@ def test_fix_attributes(db):
|
|||
assert user.verified_attributes.last_name is None
|
||||
assert user.last_name == 'Doe'
|
||||
assert user.attributes.last_name == 'Doe'
|
||||
|
||||
|
||||
def test_attributes_hasattr(db):
|
||||
user = User.objects.create(first_name='john', last_name='Doe')
|
||||
assert hasattr(user.attributes, 'first_name')
|
||||
assert hasattr(user.attributes, 'last_name')
|
||||
assert user.verified_attributes.first_name is None
|
||||
assert user.verified_attributes.last_name is None
|
||||
assert not hasattr(user.attributes, 'email')
|
||||
assert not hasattr(user.verified_attributes, 'email')
|
||||
|
||||
user.verified_attributes.first_name = 'john'
|
||||
assert hasattr(user.attributes, 'first_name')
|
||||
assert hasattr(user.attributes, 'last_name')
|
||||
assert not hasattr(user.attributes, 'email')
|
||||
assert not hasattr(user.verified_attributes, 'email')
|
||||
assert hasattr(user.verified_attributes, 'first_name')
|
||||
assert user.verified_attributes.last_name is None
|
||||
|
||||
user = User.objects.get()
|
||||
assert hasattr(user.attributes, 'first_name')
|
||||
assert hasattr(user.attributes, 'last_name')
|
||||
assert not hasattr(user.attributes, 'email')
|
||||
assert not hasattr(user.verified_attributes, 'email')
|
||||
assert hasattr(user.verified_attributes, 'first_name')
|
||||
assert user.verified_attributes.last_name is None
|
||||
|
|
Loading…
Reference in New Issue