changeset 836:942b53b4e095

use whoosh for user lookup, add EMAIL/OPENID to latest-revs schema fix some minor test issues with unicode/str data types
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 03 Sep 2011 23:33:02 +0200
parents 8fcaf02251de
children f0dceb963342
files MoinMoin/_tests/test_user.py MoinMoin/auth/_tests/test_auth.py MoinMoin/auth/_tests/test_http.py MoinMoin/config/__init__.py MoinMoin/search/indexing.py MoinMoin/user.py
diffstat 6 files changed, 42 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_user.py	Sat Sep 03 21:26:42 2011 +0200
+++ b/MoinMoin/_tests/test_user.py	Sat Sep 03 23:33:02 2011 +0200
@@ -158,7 +158,7 @@
         # "KeyError: 'enc_password'" error during user authentication.
         user_name = u'moin'
         user_password = u'{SHA}LKM56'
-        user.create_user(user_name, user_password, u'moin@moinmo.in', '')
+        user.create_user(user_name, user_password, u'moin@moinmo.in', u'')
 
         # Try to "login"
         theuser = user.User(name=user_name, password=user_password)
--- a/MoinMoin/auth/_tests/test_auth.py	Sat Sep 03 21:26:42 2011 +0200
+++ b/MoinMoin/auth/_tests/test_auth.py	Sat Sep 03 23:33:02 2011 +0200
@@ -37,13 +37,13 @@
         givenauth_obj.strip_windomain = True
         givenauth_obj.titlecase = True
         givenauth_obj.remove_blanks = True
-        create_user('Test_User', 'test_pass', 'test@moinmoin.org')
+        create_user(u'Test_User', u'test_pass', u'test@moinmoin.org')
         test_user, bool_value = givenauth_obj.request(flaskg.user)
         assert test_user.valid
         assert test_user.name == u'Test_User'
 
 def test_handle_login():
-    # no messages in the biginning
+    # no messages in the beginning
     assert not flaskg._login_messages
     test_user1 = handle_login(flaskg.user, login_username = 'test_user', login_password = 'test_password', stage = 'moin')
     test_login_message = [u'Invalid username or password.']
@@ -56,7 +56,7 @@
     givenauth_obj = GivenAuth()
     flaskg.user.auth_method = 'given'
     givenauth_obj.user_name = u'Test_User'
-    create_user('Test_User', 'test_pass', 'test@moinmoin.org')
+    create_user(u'Test_User', u'test_pass', u'test@moinmoin.org')
     test_user, bool_value = givenauth_obj.request(flaskg.user)
     test_user2 = handle_login(test_user, login_username = 'Test_User', login_password = 'test_pass', stage = 'moin')
     assert not flaskg._login_messages
--- a/MoinMoin/auth/_tests/test_http.py	Sat Sep 03 21:26:42 2011 +0200
+++ b/MoinMoin/auth/_tests/test_http.py	Sat Sep 03 23:33:02 2011 +0200
@@ -29,7 +29,7 @@
 
     def test_request(self):
         # create a new user
-        create_user(u'ValidUser', 'test_pass', 'test_email@moinmoin')
+        create_user(u'ValidUser', u'test_pass', u'test_email@moinmoin')
         httpauthmoin_obj = HTTPAuthMoin()
         test_user, bool_val = httpauthmoin_obj.request(flaskg.user)
         assert test_user.valid
--- a/MoinMoin/config/__init__.py	Sat Sep 03 21:26:42 2011 +0200
+++ b/MoinMoin/config/__init__.py	Sat Sep 03 23:33:02 2011 +0200
@@ -122,6 +122,10 @@
 WIKINAME = "wikiname"
 CONTENT = "content"
 
