[attribute aggregator] Enhancements

* Helper functions
    * In set_user_alias_in_source(), force_change parameter to allow to set an
      alias in a source already used by another user for the same source
This commit is contained in:
Mikaël Ates 2011-09-26 14:14:50 +02:00
parent aad109b9a4
commit a9882bf0d8
1 changed files with 59 additions and 7 deletions

View File

@ -45,6 +45,21 @@ def iso8601_to_datetime(date_string):
return datetime.datetime.fromtimestamp(time.mktime(tm))
def get_all_attribute_definitions():
return ATTRIBUTE_MAPPING.keys()
def get_all_sources():
from attribute_aggregator.models import AttributeSource
return AttributeSource.objects.all()
def get_full_definition(definition):
if not definition in ATTRIBUTE_MAPPING:
return None
return ATTRIBUTE_MAPPING[definition]
def get_def_name_from_name_and_ns_of_attribute(name, namespace):
if not name or not namespace:
return None
@ -67,6 +82,13 @@ def get_attribute_name_in_namespace(definition, namespace):
return None
def get_attribute_type_of_definition(definition):
if not definition or not definition in ATTRIBUTE_MAPPING \
or not 'type' in ATTRIBUTE_MAPPING[definition]:
return None
return ATTRIBUTE_MAPPING[definition]["type"]
def convert_from_string(definition_name, value):
if not definition_name in ATTRIBUTE_MAPPING:
return None
@ -90,12 +112,12 @@ def convert_from_string(definition_name, value):
return None
elif type_ == ACS_XACML_DATATYPE_TIME:
try:
return time.strptime(value,"%h:%m:%s") #12:15:00
return time.strptime(value, "%h:%m:%s") #12:15:00
except:
return None
elif type_ == ACS_XACML_DATATYPE_DATE:
try:
return time.strptime(value,"%d/%b/%Y") #28/01/1982
return time.strptime(value, "%d/%b/%Y") #28/01/1982
except:
return None
elif type_ == ACS_XACML_DATATYPE_DATETIME:
@ -180,12 +202,42 @@ def get_user_alias_in_source(user, source):
return None
def set_user_alias_in_source(user, source, name):
def set_user_alias_in_source(user, source, name, force_change=False):
from attribute_aggregator.models import UserAliasInSource
logger.debug('set_user_alias_in_source: set alias %s for user %s in \
source %s' % (name, user, source))
alias = None
try:
alias, c = UserAliasInSource.objects.get_or_create(user=user,
source=source, name=name)
alias.save()
return alias
'''
If this user has already an alias, we change it.
'''
alias = UserAliasInSource.objects.get(user=user, source=source)
logger.warn('set_user_alias_in_source: \
this user has already an alias, we change it.')
alias.delete()
except:
pass
try:
'''
If a user has already this alias...
force_change: we give it to this user
'''
alias = UserAliasInSource.objects.get(name=name, source=source)
if not force_change:
logger.warn('set_user_alias_in_source: \
a user has already this alias, we do nothing.')
return None
logger.warn('set_user_alias_in_source: \
a user has already this alias, we give it to %s.' % user)
alias.delete()
except:
pass
try:
alias = UserAliasInSource(user=user, name=name, source=source)
alias.save()
logger.debug('set_user_alias_in_source: alias created.')
return alias
except Exception, err:
logger.error('set_user_alias_in_source: unable to create alias due \
to %s.' % str(err))
return None