示例#1
0
    def get_file(self, field, required=False):
        """Get file for the given field name in form.

        This method always returns only one value associated with form field
        name. The method returns only the first value in case that more
        values were posted under such name. Please note that the order in which
        the values are received may vary from browser to browser and should not
        be counted on. If no such form field or value exists then the method
        returns the value specified by the optional parameter default. This
        parameter defaults to None if not specified.

        Args:
            field (str): Form field name.

        Keyword Args:
            required (bool): Set to 'True' to raise
                'HTTPMissingParam' instead of returning
                gracefully when field is not found
                (default 'False').

        Returns:
            tuple: FileObject Instance.

        Raises:
            HTTPMissinParam: The parameter was not found in the request, but
                it was required.

            HTTPUnsupportedMediaType: Expected payload.
        """
        form = self.form

        if required is True and field not in form:
            raise HTTPMissingFormField(field)

        try:
            value = form[field]
            if isinstance(value, list):
                if value[0].filename:
                    return FileObject(value[0].filename, value[0].type,
                                      value[0].file)

            else:
                if value.filename:
                    return FileObject(value.filename, value.type, value.file)
        except TypeError:
            pass
        except KeyError:
            pass

        if required is True:
            raise HTTPMissingFormField(field)

        return (None, None, None)
示例#2
0
    def get_files(self, field, required=False):
        """Get multiple files for the given field name in form.

        Returns an empty list if the file doesn’t exist. It’s guaranteed to
        return a list unless the field is required as per keyword args.

        Args:
            field (str): Form field name.

        Keyword Args:
            required (bool): Set to 'True' to raise
                'HTTPMissingParam' instead of returning
                gracefully when field is not found
                (default 'False').

        Returns:
            tuple: Sequence of FileObject Instances.

        Raises:
            HTTPMissinParam: The parameter was not found in the request, but
                it was required.

            HTTPUnsupportedMediaType: Expected payload.
        """

        files = []
        form = self.form

        if required is True and field not in form:
            raise HTTPMissingFormField(field)

        try:
            value = form[field]
            if isinstance(value, list):
                for item in value:
                    if item.filename:
                        obj = FileObject(item.filename, item.type, item.file)
                        files.append(obj)

            else:
                if value.filename:
                    obj = FileObject(value.filename, value.type, value.file)
                    files.append(obj)

            return tuple(files)

        except TypeError:
            pass
        except KeyError:
            pass

        return tuple(files)
示例#3
0
    def get_list(self, field, required=False):
        """Get list of values for the requested field name in form.

        Returns an empty list if the file doesn’t exist. It’s guaranteed to
        return a list unless the field is required as per keyword args.

        Args:
            field (str): Form field name.

        Keyword Args:
            required (bool): Set to 'True' to raise
                'HTTPMissingParam' instead of returning
                gracefully when field is not found
                (default 'False').

        Returns:
            tuple: List of values.

        Raises:
            HTTPMissinParam: The parameter was not found in the request, but
                it was required.

            HTTPUnsupportedMediaType: Expected payload.
        """
        form = self.form

        if required is True and field not in form:
            raise HTTPMissingFormField(field)

        try:
            return to_tuple(form.getlist(field))
        except TypeError:
            pass

        if required is True:
            raise HTTPMissingFormField(field)

        return ()
示例#4
0
    def get_first(self, field, required=False, default=None):
        """Get the value for the given field name in form.

        This method always returns only one value associated with form field
        name. The method returns only the first value in case that more
        values were posted under such name. Please note that the order in which
        the values are received may vary from browser to browser and should not
        be counted on. If no such form field or value exists then the method
        returns the value specified by the optional parameter default. This
        parameter defaults to None if not specified.

        Args:
            field (str): Form field name.

        Keyword Args:
            required (bool): Set to 'True' to raise
                'HTTPMissingParam' instead of returning
                gracefully when field is not found
                (default 'False').

            default (str): Value to return if field is not found.

        Returns:
            str: String Value of Field.

        Raises:
            HTTPMissinParam: The parameter was not found in the request, but
                it was required.

            HTTPUnsupportedMediaType: Expected payload.
        """
        form = self.form

        if required is True and field not in form:
            raise HTTPMissingFormField(field)

        if default is not None and required is False:
            default = str(default)

        try:
            if field in form:
                return parse_form_field(form.getfirst(field), default)
            else:
                return default
        except TypeError:
            pass

        return default