Skip to content. | Skip to navigation

Navigation

You are here: Home / Support / Guides / Tools / Plone / Adding Plone Pages Remotely / The External Method

Personal tools

Adding Plone Pages Remotely

The External Method

The External Method needs to:

  1. extract the unique_id representing the file containing the news article
  2. read the information in
  3. create the news article
  4. publish

The code will set the title to be a simple string. The body will be created using restructedText which requires setting the MIME type when we set the body text and start with a pretty printed version of the timestamp as a heading followed by the body text read from the file.

The various strings returned are then available for inspection to determine success or failure. It's not clear whether a command line utility should report an error when the transport of the HTTP error code was successful.

The Python code looks like:

import time

def news_update(self):
   """A first external method."""
   news_id = self.REQUEST.get('news_id',None)

   if news_id is None:
       return "No news identifier passed\n"

   news_file = '/path/to/news/%s.txt' % news_id

   if os.path.isfile (news_file) is None:
       return "News article doesn't exist\n"

   # recover the time from news_id
   when = time.strptime (news_id, "%Y%m%d%H%M%S")

   news = open (news_file)
   body = None
   location = ''
   location_msg = ''
   try:
       for line in news:
           # strip the strailing newline
           line = line.rstrip ()

           # Leading spaces which mean blockquote in text/x-rst!
           body += line.lstrip ()
   finally:
       news.close

   if body:
       # ensure a unique URL by appending a suffix if the id already
       # exists
       base_id = time.strftime ("%Y%m%d%H%M%S", when)
       id = base_id
       news_item = getattr (self, id, None)

       suffix = 2
       while news_item is not None:
           id = '%s-%d' % (base_id, suffix)
           news_item = getattr (self, id, None)
           suffix = suffix + 1

       # Create the News Item
       self.invokeFactory (type_name="News Item", id=id)
       news_item = self[id]

       news_item.setTitle ('News Update')

       description = 'News Update for %s' % time.strftime ("%A, %d %B, %Y %H:%M", when)
       if suffix > 2:
           description += ' #%d' % (suffix - 1)

       news_item.setDescription (description)

       # Create a restructuredText H1 from a date string underlined
       # with '=' chars
       date_h1 = time.strftime ("%A, %d %B, %Y %H:%M", when)
       h1 = "%s\n%s\n\n" % (date_h1, '=' * len (date_h1))
       news_item.setText (h1 + body, mimetype='text/x-rst')

       # {Creation,Modification,Effective}Date is used for sorting
       # News Items by default in the View listing??
       when_str = time.strftime('%Y/%m/%d %H:%M:%S', when)
       news_item.setCreationDate (when_str)

       # ModificationDate should reflect when we've edited this
       # news_item.setModificationDate (when_str)

       # EffectiveDate is used for sorting News Items by default in
       # the Contents listing??
       news_item.setEffectiveDate (when)

       # publish
       news_item.portal_workflow.doActionFor (news_item, "publish", comment="publised programmatically")
   else:
       return "No body generated for %s\n" % news_id

   return "Successfully added %s\n" % (news_id)

Document Actions