from plone import api
from ploneintranet.workspace.browser.add_workspace import AddWorkspace
from ploneintranet.workspace.config import TEMPLATES_FOLDER


class CopyToTemplates(AddWorkspace):
    """
    Copies the workspace that this view is called on to the templates
    folder.
    """

    def get_template(self):
        return self.context

    def __call__(self):

        portal = api.portal.get()
        templates_folder = portal.get(TEMPLATES_FOLDER)

        # Set the workspaces title and id into the request
        self.request.form["id"] = self.context.id
        self.request.form["title"] = title = self.context.title

        copied_ws = self.create_from_template(templates_folder)

        api.portal.show_message(
            'The workspace "{}"" was successfully copied to the templates '
            "folder".format(title),
            self.request,
        )

        return self.request.response.redirect(copied_ws.absolute_url())
