Ticket #1591 (new defect)

Opened 14 years ago

Last modified 13 years ago

Error creating "News Item" with photo in CPSRemoteController

Reported by: juan.lago@… Owned by: rspivak
Priority: P1 Milestone: CPS 3.5.7
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 Download (4.5 KB) - added by tracguest 13 years ago.
patch for CPSRemoteController to add 'News Item' with images
__init__.py Download (895 bytes) - added by tracguest 13 years ago.

Change History

comment:1 Changed 14 years ago by sfermigier

  • Milestone changed from CPS 3.4.0 to CPS 3.4.1

comment:2 Changed 13 years ago by sfermigier

  • Milestone changed from CPS 3.4.1 to CPS 3.4.2

comment:3 Changed 13 years ago by sfermigier

  • Milestone changed from CPS 3.4.2 to CPS 3.4.3

Changed 13 years ago by tracguest

patch for CPSRemoteController to add 'News Item' with images

comment:4 Changed 13 years ago 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.

Changed 13 years ago by tracguest

Note: See TracTickets for help on using tickets.