Beispiel #1
0
    def _set_data(cls, target, value, oldvalue, initiator):
        # Ref #384 : enforce this method
        if isinstance(value, (cStringIO.InputType, file)):
            value.seek(0)
            value = value.read()

        if isinstance(value, bytes):
            value = _to_fieldstorage(
                fp=cStringIO.StringIO(value),
                filename=target.name,
                size=len(value)
            )
        newvalue = _SQLAMutationTracker._field_set(
            target,
            value,
            oldvalue,
            initiator
        )

        if newvalue is None:
            return
        target.filename = newvalue.filename
        target.mimetype = detect_file_headers(newvalue.filename)
        target.size = newvalue.file.content_length

        return newvalue
def _to_fieldstorage(fp, filename, size, **_kwds):
    """ Build a :class:`cgi.FieldStorage` instance.

    Deform's :class:`FileUploadWidget` returns a dict, but
    :class:`depot.fields.sqlalchemy.UploadedFileField` likes
    :class:`cgi.FieldStorage` objects
    """
    f = cgi.FieldStorage()
    f.file = fp
    f.filename = filename
    f.type = detect_file_headers(filename)
    f.length = size
    return f
Beispiel #3
0
def _to_fieldstorage(fp, filename, size, **_kwds):
    """ Build a :class:`cgi.FieldStorage` instance.

    Deform's :class:`FileUploadWidget` returns a dict, but
    :class:`depot.fields.sqlalchemy.UploadedFileField` likes
    :class:`cgi.FieldStorage` objects
    """
    f = cgi.FieldStorage()
    f.file = fp
    f.filename = filename
    f.type = detect_file_headers(filename)
    f.length = size
    return f
Beispiel #4
0
    def _set_data(cls, target, value, oldvalue, initiator):
        if isinstance(value, bytes):
            value = _to_fieldstorage(fp=StringIO(value),
                                     filename=target.filename,
                                     size=len(value))

        newvalue = _SQLAMutationTracker._field_set(
            target, value, oldvalue, initiator)

        if newvalue is None:
            return
        target.filename = newvalue.filename
        target.mimetype = detect_file_headers(newvalue.filename)
        target.size = newvalue.file.content_length

        return newvalue
Beispiel #5
0
    def _set_data(cls, target, value, oldvalue, initiator):
        # Ref #384 : enforce this method
        if isinstance(value, (cStringIO.InputType, file)):
            value.seek(0)
            value = value.read()

        if isinstance(value, bytes):
            value = _to_fieldstorage(fp=cStringIO.StringIO(value),
                                     filename=target.name,
                                     size=len(value))
        newvalue = _SQLAMutationTracker._field_set(target, value, oldvalue,
                                                   initiator)

        if newvalue is None:
            return
        target.filename = newvalue.filename
        target.mimetype = detect_file_headers(newvalue.filename)
        target.size = newvalue.file.content_length

        return newvalue