Python django.http.multipartparser.MultiPartParser() Examples

The following are 11 code examples of django.http.multipartparser.MultiPartParser(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.http.multipartparser , or try the search function .
Example #1
Source File: request.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def parse_file_upload(self, META, post_data):
        """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning="You cannot alter upload handlers after the upload has been processed."
        )
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse() 
Example #2
Source File: request.py    From bioforum with MIT License 5 votes vote down vote up
def parse_file_upload(self, META, post_data):
        """Return a tuple of (POST QueryDict, FILES MultiValueDict)."""
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning="You cannot alter upload handlers after the upload has been processed."
        )
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse() 
Example #3
Source File: decorator.py    From zulip with Apache License 2.0 5 votes vote down vote up
def process_as_post(view_func: ViewFuncT) -> ViewFuncT:
    @wraps(view_func)
    def _wrapped_view_func(request: HttpRequest, *args: object, **kwargs: object) -> HttpResponse:
        # Adapted from django/http/__init__.py.
        # So by default Django doesn't populate request.POST for anything besides
        # POST requests. We want this dict populated for PATCH/PUT, so we have to
        # do it ourselves.
        #
        # This will not be required in the future, a bug will be filed against
        # Django upstream.

        if not request.POST:
            # Only take action if POST is empty.
            if request.META.get('CONTENT_TYPE', '').startswith('multipart'):
                # Note that request._files is just the private attribute that backs the
                # FILES property, so we are essentially setting request.FILES here.  (In
                # Django 1.5 FILES was still a read-only property.)
                request.POST, request._files = MultiPartParser(
                    request.META,
                    BytesIO(request.body),
                    request.upload_handlers,
                    request.encoding,
                ).parse()
            else:
                request.POST = QueryDict(request.body, encoding=request.encoding)

        return view_func(request, *args, **kwargs)

    return cast(ViewFuncT, _wrapped_view_func)  # https://github.com/python/mypy/issues/1927 
Example #4
Source File: request.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def parse_file_upload(self, META, post_data):
        """Return a tuple of (POST QueryDict, FILES MultiValueDict)."""
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning="You cannot alter upload handlers after the upload has been processed."
        )
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse() 
Example #5
Source File: request.py    From python with Apache License 2.0 5 votes vote down vote up
def parse_file_upload(self, META, post_data):
        """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning="You cannot alter upload handlers after the upload has been processed."
        )
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse() 
Example #6
Source File: request.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def parse_file_upload(self, META, post_data):
        """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning="You cannot alter upload handlers after the upload has been processed."
        )
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse() 
Example #7
Source File: request.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def parse_file_upload(self, META, post_data):
        """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning="You cannot alter upload handlers after the upload has been processed."
        )
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse() 
Example #8
Source File: request.py    From python2017 with MIT License 5 votes vote down vote up
def parse_file_upload(self, META, post_data):
        """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""
        self.upload_handlers = ImmutableList(
            self.upload_handlers,
            warning="You cannot alter upload handlers after the upload has been processed."
        )
        parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
        return parser.parse() 
Example #9
Source File: test_multipart.py    From python-libmaas with GNU Affero General Public License v3.0 5 votes vote down vote up
def parse_headers_and_body_with_django(headers, body):
    """Parse `headers` and `body` with Django's :class:`MultiPartParser`.

    `MultiPartParser` is a curiously ugly and RFC non-compliant concoction.

    Amongst other things, it coerces all field names, field data, and
    filenames into Unicode strings using the "replace" error strategy, so be
    warned that your data may be silently mangled.

    It also, in 1.3.1 at least, does not recognise any transfer encodings at
    *all* because its header parsing code was broken.

    I'm also fairly sure that it'll fall over on headers than span more than
    one line.

    In short, it's a piece of code that inspires little confidence, yet we
    must work with it, hence we need to round-trip test multipart handling
    with it.
    """
    handler = MemoryFileUploadHandler()
    meta = {
        "CONTENT_TYPE": headers["Content-Type"],
        "CONTENT_LENGTH": headers["Content-Length"],
    }
    parser = MultiPartParser(
        META=meta, input_data=BytesIO(body), upload_handlers=[handler]
    )
    return parser.parse() 
Example #10
Source File: django.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super().setUp()
        # Django 1.8 and 1.11 need to be configured before we can use
        # WSGIRequest and MultiPartParser.
        if not django.conf.settings.configured:
            django.conf.settings.configure(DEBUG=True) 
Example #11
Source File: django.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def parse_headers_and_body_with_django(cls, headers, body):
        """Parse `headers` and `body` with Django's :class:`MultiPartParser`.

        `MultiPartParser` is a curiously ugly and RFC non-compliant concoction.

        Amongst other things, it coerces all field names, field data, and
        filenames into Unicode strings using the "replace" error strategy, so
        be warned that your data may be silently mangled.

        It also, in 1.3.1 at least, does not recognise any transfer encodings
        at *all* because its header parsing code was broken.

        I'm also fairly sure that it'll fall over on headers than span more
        than one line.

        In short, it's a piece of code that inspires little confidence, yet we
        must work with it, hence we need to round-trip test multipart handling
        with it.
        """
        handler = MemoryFileUploadHandler()
        meta = {
            "CONTENT_TYPE": headers["Content-Type"],
            "CONTENT_LENGTH": headers["Content-Length"],
            # To make things even more entertaining, 1.8 prefixed meta vars
            # with "HTTP_" and 1.11 does not.
            "HTTP_CONTENT_TYPE": headers["Content-Type"],
            "HTTP_CONTENT_LENGTH": headers["Content-Length"],
        }
        parser = multipartparser.MultiPartParser(
            META=meta,
            input_data=BytesIO(body.encode("ascii")),
            upload_handlers=[handler],
        )
        return parser.parse()