diff data/plugin/script/account/subscribe.py @ 562:0a13d4186fa0

script.account.subscribe: introduced a cli script for subscribing/unsubscribing a user, members of a group or all valid users of the wiki to a page
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Thu, 07 Apr 2011 19:16:24 +0200
parents
children 127da830be6c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/plugin/script/account/subscribe.py	Thu Apr 07 19:16:24 2011 +0200
@@ -0,0 +1,106 @@
+# -*- coding: iso-8859-1 -*-
+"""
+MoinMoin - subscribes or unsubscribes users to a page
+
+@copyright: 2011 MoinMoin:ReimarBauer
+@license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin import user
+from MoinMoin.Page import Page
+from MoinMoin.script import MoinScript
+
+class PluginScript(MoinScript):
+    """\
+Purpose:
+========
+This tool allows you to subscribe or unsubscribe users of a wiki to a page
+
+Detailed Instructions:
+======================
+General syntax: moin [options] account subscribe [subscribe-options]
+
+[options] usually should be:
+    --config-dir=/path/to/my/cfg/ --wiki-url=http://wiki.example.org/
+
+1. Required is one of the options --name, ---group, --all-users.
+To subscribe a user to a page use the --name argument. For subscribe by a group of users
+give the --group page argument. Or with --all-users you subscribe ALL users.
+2. Required is also the pagename of the page to subscribe too
+3. Optional the selected users could be unsubscribed by --unsubscribe
+"""
+
+    def __init__(self, argv, def_values):
+        MoinScript.__init__(self, argv, def_values)
+
+        self.parser.add_option(
+            "-u", "--user", dest="priviledged_user",
+            help="User as whom the homepage creation operation will be performed as."
+            )
+
+        self.parser.add_option(
+            "-n", "--name", dest="user_subscribe",
+            help="The name of the user to subscribe to a page."
+            )
+
+        self.parser.add_option(
+            "-g", "--group", dest="name_of_group_page",
+            help="The name of the group page to select users to subscribe to a page"
+            )
+
+        self.parser.add_option(
+            "-a", "--all-users", dest="all_users", action="store_true",
+            help="All existing users of the wiki to subscribe to a page"
+            )
+
+        self.parser.add_option(
+            "-r", "--unsubscribe", dest="unsubscribe", action="store_true",
+            help="unsubscribe users of a wiki page"
+            )
+
+        self.parser.add_option(
+            "-p", "--pagename", dest="pagename",
+            help="The name of the page to subscribe"
+            )
+
+
+    def write_subscription(self, account, pagename, unsubscribe=False):
+        # subscribes to a page
+        if account.exists() and not account.disabled and Page(self.request, pagename).exists():
+            if unsubscribe:
+                account.unsubscribe(pagename)
+            else:
+                account.subscribe(pagename)
+            account.save()
+
+    def mainloop(self):
+        # we don't expect non-option arguments
+        self.init_request()
+        request = self.request
+        # Check for user
+        if self.options.priviledged_user:
+            uid = user.getUserId(request, self.options.priviledged_user)
+            request.user = user.User(request, uid)
+        # Check for Group definition
+        members = []
+        if self.options.user_subscribe:
+            members = [self.options.user_subscribe, ]
+        elif self.options.name_of_group_page:
+            members = request.groups.get(self.options.name_of_group_page, [])
+        elif self.options.all_users:
+            uids = user.getUserList(request)
+            members = [user.User(request, uid).name for uid in uids]
+
+        if not members:
+            print "No user selected!"
+            return
+        if not self.options.pagename:
+            print "No pagename given!"
+            return
+
+        # loop through members for doing the subscription
+        for name in members:
+            uid = user.getUserId(request, name)
+            account = user.User(request, uid)
+            self.write_subscription(account, self.options.pagename, unsubscribe=self.options.unsubscribe)
+