Skip to main content

jQuery File Upload integration for cone.app

Project description

Latest PyPI version Number of PyPI downloads https://travis-ci.org/bluedynamics/cone.fileupload.svg?branch=master https://coveralls.io/repos/github/bluedynamics/cone.fileupload/badge.svg?branch=master

This package integrates jQueryFileUpload <https://github.com/blueimp/jQuery-File-Upload>_ in cone.

Currently, version 10.32.0 is included.

Included files of jQuery File Upload:

  • jquery.iframe-transport.js

  • jquery.fileupload.js

  • jquery.fileupload-process.js

  • jquery.fileupload-ui.js

  • jquery.fileupload-validate.js

Additionally, v3.20.0 of Javascript-Templates <https://github.com/blueimp/JavaScript-Templates>_ is included.

Usage

Since cone.app not knows about the underlying data, cone.fileupload only provides an abstract server implementation.

So first we need to provide a model.

from cone.app.model import BaseNode
from pyramid.security import ALL_PERMISSIONS
from pyramid.security import Allow
from pyramid.security import Deny
from pyramid.security import Everyone

ACL = [
    (Allow, 'role:manager', ['add', 'delete']),
    (Allow, Everyone, ['login']),
    (Deny, Everyone, ALL_PERMISSIONS),
]

class Container(BaseNode):
    __acl__ = ACL

    def __call__(self):
        """Persistence happens here.
        """

class File(BaseNode):
    __acl__ = ACL
    # allow setting any value types
    allow_non_node_children = True

Now we need to provide a concrete FileUploadHandle implementation for our model.

from cone.fileupload.browser.fileupload import FileUploadHandle
from pyramid.view import view_config

@view_config(
    name='fileupload_handle',
    context=Container, # <- here the view gets bound to our model
    accept='application/json',
    renderer='json',
    permission='add')
class ContainerFileUploadHandle(FileUploadHandle):

    def create_file(self, stream, filename, mimetype):
        # this function gets called for persisting uploaded files
        file = self.model[filename] = File()
        file['body'] = stream.read()
        return {
            'name': filename,
            'size': len(file['body']),
            'url': '/{0}'.format(file.name),
            'deleteUrl': '/{0}/filedelete_handle'.format(file.name),
            'deleteType': 'GET',
        }

    def read_existing(self):
        # this function gets called for initial reading of existing files
        files = list()
        for node in self.model.values():
            files.append({
                'name': node.name,
                'size': len(node['body']),
                'url': '/{0}'.format(node.name),
                'deleteUrl': '/{0}/filedelete_handle'.format(node.name),
                'deleteType': 'GET',
            })
        return files

Optionally we might want to provide a custom fileupload tile for our model.

from cone.tile import tile
from cone.fileupload.browser.fileupload import FileUploadTile

@tile(
    name='fileupload',
    path='cone.fileupload:browser/fileupload.pt',
    interface=Container,
    permission='add')
class ContainerFileUploadTile(FileUploadTile):
    accept_file_types = '/(\.|\/)(gif|jpg)$/i'

The file upload actions are either rendered as dedicated tile by name fileupload_toolbar or integrated into the context menu. If it’s desired to display the action in the context menu, fileupload_contextmenu_actions flag must be set on model properties.

Contributors

  • Robert Niederreiter

Changes

0.6 (2021-11-21)

  • Fileupload actions optionally work from contextmenu. [rnix]

  • Move button toolbar into dedicated tile for customization. [rnix]

  • Reduce included files and plugins of jquery fileupload to required ones. [rnix]

  • Update jquery fileupload to version 10.32.0. [rnix]

0.5 (2021-11-08)

  • Rename deprecated allow_non_node_childs to allow_non_node_children in tests and documentation. [rnix]

0.4 (2020-05-30)

  • Initial pypi release [rnix]

0.3

  • Python 3 compatibility. [rnix]

  • Convert doctests to unittests. [rnix]

  • Use cone.app.main_hook decorator. [rnix]

  • Move resource registration to main hook. [rnix]

  • Upgrade to cone.app 1.0b1. [rnix]

0.2

  • Code organization. [rnix]

0.1

  • Make it work [rnix]

License

Copyright (c) 2013-2021, BlueDynamics Alliance, Austria Copyright (c) 2021, Cone Contributors All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cone.fileupload-0.6.tar.gz (43.0 kB view details)

Uploaded Source

File details

Details for the file cone.fileupload-0.6.tar.gz.

File metadata

  • Download URL: cone.fileupload-0.6.tar.gz
  • Upload date:
  • Size: 43.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3

File hashes

Hashes for cone.fileupload-0.6.tar.gz
Algorithm Hash digest
SHA256 a90a36bfc6e3b2e6f92d8eb22ad71dbc2bbc191e4cbd5e5fc95d74f3da3120cb
MD5 1ee8cee3e2521216087926ece5d83898
BLAKE2b-256 dc90ade97f27f5f8e8af6cf952911dc66a2a927d056f1bae5a3b3306c9b67692

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page