# test for may.write
# Reimar Bauer
# GPL

from MoinMoin.util import filesys
from MoinMoin.PageEditor import PageEditor

def execute(macro, args):
    request = macro.request
    _ = request.getText
    formatter = macro.formatter
    comment = ''
    oldpagename = 'TestUserHasNoACLRights'
    newpagename = 'TestIfTestUserHasNoACLRights'
    oldpage = PageEditor(request, oldpagename)
    newpage = PageEditor(request, newpagename)
    if not newpage.exists(includeDeleted=1) and not request.user.may.write(newpagename):
            return  _('You are not allowed to copy this page!')

    pageexists_error = _("""'''A page with the name {{{'%s'}}} already exists.''' Try a different name.""") %     (newpagename,)
    # Check whether a page with the new name already exists
    if newpage.exists(includeDeleted=1):
        return pageexists_error

    # Get old page text
    savetext = oldpage.get_raw_body()
    oldpath = oldpage.getPagePath(check_create=0)
    newpath = newpage.getPagePath(check_create=0)

    try:
        filesys.copytree(oldpath, newpath)
        request.write('we have copied to newpage %s<br>' % newpagename)
        newpage.error = None
        if not comment:
            comment = u"## page was copied from %s" % oldpagename
        request.write('we check here if we are able to write to an acl protected page<br>')
        if request.user.may.write(newpagename): # and request.user.may.admin(newpagename):
            request.write('Oops why we are here<br>')
    except:
        return "OK"