-
Notifications
You must be signed in to change notification settings - Fork 37
HOW TO Import file attachments
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:
"Author","Book","cover_url"
"Victor Hugo","Les misérables","http://myintranet/covers/miserables.jpg"
"Joseph Conrad","Lord Jim","http://myintranet/covers/lordjim.jpg"
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'.
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")
try:
photo = open_url(cover_url, asFile=True)
except:
# bad url
photo = None
if photo:
(new_file, contenttype) = doc.setfile(photo, filename=cover_url.split('/')[-1])
doc.setItem('cover', {new_file: contenttype})
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:
implements(IPlominoSafeDomains)
domains = [
"http://myintranet",
]
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).
Now you can run the agent, and your files should be imported in your documents.