예제 #1
0
파일: utils.py 프로젝트: Jickelsen/Arche
 def formdata_dict(self, key):
     blob = self.get(key)
     if blob:
         return filedict(mimetype = blob.mimetype,
                         size = blob.size,
                         filename = blob.filename,
                         uid = None) #uid has to be there to make colander happy
예제 #2
0
    def deserialize(self, field, pstruct):
        logger.debug('widget:deserialize, %s, %s', field, pstruct)

        if pstruct is null:
            return null

        upload = pstruct.get('upload')
        uid = pstruct.get('uid')

        if hasattr(upload, 'file'):
            # the upload control had a file selected
            blob_info = blobstore.parse_blob_info(upload)
            data = filedict(self.tmpstore._to_dict(blob_info))

            if uid:
                # previous file exists
                del self.tmpstore[uid]
        else:
            # the upload control had no file selected
            if not uid:
                # no previous file exists
                return null
            else:
                # a previous file should exist
                data = self.tmpstore.get(uid)
                # but if it doesn't, don't blow up
                if data is None:
                    return null

        return data
예제 #3
0
파일: widgets.py 프로젝트: Jickelsen/Arche
    def deserialize(self, field, pstruct):
        if pstruct is null:
            return null
        upload = pstruct.get('upload')
        uid = pstruct.get('uid')

        if hasattr(upload, 'file'):
            # the upload control had a file selected
            data = filedict()
            data['fp'] = upload.file
            filename = upload.filename
            # sanitize IE whole-path filenames
            filename = filename[filename.rfind('\\')+1:].strip()
            data['filename'] = filename
            data['mimetype'] = upload.type
            data['size']  = upload.length
            if uid is None:
                # no previous file exists
                while 1:
                    uid = self.random_id()
                    if self.tmpstore.get(uid) is None:
                        data['uid'] = uid
                        self.tmpstore[uid] = data
                        preview_url = self.tmpstore.preview_url(uid)
                        self.tmpstore[uid]['preview_url'] = preview_url
                        break
            else:
                # a previous file exists
                data['uid'] = uid
                self.tmpstore[uid] = data
                preview_url = self.tmpstore.preview_url(uid)
                self.tmpstore[uid]['preview_url'] = preview_url
        elif pstruct.get('delete') == 'delete':
            data = filedict(delete = 'delete')
        else:
            # the upload control had no file selected
            if uid is None:
                # no previous file exists
                return null
            else:
                # a previous file should exist
                data = self.tmpstore.get(uid)
                # but if it doesn't, don't blow up
                if data is None:
                    return null
        return data
예제 #4
0
파일: utils.py 프로젝트: Jickelsen/Arche
 def formdata_dict(self, key):
     blob = self.get(key)
     if blob:
         return filedict(
             mimetype=blob.mimetype,
             size=blob.size,
             filename=blob.filename,
             uid=None)  #uid has to be there to make colander happy
    def deserialize(self, field, pstruct):
        if pstruct is colander.null:
            return colander.null

        if not isinstance(pstruct, dict):
            pstruct = self.tmpstore.get(pstruct)
            if pstruct is None:
                return colander.null

        if pstruct.get('is_external', False):
            return pstruct

        upload = pstruct.get('upload')
        uid = pstruct.get('uid')

        if hasattr(upload, 'file'):
            # the upload control had a file selected
            data = filedict()
            data['fp'] = upload.file
            filename = upload.filename
            # sanitize IE whole-path filenames
            filename = filename[filename.rfind('\\')+1:].strip()
            data['filename'] = filename
            data['mimetype'] = upload.type
            data['size']  = upload.length
            if uid is None:
                # no previous file exists
                while 1:
                    uid = self.random_id()
                    if self.tmpstore.get(uid) is None:
                        data['uid'] = uid
                        self.tmpstore[uid] = data
                        preview_url = self.tmpstore.preview_url(uid)
                        data['preview_url'] = preview_url
                        self.tmpstore[uid]['preview_url'] = preview_url
                        break
            else:
                # a previous file exists
                data['uid'] = uid
                self.tmpstore[uid] = data
                preview_url = self.tmpstore.preview_url(uid)
                self.tmpstore[uid]['preview_url'] = preview_url
        else:
            # the upload control had no file selected
            if uid is None or not uid:
                # no previous file exists
                return colander.null
            else:
                # a previous file should exist
                data = self.tmpstore.get(uid)
                data['preview_url'] = data['filepath']
                # but if it doesn't, don't blow up
                if data is None:
                    return colander.null

        return data['preview_url']
예제 #6
0
 def serialize(self, node: SchemaNode, value: File) -> filedict:
     """Serialize File value to filedict."""
     if not value:
         return colander.null
     cstruct = filedict([('mimetype', value.mimetype),
                         ('size', value.size),
                         ('uid', str(hash(value))),
                         ('filename', value.title),
                         # prevent file data is written to tmpstore
                         ('fp', None),
                         ])
     return cstruct
예제 #7
0
 def serialize(self, node: SchemaNode, value: File) -> filedict:
     """Serialize File value to filedict."""
     if not value:
         return colander.null
     cstruct = filedict([('mimetype', value.mimetype),
                         ('size', value.size),
                         ('uid', str(hash(value))),
                         ('filename', value.title),
                         # prevent file data is written to tmpstore
                         ('fp', None),
                         ])
     return cstruct
예제 #8
0
    def get(self, name, default=None):
        """
        Get the filedict object for the given name.

        @param name(str): this may be the relative path to the file or a url.
        @param default(filedict): the object to return if none are found.
        """
        file_name = self._get_full_file_name(name)
        if file_name is None:
            return default

        data = filedict()
        with open(file_name, "rb") as reader:
            data["fp"] = BytesIO(reader.read())

        data["uid"] = self._get_uid(name)
        data["filename"] = path.basename(file_name)
        data["preview_url"] = None
        return data
