view data/plugin/action/ @ 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
children 2f092fcc3999
line wrap: on
line source
# -*- 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:

        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)
            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)
        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
        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  []
        recommendation_send_to = []
        for username in selected_users:
            status, msg = RecommendPage(request, pagename, username)
            if status:

        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")

    users = user.getUserList(request)
    html = ["<OPTION>%s</OPTION>" % user.User(request, id=uid).name for uid in users]

    formhtml = """
<form method="post" >
<select name="username" size="%(len)s" multiple>
<input type="hidden" name="action" value="%(actname)s">
<input type="submit" name="button" value="%(button)s">
(no selection recommends to: %(user)s)
<input type="hidden" name="ticket" value="%(ticket)s">
</form>""" % {
'querytext': 'Recommend page to',
'actname': 'RecommendPage',
'ticket': wikiutil.createTicket(request),
'option': ' '.join(html),
'len': min([3, len(html)]),
'button': 'Recommend'}

    request.theme.add_msg(formhtml, "info")