+# stuff from user profiles / for whoosh index
+EMAIL = "email"
+OPENID = "openid"
+
 # structure for contenttype groups
 CONTENTTYPE_GROUPS = [
     ('markup text items', [
--- a/MoinMoin/search/indexing.py	Sat Sep 03 21:26:42 2011 +0200
+++ b/MoinMoin/search/indexing.py	Sat Sep 03 23:33:02 2011 +0200
@@ -15,7 +15,7 @@
 
 from MoinMoin.config import WIKINAME, NAME, NAME_EXACT, REV_NO, MTIME, CONTENTTYPE, TAGS, \
                             LANGUAGE, USERID, ADDRESS, HOSTNAME, SIZE, ACTION, COMMENT, \
-                            CONTENT, UUID, ITEMLINKS, ITEMTRANSCLUSIONS, ACL
+                            CONTENT, UUID, ITEMLINKS, ITEMTRANSCLUSIONS, ACL, EMAIL, OPENID
 from MoinMoin.search.analyzers import *
 from MoinMoin.error import FatalError
 
@@ -113,6 +113,14 @@
         }
         latest_revs_fields.update(**common_fields)
 
+        userprofile_fields = {
+            # EMAIL from user profile metadata
+            EMAIL: ID(unique=True, stored=True),
+            # OPENID from user profile metadata
+            OPENID: ID(unique=True, stored=True),
+        }
+        latest_revs_fields.update(**userprofile_fields)
+
         all_revs_fields = {
             # UUID from metadata
             UUID: ID(stored=True),
--- a/MoinMoin/user.py	Sat Sep 03 21:26:42 2011 +0200
+++ b/MoinMoin/user.py	Sat Sep 03 23:33:02 2011 +0200
@@ -31,8 +31,10 @@
 from flask import g as flaskg
 from flask import session, request, url_for
 
+from whoosh.query import Term, And, Or
+
 from MoinMoin import config, wikiutil
-from MoinMoin.config import NAME, UUID, ACTION, CONTENTTYPE
+from MoinMoin.config import WIKINAME, NAME, NAME_EXACT, UUID, ACTION, CONTENTTYPE, EMAIL, OPENID
 from MoinMoin.i18n import _, L_, N_
 from MoinMoin.util.interwiki import getInterwikiHome, getInterwikiName, is_local_wiki
 from MoinMoin.util.crypto import crypt_password, upgrade_password, valid_password, \
@@ -77,7 +79,7 @@
 
     # Email should be unique - see also MoinMoin/script/accounts/moin_usercheck.py
     if theuser.email and app.cfg.user_email_unique:
-        if get_by_email_address(theuser.email):
+        if get_by_email(theuser.email):
             return _("This email already belongs to somebody else.")
 
     # Openid should be unique
@@ -93,33 +95,28 @@
     return flaskg.unprotected_storage
 
 
+def get_revs_by_filter(**q):
+    """ Searches for a user with a given filter """
+    q.update(wikiname=app.cfg.interwikiname) # XXX for now, search only users of THIS wiki
+                                             # maybe add option to not index wiki users separately,
+                                             # but share them in the index also
+    backend = get_user_backend()
+    docs = backend.documents(all_revs=False, **q)
+    return list(docs)
+
+
 def getUserList():
     """ Get a list of all (numerical) user IDs.
 
     :rtype: list
     :returns: all user IDs
     """
-    userlist = []
-    for item in get_user_backend().iteritems():
-        rev = item.get_revision(-1)
-        userlist.append(rev[UUID])
-    return userlist
-
+    revs = get_revs_by_filter()
+    return [rev[UUID] for rev in revs]
 
-def get_revs_by_filter(key, value):
-    """ Searches for a user with a given filter """
-    backend = get_user_backend()
-    revs_found = []
-    for item in backend.iteritems():
-        rev = item.get_revision(-1)
-        if rev.get(key) == value:
-            revs_found.append(rev)
-    return revs_found
-
-
-def get_by_email_address(email_address):
+def get_by_email(email):
     """ Searches for an user with a particular e-mail address and returns it. """
-    revs = get_revs_by_filter('email', email_address)
+    revs = get_revs_by_filter(email=email)
     if revs:
         return User(revs[0][UUID])
 
@@ -132,7 +129,7 @@
     :returns: the user whose openid is this one
     :rtype: user object or None
     """
-    revs = get_revs_by_filter('openid', openid)
+    revs = get_revs_by_filter(openid=openid)
     if revs:
         return User(revs[0][UUID])
 
@@ -143,18 +140,18 @@
     :rtype: string
     :returns: the corresponding user name or None
     """
-    revs = get_revs_by_filter(UUID, uuid)
+    revs = get_revs_by_filter(uuid=uuid)
     if revs:
         return revs[0][NAME]
 
-def getUserId(searchName):
+def getUserId(name):
     """ Get the user ID for a specific user NAME.
 
-    :param searchName: the user name to look up
-    :rtype: string
+    :param name: the user name to look up
+    :rtype: unicode
     :returns: the corresponding user ID or None
     """
-    revs = get_revs_by_filter(NAME, searchName)
+    revs = get_revs_by_filter(name_exact=name)
     if revs:
         return revs[0][UUID]