4teamwork / ftw.zipexport

Zip Export for Plone
2 stars 6 forks source link

Plone 5 compatibility - zip_selected #42

Open mtrebron opened 7 years ago

mtrebron commented 7 years ago

In Plone 5, the zip_selected action is missing as folder actions doesn't work anymore with the new folder contents.

Unfortunately, documentation for adding folder actions is missing from plone.app.content

jone commented 7 years ago

ftw.zipexport is not yet officially Plone 5 compatible. We are currently updating our stack to Plone 5 and we will hopefully be able to update ftw.zipexport in the next months.

mtrebron commented 7 years ago

Cheers! I wrote a few lines of code to extend the DexterityItemZipRepresentation class, so that INamedImageField can also be used.

Not sure a PR is useful - you might want to refactor it.

def get_files(self, path_prefix=u"", recursive=True, toplevel=True):
    try:
        primary_adapter = getAdapter(self.context,
                                     interface=IPrimaryFieldInfo)
    except TypeError:
        # if no primary field is available PrimaryFieldInfo
        # Adapter throws TypeError
        return

    if INamedFileField.providedBy(primary_adapter.field) or INamedImageField.providedBy(primary_adapter.field):
        named_file = primary_adapter.value
        if primary_adapter.value:
            yield self.get_file_tuple(named_file, path_prefix)

def get_file_tuple(self, named_file, path_prefix):
    path = u'{0}/{1}'.format(safe_unicode(path_prefix),
                             safe_unicode(named_file.filename))

    # Modified default behavior to provide thumbnail zip download functionality
    if INamedBlobImage.providedBy(named_file):
        # resize_type is provided in request
        # custom_scale_name is defined in plone config > images
        width = 140
        height = 40
        default_scale_type = 'thumbnail'

        resize_type = getattr(self.request, 'resize_type', default_scale_type)
        custom_scale_name = '%s_zipdownload' % resize_type

        getAvailableSizes = queryUtility(IAvailableSizes)
        available = getAvailableSizes()

        if custom_scale_name in available:
            width, height = available[custom_scale_name]

        data =  StringIO(scaleImage(named_file.data, width, height, resize_type)[0])

        return (path, data)

    elif HAVE_BLOBS and INamedBlobFile.providedBy(named_file):
        return (path, named_file.open())
    else:
        stream_data = StringIO(named_file.data)
        return (path, stream_data)