diff MoinMoin/user.py @ 2011:6cd0e4cc8e18

refactor user.getUserId makes looking up by other keys easier (in the future) email lookup could use this as well
author Johannes Berg <johannes AT sipsolutions DOT net>
date Fri, 20 Apr 2007 15:35:47 +0200
parents d234621dbf2f
children 1b0629547090
line wrap: on
line diff
--- a/MoinMoin/user.py	Fri Apr 20 15:35:31 2007 +0200
+++ b/MoinMoin/user.py	Fri Apr 20 15:35:47 2007 +0200
@@ -48,6 +48,47 @@
         if theuser.valid and theuser.email.lower() == email_address.lower():
             return theuser
 
+def _getUserIdByKey(request, key, search):
+    """ Get the user ID for a specified key/value pair.
+
+    This method must only be called for keys that are
+    guaranteed to be unique.
+
+    @param key: the key to look in
+    @param search: the value to look for
+    @return the corresponding user ID or None
+    """
+    if not search or not key:
+        return None
+    cfg = request.cfg
+    cachekey = '%s2id' % key
+    try:
+        _key2id = getattr(cfg.cache, cachekey)
+    except AttributeError:
+        arena = 'user'
+        cache = caching.CacheEntry(request, arena, cachekey, scope='wiki', use_pickle=True)
+        try:
+            _key2id = cache.content()
+        except caching.CacheError:
+            _key2id = {}
+        setattr(cfg.cache, cachekey, _key2id)
+    uid = _key2id.get(search, None)
+    if uid is None:
+        for userid in getUserList(request):
+            u = User(request, id=userid)
+            if hasattr(u, key):
+                value = getattr(u, key)
+                _key2id[value] = userid
+        arena = 'user'
+        cache = caching.CacheEntry(request, arena, cachekey, scope='wiki', use_pickle=True)
+        try:
+            cache.update(_key2id)
+        except caching.CacheError:
+            pass
+        uid = _key2id.get(search, None)
+    return uid
+
+
 def getUserId(request, searchName):
     """ Get the user ID for a specific user NAME.
 
@@ -55,35 +96,7 @@
     @rtype: string
     @return: the corresponding user ID or None
     """
-    if not searchName:
-        return None
-    cfg = request.cfg
-    try:
-        _name2id = cfg.cache.name2id
-    except AttributeError:
-        arena = 'user'
-        key = 'name2id'
-        cache = caching.CacheEntry(request, arena, key, scope='wiki', use_pickle=True)
-        try:
-            _name2id = cache.content()
-        except caching.CacheError:
-            _name2id = {}
-        cfg.cache.name2id = _name2id
-    uid = _name2id.get(searchName, None)
-    if uid is None:
-        for userid in getUserList(request):
-            name = User(request, id=userid).name
-            _name2id[name] = userid
-        cfg.cache.name2id = _name2id
-        arena = 'user'
-        key = 'name2id'
-        cache = caching.CacheEntry(request, arena, key, scope='wiki', use_pickle=True)
-        try:
-            cache.update(_name2id)
-        except caching.CacheError:
-            pass
-        uid = _name2id.get(searchName, None)
-    return uid
+    return _getUserIdByKey(request, 'name', searchName)
 
 
 def getUserIdentification(request, username=None):