1 """
   2 MoinMoin - subscription management tool
   3 GPL code written by Chris Gaskett 20041005
   4 parts from moin_usercheck-jh-new.py
   5 
   6 How to use this tool?
   7 =====================
   8 
   9 Before starting, making a backup of your wiki might be a good idea.
  10 
  11 Examples:
  12 
  13 These examples assume that group names can include numbers e.g.
  14 page_group_regex = '[0-9a-z]Group$'
  15 
  16 1. subscribe user JoeBloggs to page SubjectCp3046:
  17  moin_addsubscription JoeBloggs SubjectCp3046 --config .
  18 
  19 2. subscribe user JoeBloggs to pages starting with SubjectCp3046 (including subpages)
  20  moin_addsubscription JoeBloggs "SubjectCp3046.*" --config .
  21 
  22 3. subscribe users in SubjectCp3046Group to page SubjectCp3046:
  23  moin_addsubscription SubjectCp3046Group SubjectCp3046 --config .
  24 
  25 4. subscribe users in SubjectCp3046Group to their own homepages (including subpages):
  26  moin_addsubscription SubjectCp3046Group self --config .
  27 
  28 None of the examples above would actually save the changes, to do that specify --save, e.g.
  29  moin_addsubscription SubjectCp3046Group self --config . --save
  30 
  31 Bugs
  32 =====
  33  * Help page doesn't work well and isn't complete, doesn't show Chris Gaskett as author
  34  * Only tested under moin version 1.2.3
  35  * no special groups like Known
  36 """
  37 __version__ = "0.1"
  38 
  39 from MoinMoin.scripts import _util
  40 config = None
  41 
  42 
  43 #############################################################################
  44 ### Main program
  45 #############################################################################
  46 
  47 class MoinAddSubscription(_util.Script):
  48     def __init__(self):
  49         _util.Script.__init__(self, __name__, "username|group pagename|self [options]")
  50 
  51         # --config=DIR
  52         self.parser.add_option(
  53             "--config", metavar="DIR", dest="configdir",
  54             help="Path to moin_config.py (or its directory)"
  55         )
  56 
  57         # MoinMoin 1.3+
  58         #self._addFlag("remove",
  59         #    "Unsubscribe rather than subscribe"
  60         #)
  61 
  62         self._addFlag("save",
  63             "Will allow the other"
  64             " options to save user accounts back to disk."
  65             " If not specified, no settings will be changed permanently."
  66         )
  67 
  68 
  69     def _addFlag(self, name, help):
  70         self.parser.add_option("--" + name,
  71             action="store_true", dest=name, default=0, help=help)
  72         
  73     def subscribeUser(self, username, pagename):
  74         from MoinMoin import user
  75         uid = user.getUserId(username)
  76         if uid is None:
  77             print "User not found: %s" % (username)
  78             return 1
  79         u = user.User(None, id=uid)
  80 
  81         if pagename == "self":
  82             pagename = username + ".*"
  83 
  84         print "Before: %-25s %-35s" % (u.name, u.subscribed_pages)
  85         # u.subscribePage(pagename, self.options.remove) # MoinMoin 1.3+
  86         u.subscribePage(pagename)
  87         print "After:  %-25s %-35s" % (u.name, u.subscribed_pages)
  88         if self.options.save:
  89             u.save()
  90         return 0
  91         
  92     def mainloop(self):
  93         """ moin-addsubscription's main code.
  94         """
  95         import os, sys
  96 
  97         # arguments are username or group and subscription entry to be added
  98         if len(self.args) != 2:
  99             print "Error: requires two arguments"
 100             self.parser.print_help()
 101             sys.exit(1)
 102 
 103         #
 104         # Load the configuration, code taken from moin_usercheck-jh-new.py
 105         #
 106         configdir = self.options.configdir
 107         if configdir:
 108             if os.path.isfile(configdir): configdir = os.path.dirname(configdir)
 109             if not os.path.isdir(configdir):
 110                 _util.fatal("Bad path %r given to --config parameter" % configdir)
 111             configdir = os.path.abspath(configdir)
 112             sys.path[0:0] = [configdir]
 113             os.chdir(configdir)
 114 
 115         global config
 116         from MoinMoin import config
 117         if config.default_config:
 118             _util.fatal("You have to be in the directory containing moin_config.py, "
 119                 "or use the --config option!")
 120 
 121         # real work begins here
 122         
 123         groupname_or_username = self.args[0]
 124         pagename = self.args[1]
 125 
 126         import re
 127         if re.search(config.page_group_regex, groupname_or_username, re.UNICODE) is not None:
 128             from MoinMoin import wikidicts
 129             group = wikidicts.Group(groupname_or_username)
 130             for username in group.members():
 131                 self.subscribeUser(username, pagename)
 132         else:
 133             # user, not a group
 134             self.subscribeUser(groupname_or_username, pagename)
 135         if self.options.save:
 136             print "Any subscription changes were saved."
 137         else:
 138             print "Subscription changes were not saved; to make them permanent use the --save flag."
 139 
 140 
 141 def run():
 142     MoinAddSubscription().run()
 143 
 144 if __name__ == "__main__":
 145     run()

MoinMoin: CreateMultipleUserProfiles/moin_addsubscription.py (last edited 2007-10-29 19:10:02 by localhost)