def anonymous_id_for_user(user, course_id, save=True):
 if user.is_anonymous():
  return None
 cached_id = getattr(user, '_anonymous_id', {}).get(course_id)
 if (cached_id is not None):
  return cached_id
 hasher = hashlib.md5()
 hasher.update(settings.SECRET_KEY)
 hasher.update(unicode(user.id))
 if course_id:
  hasher.update(unicode(course_id).encode('utf-8'))
 digest = hasher.hexdigest()
 if (not hasattr(user, '_anonymous_id')):
  user._anonymous_id = {}
 user._anonymous_id[course_id] = digest
 if (save is False):
  return digest
 try:
  AnonymousUserId.objects.get_or_create(user=user, course_id=course_id, anonymous_user_id=digest)
 except IntegrityError:
  pass
 return digest

def anonymous_id_for_user ( user , course_id , save = True ) : if user . is_anonymous ( ) : return None cached_id = getattr ( user , '_anonymous_id' , { } ) . get ( course_id ) if ( cached_id is not None ) : return cached_id hasher = hashlib . md5 ( ) hasher . update ( settings . SECRET_KEY ) hasher . update ( unicode ( user . id ) ) if course_id : hasher . update ( unicode ( course_id ) . encode ( 'utf-8' ) ) digest = hasher . hexdigest ( ) if ( not hasattr ( user , '_anonymous_id' ) ) : user . _anonymous_id = { } user . _anonymous_id [ course_id ] = digest if ( save is False ) : return digest try : AnonymousUserId . objects . get_or_create ( user = user , course_id = course_id , anonymous_user_id = digest ) except IntegrityError : pass return digest
return a unique id for a pair .