HOW TO Import file attachments

Eric BREHAULT edited this page Sep 23, 2016 · 1 revision

When we migrate some data from a third-party system to Plomino, CSV is a good format, but it odes allow to import attachement. Here is how to solve this problem.

Prepare our CSV file

Our CSV file must contains all the field values but also an extra column containing the path to attachments.

Depending on how our files will be accessible from the Plone server, it might be a URL (http, ftp, etc.) or it might be a local path (file://).

Let's assume we produce the following CSV:

"Victor Hugo","Les misérables","http://myintranet/covers/miserables.jpg"
"Joseph Conrad","Lord Jim","http://myintranet/covers/lordjim.jpg" 

Import the CSV file

We just use the Plomino CSV import feature in the Replication tab to import our CSV.

It will create the corresponding documents, and they will contains an extra field named 'cover_url'.

Create a post-processing agent

Now we need to create an agent that will read the file url, load the corresponding file, and attach it in the document:

db = context.getParentDatabase()
for doc in db.getParentDatabase():
    url = doc.getItem("cover_url")
        photo = open_url(cover_url, asFile=True)
        # bad url
        photo = None
    if photo:
        (new_file, contenttype) = doc.setfile(photo, filename=cover_url.split('/')[-1])
        doc.setItem('cover', {new_file: contenttype})

Authorize the domain

By default, for security reason, the open_url method is not allowed to load any file.

We need to enable the needed domain by declaring them safe that way:

from zope.interface import implements
from zope.component import provideUtility
from Products.CMFPlomino.interfaces import IPlominoSafeDomains

class MySafeDomains:

    domains = [
provideUtility(MySafeDomains, IPlominoSafeDomains)

(this piece of code must be located in your skin product or any custom product you might have on your Plone portal).

Run the agent

Now you can run the agent, and your files should be imported in your documents.