Back to index

moin  1.9.0~rc2
UpdateGroupTest.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: iso-8859-1 -*-
00003 """
00004     MoinMoin - client side of xmlrpc UpdateGroup functionality.
00005 
00006     You can use this code to update a group page in a target wiki via xmlrpc.
00007     Of course you need to enable the xmlrpc service in the target wiki, see
00008     your actions_excluded settings (by default, it contains 'xmlrpc')!
00009 
00010     @copyright: 2006-2008 MoinMoin:ThomasWaldmann
00011     @license: GNU GPL, see COPYING for details.
00012 """
00013 # convenience: fixup python path so script can be started from here:
00014 import sys
00015 sys.path.insert(0, '../../../..')
00016 
00017 import xmlrpclib
00018 
00019 
00020 def updateGroup(server_url, username, password, groupname, groupdesc, groupmembers, acl=''):
00021     """
00022     Update a Wiki Group Page named <groupname> with a list of <groupmembers> via xmlrpc.
00023     Contact the target wiki xmlrpc service at <server_url> and use <username>
00024     and <password> to authenticate as wiki user there.
00025 
00026     @param server_url: xmlrpc service url of target wiki (str)
00027     @param username: username used to authenticate at server_url wiki (unicode)
00028     @param password: password of <username> (unicode)
00029     @param groupname: group page name (unicode)
00030     @param groupdesc: group description (unicode)
00031     @param groupmembers: group member names (list of unicode)
00032     @param acl: Access Control List value (optional, unicode)
00033     """
00034     wiki = xmlrpclib.ServerProxy(server_url)
00035     auth_token = wiki.getAuthToken(username, password)
00036     assert auth_token, 'Invalid username/password'
00037 
00038     # Verify that the token is valid by using it
00039     # and checking that the result is 'SUCCESS'.
00040     # The token should be valid for 15 minutes.
00041     assert wiki.applyAuthToken(auth_token) == 'SUCCESS'
00042 
00043     try:
00044         # build a multicall object that
00045         mcall = xmlrpclib.MultiCall(wiki)
00046         # first applies the token and
00047         mcall.applyAuthToken(auth_token)
00048         # then creates/updates the group page
00049         mcall.UpdateGroup(groupname, groupdesc, groupmembers, acl)
00050         # now execute the multicall
00051         results = mcall()
00052 
00053         # everything should have worked
00054         # instead of the asserts you can have anything else
00055         # but you should definitely access all the results
00056         # once so that faults are checked and raised
00057         assert results[0] == 'SUCCESS'
00058         # TODO: process other results / xmlrpc faults
00059     finally:
00060         # be nice to the server and clean up the token
00061         # regardless of what happened
00062         assert wiki.deleteAuthToken(auth_token) == 'SUCCESS'
00063 
00064 
00065 if __name__ == "__main__":
00066     # xmlrpc url of target wiki, where the group pages shall get created
00067     server_url = "http://master.moinmo.in/?action=xmlrpc2"
00068     # user and password of a wiki user of the target wiki with enough priviledges
00069     username = "ThomasWaldmann"
00070     password = "wrong"
00071 
00072     # data for the group page:
00073     groupname = u"TestGroup"
00074     groupdesc = u"Just a test group"
00075     groupmembers = [u'JoeDoe', u'JaneDoe', ]
00076     acl = "All:read,write,delete" # optional, can be empty
00077 
00078     updateGroup(server_url, username, password, groupname, groupdesc, groupmembers, acl)
00079