changeset 5173:b51c4e6efeee

SubscribeUser action: username regexes, support unsubscribing [+|-]username,[+|-]re:regex_username + subscribes (default if not given) - unsubscribes re: giving a regex E.g.: +ThomasWaldmann,-re:JuergenH.* Subscribes ThomasWaldmann and unsubscribes everyone whose name matches JuergenH.*
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 02 Oct 2009 19:54:32 +0200
parents f8d1d1d5c7a5
children e8d4fabc31e2
files MoinMoin/action/SubscribeUser.py
diffstat 1 files changed, 64 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/SubscribeUser.py	Fri Oct 02 18:26:42 2009 +0200
+++ b/MoinMoin/action/SubscribeUser.py	Fri Oct 02 19:54:32 2009 +0200
@@ -7,7 +7,7 @@
    @license: GNU GPL, see COPYING for details.
 """
 
-import sys, os
+import sys, os, re
 
 from MoinMoin.Page import Page
 from MoinMoin import user
@@ -31,6 +31,30 @@
     request.theme.send_closing_html()
 
 
+def parse_re(usernames):
+    username_regexes = []
+    for name in usernames:
+        if name.startswith("re:"):
+            name = name[3:]
+        else:
+            name = re.escape(name)
+        username_regexes.append(name)
+    return username_regexes
+
+
+def parse_userlist(usernames):
+    subscribe = []
+    unsubscribe = []
+    for name in usernames:
+        if name.startswith("-"):
+            unsubscribe.append(name[1:])
+        elif name.startswith("+"):
+            subscribe.append(name[1:])
+        else:
+            subscribe.append(name)
+    return parse_re(subscribe), parse_re(unsubscribe)
+
+
 def show_result(pagename, request):
     _ = request.getText
     request.emit_http_headers()
@@ -40,51 +64,61 @@
     from MoinMoin.formatter.text_html import Formatter
     formatter = Formatter(request)
 
-    result = subscribe_users(request, request.form['users'][0].split(","), pagename, formatter)
+    usernames = request.form['users'][0].split(",")
+    subscribe, unsubscribe = parse_userlist(usernames)
+
+    result = subscribe_users(request, subscribe, unsubscribe, pagename, formatter)
     request.write(result)
 
     request.theme.send_footer(pagename)
     request.theme.send_closing_html()
 
 
-def subscribe_users(request, usernamelist, pagename, formatter):
+def subscribe_users(request, subscribe, unsubscribe, pagename, formatter):
     _ = request.getText
 
     if not Page(request, pagename).exists():
         return u"Page does not exist."
 
     result = []
-
-    realusers = []              # usernames that are really wiki users
+    did_match = {}
 
     # get user object - only with IDs!
     for userid in user.getUserList(request):
-        success = False
         userobj = user.User(request, userid)
+        name = userobj.name
 
-        if userobj.name in usernamelist:   # found a user
-            realusers.append(userobj.name)
-            if userobj.isSubscribedTo([pagename]):
-                success = True
-            elif not userobj.email and not userobj.jid:
-                success = False
-            elif userobj.subscribe(pagename):
-                success = True
-            if success:
-                result.append(formatter.smiley('{OK}'))
-                result.append(formatter.text(" "))
-            else:
-                result.append(formatter.smiley('{X}'))
-                result.append(formatter.text(" "))
-            result.append(formatter.url(1, Page(request, userobj.name).url(request)))
-            result.append(formatter.text(userobj.name))
-            result.append(formatter.url(0))
-            result.append(formatter.linebreak(preformatted=0))
+        matched = subscribed = False
+
+        for name_re in unsubscribe:
+            if re.match(name_re, name, re.U):
+                matched = did_match[name_re] = True
+                if (not userobj.isSubscribedTo([pagename]) or
+                    userobj.unsubscribe(pagename)):
+                    subscribed = False
+                break
+
+        for name_re in subscribe:
+            if re.match(name_re, name, re.U):
+                matched = did_match[name_re] = True
+                if (userobj.isSubscribedTo([pagename]) or
+                    (userobj.email or userobj.jid) and userobj.subscribe(pagename)):
+                    subscribed = True
+                break
+
+        if matched:
+            result.extend([formatter.smiley(subscribed and '{*}' or '{o}'),
+                           formatter.text(" "),
+                           formatter.url(1, Page(request, name).url(request)),
+                           formatter.text(name),
+                           formatter.url(0),
+                           formatter.linebreak(preformatted=0),
+                          ])
 
     result.extend([''.join([formatter.smiley('{X}'),
-                            formatter.text(" " + _("Not a user:") + " " + username),
+                            formatter.text(" " + _("Not a user:") + " " + name_re),
                             formatter.linebreak(preformatted=0)])
-                   for username in usernamelist if username not in realusers])
+                   for name_re in subscribe + unsubscribe if name_re not in did_match])
 
     return ''.join(result)
 
@@ -118,7 +152,7 @@
         raise SystemExit
 
     pagename = args[1]
-    usernames = args[2]
+    usernames = args[2].split(",")
 
     if len(args) > 3:
         request_url = args[3]
@@ -133,5 +167,7 @@
     from MoinMoin.formatter.text_plain import Formatter
     formatter = Formatter(request)
 
-    print subscribe_users(request, usernames.split(","), pagename, formatter)
+    subscribe, unsubscribe = parse_userlist(usernames)
 
+    print subscribe_users(request, subscribe, unsubscribe, pagename, formatter)
+