ldap backend: use a more pythonic sort to get the last connected

Closes #5835
This commit is contained in:
Jérôme Schneider 2014-11-21 14:43:37 +01:00
parent 9f59002549
commit d21f26c66b
1 changed files with 8 additions and 15 deletions

View File

@ -10,6 +10,9 @@ from mandaye import config
from mandaye.log import logger
from mandaye.backends.default import storage_conn
def cmp_reverse_last_connection_date(x, y):
return -cmp(x[1]['lastConnectionDate'][0], y[1]['lastConnectionDate'][0])
class Association(object):
"""
association dictionary return by the following methods:
@ -106,17 +109,12 @@ class Association(object):
logger.info("New association %r with %r", sp_login, idp_unique_id)
return unique_id
else:
biggest = '0'
biggest_pos = 0
for i, result in enumerate(results):
if result[1]['lastConnectionDate'][0] > biggest:
biggest = result[1]['lastConnectionDate'][0]
biggest_pos = i
dn = results[biggest_pos][0]
results.sort(cmp_reverse_last_connection_date)
dn = results[0][0]
mod_list = [(ldap.MOD_REPLACE, 'spPostValues', json.dumps(sp_post_values))]
storage_conn.modify_s(dn, mod_list)
logger.info("Update post values for %r (%r)", sp_login, idp_unique_id)
return results[biggest_pos][1]['uniqueID'][0]
return results[0][1]['uniqueID'][0]
@staticmethod
def delete(asso_id):
@ -133,13 +131,8 @@ class Association(object):
results = storage_conn.search_s(config.ldap_base_dn, ldap.SCOPE_ONELEVEL,
filterstr='(&(objectClass=MandayeUser)(spName=%s)(idpUniqueID=%s)(idpName=%s))' % (sp_name, idp_unique_id, idp_name))
if results:
biggest = '0'
biggest_pos = 0
for i, result in enumerate(results):
if result[1]['lastConnectionDate'][0] > biggest:
biggest = result[1]['lastConnectionDate'][0]
biggest_pos = i
return Association.ldap2association(results[biggest_pos][1])
results.sort(cmp_reverse_last_connection_date)
return Association.ldap2association(results[0][1])
return None
@staticmethod