예제 #9
0
 def get(self, name, default=None):
     #log.debug("get(%s, %s)" % (repr(name), repr(default)))
     files_dict = self.get_file_mapping()
     _id = files_dict.get(name)
     if _id:
         try:
             result = self.gridfs.get(_id)
         except NoFile, e:
             return default
         return filedict(
             fp=result,
             mimetype=result.content_type,
             uid=name,
             preview_url=self.preview_url_for_id(_id),
             filename=result.name,
             size=result.length,
             _id=_id,
             parents=result.parents
         )
예제 #10
0
파일: schema.py 프로젝트: adroullier/deform
    def serialize(self, node, value):
        """
        Serialize a dictionary representing partial file information
        to a dictionary containing information expected by a file
        upload widget.
        
        The file data dictionary passed as ``value`` to this
        ``serialize`` method *must* include:

        filename
            Filename of this file (not a full filesystem path, just the
            filename itself).

        uid
            Unique string id for this file.  Needs to be unique enough to
            disambiguate it from other files that may use the same
            temporary storage mechanism before a successful validation,
            and must be adequate for the calling code to reidentify it
            after deserialization.

        A fully populated dictionary *may* also include the following
        values:

        fp
            File-like object representing this file's content or
            ``None``.  ``None`` indicates this file has already been
            committed to permanent storage.  When serializing a
            'committed' file, the ``fp`` value should ideally not be
            passed or it should be passed as ``None``; ``None`` as an
            ``fp`` value is a signifier to the file upload widget that
            the file data has already been committed.  Using ``None``
            as an ``fp`` value helps prevent unnecessary data copies
            to temporary storage when a form is rendered, however its
            use requires cooperation from the calling code; in
            particular, the calling code must be willing to translate
            a ``None`` ``fp`` value returned from a deserialization
            into the file data via the ``uid`` in the deserialization.

        mimetype
            File content type (e.g. ``application/octet-stream``).

        size
            File content length (integer).

        preview_url
            URL which provides an image preview of this file's data.

        If a ``size`` is not provided, the widget will have no access
        to size display data.  If ``preview_url`` is not provided, the
        widget will not be able to show a file preview.  If
        ``mimetype`` is not provided, the widget will not be able to
        display mimetype information.
        """
        if value in (colander.null, None, ""):
            return colander.null
        
        if not hasattr(value, 'get'):
            mapping = {'value':repr(value)}
            raise colander.Invalid(
                node,
                _('${value} is not a dictionary', mapping=mapping)
                )
        for n in ('filename', 'uid'):
            if not n in value:
                mapping = {'value':repr(value), 'key':n}
                raise colander.Invalid(
                    node,
                    _('${value} has no ${key} key', mapping=mapping)
                    )
        result = widget.filedict()
        result['filename'] = value['filename']
        result['uid'] = value['uid']
        result['mimetype'] = value.get('mimetype')
        result['size'] = value.get('size')
        result['fp'] = value.get('fp')
        result['preview_url'] = value.get('preview_url')
        return result
예제 #11
0
파일: schema.py 프로젝트: sbrauer/deform
    def serialize(self, node, value):
        """
        Serialize a dictionary representing partial file information
        to a dictionary containing information expected by a file
        upload widget.
        
        The file data dictionary passed as ``value`` to this
        ``serialize`` method *must* include:

        filename
            Filename of this file (not a full filesystem path, just the
            filename itself).

        uid
            Unique string id for this file.  Needs to be unique enough to
            disambiguate it from other files that may use the same
            temporary storage mechanism before a successful validation,
            and must be adequate for the calling code to reidentify it
            after deserialization.

        A fully populated dictionary *may* also include the following
        values:

        fp
            File-like object representing this file's content or
            ``None``.  ``None`` indicates this file has already been
            committed to permanent storage.  When serializing a
            'committed' file, the ``fp`` value should ideally not be
            passed or it should be passed as ``None``; ``None`` as an
            ``fp`` value is a signifier to the file upload widget that
            the file data has already been committed.  Using ``None``
            as an ``fp`` value helps prevent unnecessary data copies
            to temporary storage when a form is rendered, however its
            use requires cooperation from the calling code; in
            particular, the calling code must be willing to translate
            a ``None`` ``fp`` value returned from a deserialization
            into the file data via the ``uid`` in the deserialization.

        mimetype
            File content type (e.g. ``application/octet-stream``).

        size
            File content length (integer).

        preview_url
            URL which provides an image preview of this file's data.

        If a ``size`` is not provided, the widget will have no access
        to size display data.  If ``preview_url`` is not provided, the
        widget will not be able to show a file preview.  If
        ``mimetype`` is not provided, the widget will not be able to
        display mimetype information.
        """
        if value is colander.null:
            return colander.null

        if not hasattr(value, 'get'):
            mapping = {'value': repr(value)}
            raise colander.Invalid(
                node, _('${value} is not a dictionary', mapping=mapping))
        for n in ('filename', 'uid'):
            if not n in value:
                mapping = {'value': repr(value), 'key': n}
                raise colander.Invalid(
                    node, _('${value} has no ${key} key', mapping=mapping))
        result = widget.filedict()
        result['filename'] = value['filename']
        result['uid'] = value['uid']
        result['mimetype'] = value.get('mimetype')
        result['size'] = value.get('size')
        result['fp'] = value.get('fp')
        result['preview_url'] = value.get('preview_url')
        return result