# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - ApprovePage action

    This action allows you to approve a page.

    @copyright: 2005 by Robert Penz <robert.penz@outertech.com>
    @license: GNU GPL, see COPYING for details.

    Changes by Oliver Siemoneit 4.12.2006:
    * made action 1.5 compatible

    lameike organzied code better (02.2010, see http://moinmo.in/ActionMarket/ApprovePageAction)

    Frank Steinhauer commented do_action, removed unused code (11.2010)
    * renamed to approvePage.py
"""

import os
from datetime import datetime
from MoinMoin import wikiutil
from MoinMoin.PageEditor import PageEditor
from MoinMoin.action import ActionBase

class ApprovePage(ActionBase):
    """ Approve page action

    Note: the action name is the class name
    """
    def __init__(self, pagename, request):
        ActionBase.__init__(self, pagename, request)
        self.use_ticket = True
        _ = self._
        self.form_trigger = 'approve'
        self.form_trigger_label = _('Approve')

    def is_allowed(self):
        # check user right
        return self.request.user.may.approve(self.pagename)

    def check_condition(self):
        _ = self._
        if not self.page.exists():
            return _('This page is already deleted or was never created! Cannot be approved.')
        else:
            return None

    def do_action(self):
        """ Approve the page, i.e. write line to file "approved" in page directory
            * first colum is the revision
            * second column the username
            * third column is date in format "YYYY-MM-DD HH:MM"
        """ 
        open(os.path.abspath(os.path.join(self.page.getPagePath(), "approved")),"a").write("%08d\t%s\t%s\n" % (self.page.getRevList()[0], self.request.user.name,datetime.utcnow().strftime("%Y-%m-%d %H:%M")))

        msgs = 'Page "%s" has been successfully approved!' % self.pagename
        return 1, msgs

    def get_form_html(self, buttons_html):
        _ = self._

        d = {
            'pagename': self.pagename,
            'buttons_html': buttons_html,
            'querytext': _('Really approve page?'),
            }

        return '''
<strong>%(querytext)s</strong>
<table>
    <tr>
        <td></td>
        <td class="buttons">
            %(buttons_html)s
        </td>
    </tr>
</table>
''' % d



def execute(pagename, request):
    """ Glue code for actions """
    ApprovePage(pagename, request).render()

