Example #1
0
 def test_deserialize_file(self):
     from nive.components.reform.schema import null
     from nive.helper import File
     from StringIO import StringIO
     schema = DummySchema()
     field = DummyField(schema)
     widget = self._makeOne()
     file = File(file=StringIO("contents"))
     result = widget.deserialize(field, file)
     self.assertEqual(result, "contents")
Example #2
0
 def test_deserialize_file_del(self):
     from nive.components.reform.schema import null
     from nive.helper import File
     schema = DummySchema()
     field = DummyField(schema)
     field.name="fname"
     widget = self._makeOne()
     file = File()
     result = widget.deserialize(field, "", {"fname_delfile":"1"})
     self.assertEqual(result, "")
Example #3
0
 def test_deserialize_file(self):
     from nive.components.reform.schema import null
     from nive.helper import File
     schema = DummySchema()
     field = DummyField(schema)
     widget = self._makeOne()
     file = File()
     result = widget.deserialize(field, file)
     self.assertEqual(result.filename, file.filename)
     self.assertEqual(result.file, file.file)
Example #4
0
 def deserialize(self, field, pstruct, formstruct=None):
     if pstruct in ("", null, None):
         return null
     file = File()
     file.filename = pstruct.filename
     file.file = pstruct.file
     file.filekey = field.name
     #file.uid = pstruct.uid
     file.mime = pstruct.type
     file.size = pstruct.length
     file.tempfile = True
     return file
Example #5
0
 def deserialize(self, field, pstruct, formstruct=None):
     if pstruct in ("", null, None):
         return null
     file = File()
     file.filename = pstruct.filename
     file.file = pstruct.file
     file.filekey = field.name
     # file.uid = pstruct.uid
     file.mime = pstruct.type
     file.size = pstruct.length
     file.tempfile = True
     return file
Example #6
0
    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 (null, None, ""):
            return null

        if not hasattr(value, 'get'):
            mapping = {'value': repr(value)}
            raise Invalid(node,
                          _('${value} is not a dictionary', mapping=mapping))
        for n in ('filename', ):
            if not n in value:
                mapping = {'value': repr(value), 'key': n}
                raise Invalid(node,
                              _('${value} has no ${key} key', mapping=mapping))
        if isinstance(value, basestring):
            # from path
            file = File()
            file.fromPath(value)
            return file

        elif not isinstance(value, File):
            # dictionary or similar
            file = File()
            file.filename = value.get('filename', '')
            file.file = value.get('file')
            file.filekey = node.name
            file.uid = value.get('uid', node.name)
            file.mime = value.get('mimetype')
            file.size = value.get('size')
            file.tempfile = True
            return file
        return value
Example #7
0
    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 (null, None, ""):
            return null

        if not hasattr(value, "get"):
            mapping = {"value": repr(value)}
            raise Invalid(node, _("${value} is not a dictionary", mapping=mapping))
        for n in ("filename",):
            if not n in value:
                mapping = {"value": repr(value), "key": n}
                raise Invalid(node, _("${value} has no ${key} key", mapping=mapping))
        if isinstance(value, basestring):
            # from path
            file = File()
            file.fromPath(value)
            return file

        elif not isinstance(value, File):
            # dictionary or similar
            file = File()
            file.filename = value.get("filename", "")
            file.file = value.get("file")
            file.filekey = node.name
            file.uid = value.get("uid", node.name)
            file.mime = value.get("mimetype")
            file.size = value.get("size")
            file.tempfile = True
            return file
        return value