def getAssociation(self, server_url, handle=None):
"""
Gets a server url and the handle and finds a matching association that
has not expired. Expired associations are deleted. The association
returned is the one with the most recent issuing timestamp.
"""
query = {'server_url': server_url}
if handle:
query.update({'handle': handle.encode('hex')})
try:
mist_associations = MistAssociation.objects(**query)
except me.DoesNotExist:
mist_associations = []
filtered_mist_assocs = []
for assoc in mist_associations:
if assoc.is_expired():
assoc.delete()
else:
filtered_mist_assocs.append(assoc)
filtered_mist_assocs = sorted(filtered_mist_assocs,
key=lambda assoc: assoc.issued,
reverse=True)
if len(filtered_mist_assocs) > 0:
mist_assoc = filtered_mist_assocs[0]
association = Association(handle=mist_assoc.handle.decode('hex'),
secret=mist_assoc.secret.decode('hex'),
issued=mist_assoc.issued,
lifetime=mist_assoc.lifetime,
assoc_type=mist_assoc.assoc_type)
return association
return None
评论列表
文章目录