Back to index

moin  1.9.0~rc2
SubscribeUser.py
Go to the documentation of this file.
00001 """
00002    MoinMoin - Subscribeuser - Action
00003    Subscribe (or unsubscribe) a user to a page.
00004 
00005    @copyright: 2003 Daniela Nicklas <nicklas@informatik.uni-stuttgart.de>,
00006                2005 MoinMoin:AlexanderSchremmer,
00007                2009 MoinMoin:ThomasWaldmann
00008    @license: GNU GPL, see COPYING for details.
00009 """
00010 
00011 import sys, os, re
00012 
00013 from MoinMoin.Page import Page
00014 from MoinMoin import user
00015 from MoinMoin import wikiutil
00016 
00017 
00018 def show_form(pagename, request):
00019     _ = request.getText
00020     request.theme.send_title(_("Subscribe users to the page %s") % pagename, pagename=pagename)
00021 
00022     request.write("""
00023 <form action="%s" method="POST" enctype="multipart/form-data">
00024 <input type="hidden" name="action" value="SubscribeUser">
00025 %s <input type="text" name="users" size="50">
00026 <input type="submit" value="Subscribe">
00027 </form>
00028 """ % (request.href(pagename),
00029       _("Enter user names (comma separated):")))
00030     request.theme.send_footer(pagename)
00031     request.theme.send_closing_html()
00032 
00033 
00034 def parse_re(usernames):
00035     username_regexes = []
00036     for name in usernames:
00037         if name.startswith("re:"):
00038             name = name[3:]
00039         else:
00040             name = re.escape(name)
00041         username_regexes.append(name)
00042     return username_regexes
00043 
00044 
00045 def parse_userlist(usernames):
00046     subscribe = []
00047     unsubscribe = []
00048     for name in usernames:
00049         if name.startswith("-"):
00050             unsubscribe.append(name[1:])
00051         elif name.startswith("+"):
00052             subscribe.append(name[1:])
00053         else:
00054             subscribe.append(name)
00055     return parse_re(subscribe), parse_re(unsubscribe)
00056 
00057 
00058 def show_result(pagename, request):
00059     _ = request.getText
00060 
00061     request.theme.send_title(_("Subscribed for %s:") % pagename, pagename=pagename)
00062 
00063     from MoinMoin.formatter.text_html import Formatter
00064     formatter = Formatter(request)
00065 
00066     usernames = request.form['users'].split(",")
00067     subscribe, unsubscribe = parse_userlist(usernames)
00068 
00069     result = subscribe_users(request, subscribe, unsubscribe, pagename, formatter)
00070     request.write(result)
00071 
00072     request.theme.send_footer(pagename)
00073     request.theme.send_closing_html()
00074 
00075 
00076 def subscribe_users(request, subscribe, unsubscribe, pagename, formatter):
00077     _ = request.getText
00078 
00079     if not Page(request, pagename).exists():
00080         return u"Page does not exist."
00081 
00082     result = []
00083     did_match = {}
00084 
00085     # get user object - only with IDs!
00086     for userid in user.getUserList(request):
00087         userobj = user.User(request, userid)
00088         name = userobj.name
00089 
00090         matched = subscribed = False
00091 
00092         for name_re in unsubscribe:
00093             if re.match(name_re, name, re.U):
00094                 matched = did_match[name_re] = True
00095                 if (not userobj.isSubscribedTo([pagename]) or
00096                     userobj.unsubscribe(pagename)):
00097                     subscribed = False
00098                 break
00099 
00100         for name_re in subscribe:
00101             if re.match(name_re, name, re.U):
00102                 matched = did_match[name_re] = True
00103                 if (userobj.isSubscribedTo([pagename]) or
00104                     (userobj.email or userobj.jid) and userobj.subscribe(pagename)):
00105                     subscribed = True
00106                 break
00107 
00108         if matched:
00109             result.extend([formatter.smiley(subscribed and '{*}' or '{o}'),
00110                            formatter.text(" "),
00111                            formatter.url(1, Page(request, name).url(request)),
00112                            formatter.text(name),
00113                            formatter.url(0),
00114                            formatter.linebreak(preformatted=0),
00115                           ])
00116 
00117     result.extend([''.join([formatter.smiley('{X}'),
00118                             formatter.text(" " + _("Not a user:") + " " + name_re),
00119                             formatter.linebreak(preformatted=0)])
00120                    for name_re in subscribe + unsubscribe if name_re not in did_match])
00121 
00122     return ''.join(result)
00123 
00124 
00125 def execute(pagename, request):
00126     _ = request.getText
00127     if not request.user.may.admin(pagename):
00128         thispage = Page(request, pagename)
00129         request.theme.add_msg(_("You are not allowed to perform this action."), "error")
00130         return thispage.send_page()
00131     elif 'users' not in request.form:
00132         show_form(pagename, request)
00133     else:
00134         show_result(pagename, request)
00135 
00136 
00137 if __name__ == '__main__':
00138     args = sys.argv
00139     if len(args) < 2:
00140         print >>sys.stderr, """Subscribe users
00141 
00142 %(myname)s pagename [+|-][re:]username[,username[,username[,...]]] [URL]
00143 
00144 +username: subscribes user <username> to page <pagename>.
00145 -username: unsubscribes user <username> from page <pagename>.
00146 +re:username_re: subscribes users who match <username_re> regex.
00147 -re:username_re: unsubscribes users who match <username_re> regex.
00148 
00149 URL is just needed for a farmconfig scenario.
00150 
00151 Example:
00152 %(myname)s FrontPage TestUser,MatthewSimpson
00153 
00154 """ % {"myname": os.path.basename(args[0])}
00155         raise SystemExit
00156 
00157     pagename = args[1]
00158     usernames = args[2].split(",")
00159 
00160     if len(args) > 3:
00161         request_url = args[3]
00162     else:
00163         request_url = None
00164 
00165     # Setup MoinMoin environment
00166     from MoinMoin.web.contexts import ScriptContext
00167     request = ScriptContext(url=request_url)
00168 
00169     from MoinMoin.formatter.text_plain import Formatter
00170     formatter = Formatter(request)
00171 
00172     subscribe, unsubscribe = parse_userlist(usernames)
00173 
00174     print subscribe_users(request, subscribe, unsubscribe, pagename, formatter)
00175