Commit 507767e1 authored by Vincent Pelletier's avatar Vincent Pelletier

Improve default-value support to improve backward compatibility:

 - return None when no password is set and no default was provided, instead of raising
 - return default (or None) when requested encoding is not present (even if password value is a string)


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25616 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 03e08980
...@@ -227,26 +227,35 @@ class Person(XMLObject): ...@@ -227,26 +227,35 @@ class Person(XMLObject):
default (anything) default (anything)
Value to return if no passord is set on context. Value to return if no passord is set on context.
Default: no default, raises AttributeError if property is not set. Default: None
format (string) format (string)
String defining the format in which the password is expected. String defining the format in which the password is expected.
If passowrd is not available in that format, KeyError will be If passowrd is not available in that format, KeyError will be
raised. raised.
Default: 'default' Default: 'default'
""" """
password = getattr(aq_base(self), 'password', *args) marker = []
password = getattr(aq_base(self), 'password', marker)
if password is marker:
if len(args):
password = args[0]
else:
password = None
else:
format = kw.get('format', 'default') format = kw.get('format', 'default')
try:
# Backward compatibility: if it's not a PersistentMapping instance, # Backward compatibility: if it's not a PersistentMapping instance,
# assume it's a monovalued string, which corresponds to default # assume it's a monovalued string, which corresponds to default
# password encoding. # password encoding.
if isinstance(password, PersistentMapping): if isinstance(password, PersistentMapping):
password = password[format] password = password.get(format, marker)
if password is marker:
if len(args):
password = args[0]
else:
password = None
else: else:
if format != 'default': if format != 'default':
raise KeyError password = None
except KeyError:
raise KeyError, 'Password is not available in %r format.' % (format, )
return password return password
# Time management # Time management
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment