changeset 4046:6d9652a1db90

UpdateGroup client: use multicall / auth_token, refactor code so that updateGroup function is reusable
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 26 Aug 2008 17:37:26 +0200
parents a27cfd844424
children 560cdfdfffc3
files MoinMoin/script/old/xmlrpc-tools/UpdateGroupTest.py
diffstat 1 files changed, 73 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/old/xmlrpc-tools/UpdateGroupTest.py	Tue Aug 26 17:19:49 2008 +0200
+++ b/MoinMoin/script/old/xmlrpc-tools/UpdateGroupTest.py	Tue Aug 26 17:37:26 2008 +0200
@@ -1,46 +1,83 @@
 #!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-This script is just an example how to update a group definition page using xmlrpc.
-
-GPL software, 2005 Thomas Waldmann
+# -*- coding: iso-8859-1 -*-
 """
-def run():
-    import sys
-    sys.path.insert(0, '../../..')
-
-    import xmlrpclib
-    from MoinMoin.support.BasicAuthTransport import BasicAuthTransport
-
-    user = "XmlRpc"
-    password = "wrong"
-    dsttrans = BasicAuthTransport(user, password)
-    mywiki = xmlrpclib.ServerProxy("http://master.moinmo.in/?action=xmlrpc2", transport=dsttrans)
+    MoinMoin - client side of xmlrpc UpdateGroup functionality.
+    
+    You can use this code to update a group page in a target wiki via xmlrpc.
+    Of course you need to enable the xmlrpc service in the target wiki, see
+    your actions_excluded settings (by default, it contains 'xmlrpc')!
 
-    groupname = "TestGroup"
-    groupdesc = "This is just a test."
-    groupmembers = ["TestUser1", "TestUser2", ]
-    print mywiki.UpdateGroup(groupname, groupdesc, groupmembers)
-
-    groupname = "TestAclGroup"
-    groupdesc = "This is just a test."
-    groupmembers = ["TestUser3", ]
-    print mywiki.UpdateGroup(groupname, groupdesc, groupmembers, "All:read,write,delete,revert")
+    @copyright: 2006-2008 MoinMoin:ThomasWaldmann
+    @license: GNU GPL, see COPYING for details.
+"""
+# convenience: fixup python path so script can be started from here:
+import sys
+sys.path.insert(0, '../../../..')
 
-    del mywiki
-    del dsttrans
+import xmlrpclib
 
-    user = "XmlRpc"
-    password = "completelywrong"
-    dsttrans = BasicAuthTransport(user, password)
-    mywiki = xmlrpclib.ServerProxy("http://master.moinmo.in/?action=xmlrpc2", transport=dsttrans)
+# we have a fixed xmlrpc multicall implementation (in py 2.3 it was broken):
+from MoinMoin.support.multicall import MultiCall
 
-    groupname = "TestGroup"
-    groupdesc = "This is just a test."
-    groupmembers = ["WrongUser1", "WrongUser2", ]
-    print mywiki.UpdateGroup(groupname, groupdesc, groupmembers)
+
+def updateGroup(server_url, username, password, groupname, groupdesc, groupmembers, acl=''):
+    """
+    Update a Wiki Group Page named <groupname> with a list of <groupmembers> via xmlrpc.
+    Contact the target wiki xmlrpc service at <server_url> and use <username>
+    and <password> to authenticate as wiki user there.
+
+    @param server_url: xmlrpc service url of target wiki (str)
+    @param username: username used to authenticate at server_url wiki (unicode)
+    @param password: password of <username> (unicode)
+    @param groupname: group page name (unicode)
+    @param groupdesc: group description (unicode)
+    @param groupmembers: group member names (list of unicode)
+    @param acl: Access Control List value (optional, unicode)
+    @return: 
+    """
+    wiki = xmlrpclib.ServerProxy(server_url)
+    auth_token = wiki.getAuthToken(username, password)
+    assert auth_token, 'Invalid username/password'
+
+    # Verify that the token is valid by using it
+    # and checking that the result is 'SUCCESS'.
+    # The token should be valid for 15 minutes.
+    assert wiki.applyAuthToken(auth_token) == 'SUCCESS'
+
+    try:
+        # build a multicall object that
+        mcall = MultiCall(wiki)
+        # first applies the token and
+        mcall.applyAuthToken(auth_token)
+        # then creates/updates the group page
+        mcall.UpdateGroup(groupname, groupdesc, groupmembers, acl)
+        # now execute the multicall
+        results = mcall()
+
+        # everything should have worked
+        # instead of the asserts you can have anything else
+        # but you should definitely access all the results
+        # once so that faults are checked and raised
+        assert results[0] == 'SUCCESS'
+        # TODO: process other results / xmlrpc faults
+    finally:
+        # be nice to the server and clean up the token
+        # regardless of what happened
+        assert wiki.deleteAuthToken(auth_token) == 'SUCCESS'
 
 
 if __name__ == "__main__":
-    run()
+    # xmlrpc url of target wiki, where the group pages shall get created
+    server_url = "http://master.moinmo.in/?action=xmlrpc2"
+    # user and password of a wiki user of the target wiki with enough priviledges
+    username = "ThomasWaldmann"
+    password = "wrong"
 
+    # data for the group page:
+    groupname = u"TestGroup"
+    groupdesc = u"Just a test group"
+    groupmembers = [u'JoeDoe', u'JaneDoe', ]
+    acl = "All:read,write,delete" # optional, can be empty
+
+    updateGroup(server_url, username, password, groupname, groupdesc, groupmembers, acl)
+