view data/plugin/action/RecommendPage.py @ 654:8c828d7b6cd4 tip

PDFList: check for pyPdf.utils.PdfReadError
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Wed, 07 Dec 2016 14:27:25 +0100
parents 127da830be6c
children
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-2016 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 if not user.User(request, id=uid).disabled]
    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