You are not logged in Log in Join
You are here: Home » Members » camil7 » Bulk upload of images

Log in
Name

Password

 

Created by camil7 . Last modified 2006-03-27 15:56:47.

Bulk upload of Images

Sometimes (especially when migrating from a non-Zope site) one wants to upload a bulk of images. In this tip I tell you how to do this with a few lines of Python code.

If you ever find yourself in the position of someone having to upload a lots of images, the Zope Management Interface can be quite annoying as it allows only a one by one upload, and you will find yourself trapped in a click orgy if you try to do so.

If you have a WebDAV client at hand, this will be quite easy(, I guess), but what is if you have none? (I have not tried if WS_FTP works as it does not run on my operation system. Alternatively one could try a NFS adapter like davfs and simply copy the images into Zope.

However I guess Zope thinks the uploads/copies are DTML which will result in a mess. Please mail me, if you know something about it.)


In this tip I give a few small building steps that allows a hand made bulk upload in case you have access to the file system where Zope is running. The steps include an external method, a script to call this method conveniently (which could be a DTML-method as well) and a simple DTML method to trigger the script. It is all very rude; I hope you can guess how to adapt it to your special needs.

The External Method

First go to the "Extensions" directory of the root of your Zope installation. Create a new file there with the name upload.py and the following content:

    import sys, os, string
    def uploadAllImages(context):
        ''' upload all files in file pointed to by the request
        parameter "uploadDir" into the folder "context"
        "uploadDir" should be a valid pathname on the local file system
        containing only images
        '''
        msg = []
        files = []
        filename = context.REQUEST['uploadDir']
        try:
           files = os.listdir(filename)
        except:
           msg.append('<h2>Cannot list any files in ' +filename+'</h2>')
        for file in files:
           fd = open(filename+os.sep+file,'r')
           image = fd.read()
           fd.close()
           context.manage_addImage(file, image)
           msg.append('<li>added image '+file+' </li>')
        return '<html><head></head><body><ul>'+string.join(msg)+'</ul></body></html>'

Point your browser to the ZMI and create a "External Method" in the root folder.

The external method may get the id bulk_upload, some nice title, and must have the Module Name upload and the Function Name uploadAllImages. Maybe you must restart your Zope, if it does not find the extension method.

The wrapper script

To pass the context variable I have found it necessary to create a wrapper script. (Maybe I will find a way to get rid of this some time, or if someone gives be a hint.)

The Python script should have the id trigger_upload, maybe some nice title, and the one line content:

     return container.bulk_upload(context)
  

The DTML trigger

For convenient calling, add a DTML Document with id uploadForm, some title like Upload form for images, and the contents:

    <dtml-var standard_html_header>
    <form action="trigger_upload">
      Add a remote directory here where all images should be imported from<br />;
      <input type="text" name="uploadDir" />
      <input type="submit" value="import" />
    </form>
    <dtml-var standard_html_footer>

Now You are ready to do some bulk upload:

  1. Copy the images you want to upload into an empty directory in the file system, where Zope can access them.
  2. Point your browser to the url /my_image_folder/uploadForm where /my_image_folder is the folder into which you want to import the images. The folder must already exist.
  3. In the input field type in the path name of the directory, where you copied your files in the first step and submit the form.
  4. If you misspelled the directory name, you get an error message. Hit the back button of your browser and try again.
  5. You now should get a report of your import. You also may check via the ZMI that the images were properly uploaded.
Some caveats:
  • I have tested this script with Zope 2.5.0 only.
    Gus Gollings reports it works with Zope 2.7.4 / python 2.3.5.
    Alpha Dia reports it still works with Zope 2.9.0.

    Please let me know if it does or does not run with other Zope versions.

  • Better create an empty Folder and import your images there. Otherwise you can get into trouble because there is no simple Undo in case of mistakes.
    You can "select all" in the Folder and copy the images to their proper place in the ZMI later.
  • Put only images in the directory. The script is not smart enough to tell images from non-images. (I have not tried what happens if you put a non image there. I guess Zope will jump on you with an error message.)
  • The script does not truncate suffixes from the filename, nor does it have any error handling. If you know a little Python, you can easily add some nifty features.