"""
   MoinMoin - Subscribers - Action
   Displays the subscribed users of a page

   Author: Daniela Nicklas <nicklas@informatik.uni-stuttgart.de>
   (c) 2003, all rights reserved

   Designed for MoinMoin 1.1

"""

from MoinMoin import wikiutil, webapi
from MoinMoin.formatter.text_html import Formatter

from MoinMoin.Page import Page
from MoinMoin.i18n import _

def execute(pagename, request):

    # send http headers
    webapi.http_headers(request)

    # send title
    p = Page(pagename)
    wikiutil.send_title(request, _("Subscribers for %s"%pagename))

    # get subscribers
    page = Page(pagename)

    # show subscribers    
    users = page.getSubscribers(request,  include_self=1, return_users=1)
    if users:
        request.write('<b>', _('The following users subscribed to this page:'), '</b><ul>')
        for lang in users.keys():
            request.write('<li>[%s] ' % lang)
            for user in users[lang]:
                # do NOT disclose email addr, only WikiName
                userhomepage = Page(user.name)
                if userhomepage.exists():
                    request.write(userhomepage.link_to() + ' ')
                else:
                    request.write(user.name + ' ')
        request.write('</ul>')

    # generate "mail to all" - link
    subscribers = page.getSubscribers(request,  include_self=1, return_users=0)
    allsubs = ''
    if subscribers:
        form = Formatter(request)
        for subscriber in subscribers.values():
            for email in subscriber:
                allsubs += '%s, '%email
    if allsubs:
        mailtoall = form.url('mailto:%s?subject=%s'%(allsubs,pagename),_('Mail to all'))
        request.write(mailtoall)
        
    # send footer
    wikiutil.send_footer(request, pagename, editable=0, showactions=1, form=request.form)
