## Script (Python) "xmlrpc-postdoc"    
##parameters=dest, doc_id, title, body, doc_type='Document',    
format='structured-text', state='publish', description='',    
edate=None, subject=''    
##title=    
##    
"""    
This script is to allow posting of Plone documents via XML-RPC.    
The new document's absolute URL string is returned on success.    
    
You *may* need to grant View privileges to Authenticated     
(or some other more restricted) role for this to work. 
I place the script in my plone root folder and work from 
there. The login used must be at least have Reviewer role 
to create objects with "publish" state.     
    
dest    
   A folder or subfolder, relative to your url (eg: Members/x).    
   No error checking, so it better exist!    
    
doc_id    
   The ID the document will be accessed with    
    
title    
   Maps to "headline" for news items    
    
description    
   Maps to "lead in" for News Items.    
    
body    
   The main text of the document    
    
doc_type    
   CMF/Plone document type. 'Document' is the default.    
   'News Item' is another. See portal_types in ZMI.    
   This script may need tweaking for some content types    
   with different properties.    
    
format    
   Whatever your plone instance supports. Default is     
   structured-text; other plone defaults are plain and html.    
    
state    
   Workflow state. Default is 'publish' as it is assumed    
   that any document you go to the trouble to create     
   programmatically you want published right away. The    
   plone default, however, is 'visible'.    
    
edate    
   The DublinCore EffectiveDate property will be set to this.     
   It must be parsable by Zope's DateTime. This date is used    
   for sorting in the "news" listing, for example.    
    
subject    
   Maps to "keywords" in Plone. To attach multiple keywords    
   separate them with semicolons (e.g. Meeting;Event)    
       
    
An example with RPC (note that RPC calls do not accept     
keyword parameters as far as I know, so you have to use    
all positional parameters)::    
    
  import xmlrpclib    
  s = xmlrpclib.ServerProxy('http://login:pass@beta.vex.net/')    
  s.postnews('Members/x/News', 'rpc1', 'title',     
        'Hi, I am testing rpc!', 'News Item', 'plain',     
        state='visible')    
    
Tim Middleton, for http://www.vex.net    
"""    
    
folder = context    
for f in dest.split('/'):    
    if f:    
        folder = folder[f]    
    
try:    
    document = getattr(folder, doc_id)    
    #raise ValueError, "Object Exists, we refuse to modify it"    
except:    
    docobj = folder.invokeFactory(doc_type, doc_id)    
    document = getattr(folder, doc_id)    
        
subj = subject.split(';')    
    
document.setTitle(title)    
document.editMetadata(title=title, description=description,     
        subject=subj)    
    
if edate:    
    # edate better be parsable...    
    ets = DateTime(edate)    
    document.setEffectiveDate(ets)    
    
document.edit(text_format=format, text=body)    
    
    
review_state = document.portal_workflow.getInfoFor(document,     
        'review_state', '')    
if not review_state in ('rejected', 'retracted', state):    
    document.portal_workflow.doActionFor(document, state,     
            comment='')    
    
return document.absolute_url()    
