diff data/plugin/action/RecommendPage.py @ 513:438f1d16e63f

action.RecommendPage: By this action one user can point another one to an usefully interesting page.
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Tue, 06 Apr 2010 17:02:12 +0200
parents
children 2f092fcc3999
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/plugin/action/RecommendPage.py	Tue Apr 06 17:02:12 2010 +0200
@@ -0,0 +1,108 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - RecommendPage Action Macro
+
+    By this action one user can point another one to an usefully interesting page.
+    The hint becomes written to a page named user/RecommendedPage and the user
+    becomes subscribed to that page.
+
+    @copyright: 2005-2010 by MoinMoin:ReimarBauer
+    @license: GNU GPL, see COPYING for details.
+"""
+from MoinMoin import wikiutil, user
+from MoinMoin.Page import Page
+from MoinMoin.PageEditor import PageEditor
+
+def RecommendPage(request, pagename, username):
+    err = None
+    name = "%(username)s/RecommendedPage" % {"username": username}
+    page = PageEditor(request, name)
+    if request.user.may.write(name):
+        if user.getUserId(request, username) is not None:
+            uid = user.getUserId(request, username)
+            recom_user = user.User(request, id=uid)
+            subscription_list = recom_user.getSubscriptionList()
+            if name not in subscription_list:
+                recom_user.subscribe(name)
+                recom_user.save()
+
+        newtext = u" * [[%(pagename)s]]  <<ShortText(%(pagename)s)>> ...\n %(username)s\n" % {
+                 "pagename": pagename,
+                 "username": "@SIG@"}
+        if not page.exists():
+            return True, PageEditor.saveText(page, newtext, 0)
+        else:
+            body = page.get_data()
+            meta = page.get_meta()
+            text_meta = ''
+            for command, attrib in meta:
+                text_meta = '#%s %s\n%s' % (command, attrib, text_meta)
+
+            text = "%s\n%s\n%s\n" % (text_meta, newtext, body)
+            return True, PageEditor.saveText(page, text, 0)
+    else:
+        return False, "You are not allowed to edit this page."
+
+def execute(pagename, request):
+    _ = request.getText
+    actname = __name__.split('.')[-1]
+    thispage = Page(request, pagename)
+    if not (request.user.valid and request.user.may.read(pagename)):
+        msg = _("You are not allowed to do %(action_name)s on this page.") % {"action_name": actname}
+        request.theme.add_msg(msg, "warning")
+        return thispage.send_page()
+
+    if request.method == 'POST' and request.form.has_key('button'):
+        if not wikiutil.checkTicket(request, request.form.get('ticket', '')):
+            msg=_('Please use the interactive user interface to recommend pages!')
+            request.theme.add_msg(msg, "warning")
+            return thispage.send_page()
+
+        selected_users = request.values.getlist('username') or  [request.user.name]
+        recommendation_send_to = []
+        for username in selected_users:
+            status, msg = RecommendPage(request, pagename, username)
+            if status:
+                recommendation_send_to.append(username)
+
+        no_recommendation_for = set(selected_users) - set(recommendation_send_to)
+        msg = []
+        msg.append("recommended to read %(pagename)s for %(username)s" % {
+              "pagename": pagename,
+              "username": ', '.join(recommendation_send_to)})
+        if no_recommendation_for:
+            msg.append("You can't send a recommendation to %(username)s" % {
+              "username": ', '.join(no_recommendation_for)})
+
+        request.theme.add_msg('<br>'.join(msg), "info")
+        thispage.send_page()
+        return
+
+    users = user.getUserList(request)
+    html = ["<OPTION>%s</OPTION>" % user.User(request, id=uid).name for uid in users]
+    html.sort()
+
+    formhtml = """
+<form method="post" >
+<strong>%(querytext)s</strong><BR>
+<select name="username" size="%(len)s" multiple>
+%(option)s
+</select>
+<input type="hidden" name="action" value="%(actname)s">
+<input type="submit" name="button" value="%(button)s">
+<br>
+(no selection recommends to: %(user)s)
+<input type="hidden" name="ticket" value="%(ticket)s">
+<p>
+</form>""" % {
+'querytext': 'Recommend page to',
+'actname': 'RecommendPage',
+'ticket': wikiutil.createTicket(request),
+'option': ' '.join(html),
+'user': request.user.name,
+'len': min([3, len(html)]),
+'button': 'Recommend'}
+
+    request.theme.add_msg(formhtml, "info")
+    thispage.send_page()
+    return