Example #1
0
    def _send_as_multipart(self, request, data):
        """
        Based on the request it decides if we should send the request as multipart
        or not.

        :return: (Boolean that indicates if multipart should be used,
                  List with string variables,
                  List with file variables)
        """
        multipart = False
        v_vars = []
        v_files = []

        to_str = lambda _str: _str.encode(DEFAULT_ENCODING) if \
            isinstance(_str, unicode) else _str

        header_items = request.header_items()
        for name, value in header_items:
            if name.lower() == 'content-type' and 'multipart/form-data' in value:
                multipart = True

        try:
            for pname, val in data.items():
                # Added to support repeated parameter names
                enc_pname = to_str(pname)

                if isinstance(val, basestring):
                    val = [val]
                else:
                    try:
                        # is this a sufficient test for sequence-ness?
                        len(val)
                    except:
                        val = [val]

                for elem in val:
                    if is_file_like(elem):
                        if not elem.closed:
                            v_files.append((enc_pname, elem))
                            multipart = True
                        else:
                            v_vars.append((enc_pname, ''))
                    elif hasattr(elem, 'isFile'):
                        v_files.append((enc_pname, elem))
                        multipart = True
                    else:
                        # Ensuring we actually send a string
                        elem = to_str(elem)
                        v_vars.append((enc_pname, elem))
        except TypeError:
            try:
                tb = sys.exc_info()[2]
                # pylint: disable=E0702
                # http://www.logilab.org/ticket/113023
                raise (TypeError, "not a valid non-string sequence or "
                       "mapping object", tb)
            finally:
                del tb

        return multipart, v_vars, v_files
Example #2
0
def _split_vars_files(data):
    """
    Based on the request it decides if we should send the request as
    multipart or not.

    :return: (List with string variables,
              List with file variables)
    """
    v_vars = []
    v_files = []

    for token in data.iter_tokens():

        pname = token.get_name()
        value = token.get_value()

        enc_pname = smart_str(pname, encoding=DEFAULT_ENCODING)

        if is_file_like(value):
            if not value.closed:
                v_files.append((enc_pname, value))
            else:
                v_vars.append((enc_pname, ''))
        elif hasattr(value, 'isFile'):
            v_files.append((enc_pname, value))
        else:
            # Ensuring we actually send a string
            value = smart_str(value, encoding=DEFAULT_ENCODING)
            v_vars.append((enc_pname, value))

    return v_vars, v_files
Example #3
0
    def get_data(self):
        """
        :return: A string representation of the DataContainer. There is a
        special case, in which the DataContainer has a file inside, in which
        we return the data container as it is. This is needed by the multipart
        post handler.
        """

        # TODO: This is a hack I'm not comfortable with. There should
        # be a fancier way to do this.
        # If it contains a file then we are not interested in returning
        # its string representation
        for value in self._dc.itervalues():

            if isinstance(value, basestring):
                continue
            elif is_file_like(value) or (hasattr(value, "__iter__") and any(imap(is_file_like, value))):
                return self._dc

        # Ok, no file was found; return the string representation
        return str(self._dc)
Example #4
0
    def get_data(self):
        """
        :return: A string representation of the DataContainer. There is a
        special case, in which the DataContainer has a file inside, in which
        we return the data container as it is. This is needed by the multipart
        post handler.
        """

        # TODO: This is a hack I'm not comfortable with. There should
        # be a fancier way to do this.
        # If it contains a file then we are not interested in returning
        # its string representation
        for value in self._dc.itervalues():

            if isinstance(value, basestring):
                continue
            elif is_file_like(value) or (hasattr(value, "__iter__")
                                         and any(imap(is_file_like, value))):
                return self._dc

        # Ok, no file was found; return the string representation
        return str(self._dc)
Example #5
0
    def _send_as_multipart(self, request, data):
        """
        Based on the request it decides if we should send the request as multipart
        or not.

        :return: (Boolean that indicates if multipart should be used,
                  List with string variables,
                  List with file variables)
        """
        multipart = False
        v_vars = []
        v_files = []

        to_str = lambda _str: _str.encode(DEFAULT_ENCODING) if \
            isinstance(_str, unicode) else _str

        header_items = request.header_items()
        for name, value in header_items:
            if name.lower(
            ) == 'content-type' and 'multipart/form-data' in value:
                multipart = True

        try:
            for pname, val in data.items():
                # Added to support repeated parameter names
                enc_pname = to_str(pname)

                if isinstance(val, basestring):
                    val = [val]
                else:
                    try:
                        # is this a sufficient test for sequence-ness?
                        len(val)
                    except:
                        val = [val]

                for elem in val:
                    if is_file_like(elem):
                        if not elem.closed:
                            v_files.append((enc_pname, elem))
                            multipart = True
                        else:
                            v_vars.append((enc_pname, ''))
                    elif hasattr(elem, 'isFile'):
                        v_files.append((enc_pname, elem))
                        multipart = True
                    else:
                        # Ensuring we actually send a string
                        elem = to_str(elem)
                        v_vars.append((enc_pname, elem))
        except TypeError:
            try:
                tb = sys.exc_info()[2]
                # pylint: disable=E0702
                # http://www.logilab.org/ticket/113023
                raise (TypeError, "not a valid non-string sequence or "
                       "mapping object", tb)
            finally:
                del tb

        return multipart, v_vars, v_files