Ticket #1591 (new defect)

Opened 3 years ago

Last modified 3 years ago

Error creating "News Item" with photo in CPSRemoteController

Reported by: juan.lago@iavante.es Assigned to: rspivak
Priority: P1 Milestone: CPS 3.5.0
Component: CPSRemoteController Version: 3.4.0
Severity: normal Keywords:
Cc:

Description

I'm trying to create some documents in a CPS Default Site (3.4) from a client python application using CPSRemoteController. I have problems when creating a "News Item" with a photo (photo field in schema). I've used the xmlrpc Binary object (like File document examples in "Understanding and using the CPS Remote Controller") but when I access the News Item created in CPS it appears a blinking "!!!". If I try to edit the document created, there's an AttributeError?: Binary instance has no attribute 'size'.

Here you are the source code of the client application:

# -*- coding: ISO-8859-15 -*-
from xmlrpclib import ServerProxy, Binary
SERVER = 'myIPaddress'
PORT = '8081'
PORTAL = 'myportal'
USER = 'myuser'
PWD = 'mypwd'

connectString = 'http://%s:%s@%s:%s/%s/portal_remote_controller' % (USER,PWD,SERVER,PORT,PORTAL) proxy = ServerProxy(connectString)

# Read image from file
f = open('banner.jpg')
binary = Binary(f.read())
f.close()

# Document definition
doc_def = {
    'Title': 'A news item example',
    'Description': 'The description goes here',
    'photo': binary,
    }
try:
    proxy.createDocument('News Item', doc_def, 'workspaces', 0, 'No comment')
except:
    print 'Error'
 
print "End ---"

Attachments

PatchAddImageToDocument.py (4.5 kB) - added by tracguest on 10/25/06 16:31:04.
patch for CPSRemoteController to add 'News Item' with images
__init__.py (0.9 kB) - added by tracguest on 10/25/06 16:33:27.

Change History

04/04/06 19:06:25 changed by sfermigier

  • milestone changed from CPS 3.4.0 to CPS 3.4.1.

05/19/06 17:07:26 changed by sfermigier

  • milestone changed from CPS 3.4.1 to CPS 3.4.2.

05/19/06 17:08:24 changed by sfermigier

  • milestone changed from CPS 3.4.2 to CPS 3.4.3.

10/25/06 16:31:04 changed by tracguest

  • attachment PatchAddImageToDocument.py added.

patch for CPSRemoteController to add 'News Item' with images

10/25/06 16:33:04 changed by tracguest

I have had the same problem and I have found a possible solution.

I have implemented a patch (attached file) that modify the RemoteControllerTool? module:

  1. Add a constant key "BINARY_FILE_TYPE = 'file_type'" to define the type of the uploaded file (It is suppose to be a File object, but if we want to upload Images we have to make a distinction).
  2. New implementation of the _editDocument method, where we have to get the file_type variable from the doc_def dictionary:
             file_type = doc_def.get(BINARY_FILE_TYPE,None)
    
  3. And where we check whether the file is an Image or a File (in _editDocument as well):
           ...
           if file is not None:
               if isinstance(file, Binary):
                   file_id = generateFileName(file_name)
                   # Checking whether the file is an Image/Photo or a File
                   if file_type is 'Image' or 'Photo':
                       doc_def[file_key] = Image(file_id, file_name, file.data)
                   else:
                       doc_def[file_key] = File(file_id, file_name, file.data)
                   elif isinstance(file, File):
                       # CPSDistantPublisher is able to directly send File instances
                       doc_def[file_key] = file
           ...
    

In a test:

   ...
   f = open('<image.gif>')
   binary = Binary(f.read())
   f.close()
   ...
   doc_def = {'Title': <doc_title>, 
              'Description': <doc_desc>,
              'file' : binary,                    
              'file_name' : <photo_name>,
              'file_key' : 'photo',
              'file_type' : 'Image',
              }      
   try:
       proxy.createDocument('News Item', doc_def, 'workspaces', 0, 'No comment')
   except:
       print 'Error'    
   ...

where:

  • "<doc_type>" is the type of the document to be created.
  • "<doc_def>" is a dictionary containing the document data:
    • "Title" is the title of the document
    • "Description" is the document description
    • "file" is the binary instance of the uploaded image.
    • "file_name" is the name of the uploaded file.
    • "file_key" is the name of the ATTRIBUTE where the image is going to be stored (schema!).
    • "file_type" is the type of the uploaded file: Image/Photo or File

After these changes, the test create a 'News Item' with image with no problem.

10/25/06 16:33:27 changed by tracguest

  • attachment __init__.py added.