| 1 |
# (C) Copyright 2007-2008 Nuxeo SAS <http://nuxeo.com> |
|---|
| 2 |
# Authors: |
|---|
| 3 |
# M.-A. Darche <madarche@nuxeo.com> |
|---|
| 4 |
# |
|---|
| 5 |
# This program is free software; you can redistribute it and/or modify |
|---|
| 6 |
# it under the terms of the GNU General Public License version 2 as published |
|---|
| 7 |
# by the Free Software Foundation. |
|---|
| 8 |
# |
|---|
| 9 |
# This program is distributed in the hope that it will be useful, |
|---|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 |
# GNU General Public License for more details. |
|---|
| 13 |
# |
|---|
| 14 |
# You should have received a copy of the GNU General Public License |
|---|
| 15 |
# along with this program; if not, write to the Free Software |
|---|
| 16 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
|---|
| 17 |
# 02111-1307, USA. |
|---|
| 18 |
# |
|---|
| 19 |
# $Id$ |
|---|
| 20 |
"""Module dedicated to recursive publishing. |
|---|
| 21 |
""" |
|---|
| 22 |
|
|---|
| 23 |
import os.path |
|---|
| 24 |
from logging import getLogger |
|---|
| 25 |
|
|---|
| 26 |
from AccessControl import Unauthorized |
|---|
| 27 |
from AccessControl import ModuleSecurityInfo |
|---|
| 28 |
from AccessControl.requestmethod import postonly |
|---|
| 29 |
|
|---|
| 30 |
from Products.CMFCore.utils import getToolByName |
|---|
| 31 |
from Products.CMFCore.utils import _checkPermission |
|---|
| 32 |
|
|---|
| 33 |
LOG_KEY = 'recursivepublish' |
|---|
| 34 |
|
|---|
| 35 |
WORKSPACE_PORTAL_TYPE = 'Workspace' |
|---|
| 36 |
SECTION_PORTAL_TYPE = 'Section' |
|---|
| 37 |
|
|---|
| 38 |
FOLDERISH_PROXY_TYPES = ['folder', 'folderishdocument', |
|---|
| 39 |
'btreefolder', 'btreefolderishdocument'] |
|---|
| 40 |
|
|---|
| 41 |
security = ModuleSecurityInfo('Products.CPSDefault.recursivepublish') |
|---|
| 42 |
|
|---|
| 43 |
security.declarePublic('recursivePublish') |
|---|
| 44 |
@postonly |
|---|
| 45 |
def recursivePublish(workspace, target_section_rpaths, context, REQUEST=None): |
|---|
| 46 |
"""Recursively publish all the content below the given workspace container |
|---|
| 47 |
into the given target sections. |
|---|
| 48 |
""" |
|---|
| 49 |
logger = getLogger(LOG_KEY + '.recursivePublish') |
|---|
| 50 |
logger.debug("target_section_rpaths = %s" % target_section_rpaths) |
|---|
| 51 |
utool = getToolByName(context, 'portal_url') |
|---|
| 52 |
portal = utool.getPortalObject() |
|---|
| 53 |
for rpath in target_section_rpaths: |
|---|
| 54 |
recursivePublishInFolder(workspace, rpath, context) |
|---|
| 55 |
logger.debug("DONE") |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
security.declarePrivate('recursivePublishInFolder') |
|---|
| 59 |
def recursivePublishInFolder(workspace, target_section_rpath, context): |
|---|
| 60 |
logger = getLogger(LOG_KEY + '.recursivePublishInFolder') |
|---|
| 61 |
utool = getToolByName(context, 'portal_url') |
|---|
| 62 |
ttool = getToolByName(context, 'portal_types') |
|---|
| 63 |
wtool = getToolByName(context, 'portal_workflow') |
|---|
| 64 |
portal = utool.getPortalObject() |
|---|
| 65 |
|
|---|
| 66 |
target_section = portal.restrictedTraverse(target_section_rpath, None) |
|---|
| 67 |
if target_section is None: |
|---|
| 68 |
raise ValueError("Folder %s doesn't exist." % target_section_rpath) |
|---|
| 69 |
|
|---|
| 70 |
for item_id, item in workspace.objectItems(): |
|---|
| 71 |
logger.debug("item = %s ..." % utool.getRpath(item)) |
|---|
| 72 |
|
|---|
| 73 |
# Don't publish special items such as configuration files |
|---|
| 74 |
if item_id.startswith('.'): |
|---|
| 75 |
continue |
|---|
| 76 |
|
|---|
| 77 |
# Don't publish documents which are locked or being worked on, |
|---|
| 78 |
# since they can't be published. |
|---|
| 79 |
proxy_info = context.getContentInfo(item) |
|---|
| 80 |
if proxy_info['review_state'] in ('locked', 'draft'): |
|---|
| 81 |
continue |
|---|
| 82 |
|
|---|
| 83 |
fti = ttool[item.portal_type] |
|---|
| 84 |
if fti.cps_proxy_type not in FOLDERISH_PROXY_TYPES: |
|---|
| 85 |
logger.debug("Publishing the document %s in the right section ..." |
|---|
| 86 |
% item_id) |
|---|
| 87 |
workflow_action = 'copy_submit' |
|---|
| 88 |
transition = 'publish' |
|---|
| 89 |
comments = 'Automatic recursive publishing.' |
|---|
| 90 |
wtool.doActionFor(item, workflow_action, |
|---|
| 91 |
dest_container=target_section_rpath, |
|---|
| 92 |
initial_transition=transition, |
|---|
| 93 |
comment=comments) |
|---|
| 94 |
logger.debug("Publishing the document %s in the right section DONE" |
|---|
| 95 |
% item_id) |
|---|
| 96 |
else: |
|---|
| 97 |
# Creating the folder if it doesn't exist yet |
|---|
| 98 |
target_folder = getattr(target_section, item_id, None) |
|---|
| 99 |
if target_folder is None: |
|---|
| 100 |
target_section.invokeFactory(type_name=SECTION_PORTAL_TYPE, |
|---|
| 101 |
id=item_id, |
|---|
| 102 |
) |
|---|
| 103 |
target_folder = getattr(target_section, item_id) |
|---|
| 104 |
# Setting target section title and description after the target |
|---|
| 105 |
# section has been created thus possibly synchronizing through |
|---|
| 106 |
# modification already created sections. |
|---|
| 107 |
folder_doc = item.getContent() |
|---|
| 108 |
target_folder_doc = target_folder.getEditableContent() |
|---|
| 109 |
target_folder_doc.edit(Title=folder_doc.Title(), |
|---|
| 110 |
Description=folder_doc.Description(), |
|---|
| 111 |
) |
|---|
| 112 |
|
|---|
| 113 |
# Then looping through the recursion |
|---|
| 114 |
recursivePublishInFolder(item, utool.getRpath(target_folder), |
|---|
| 115 |
context) |
|---|