diff options
author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
tree | f0506b668a013d0063e5fba3dbf4863b466713ba /django/core/files/uploadhandler.py | |
parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
download | django-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/core/files/uploadhandler.py')
-rw-r--r-- | django/core/files/uploadhandler.py | 61 |
1 files changed, 44 insertions, 17 deletions
diff --git a/django/core/files/uploadhandler.py b/django/core/files/uploadhandler.py index ee6bb31fce..64781f811b 100644 --- a/django/core/files/uploadhandler.py +++ b/django/core/files/uploadhandler.py @@ -5,15 +5,18 @@ import os from io import BytesIO from django.conf import settings -from django.core.files.uploadedfile import ( - InMemoryUploadedFile, TemporaryUploadedFile, -) +from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile from django.utils.module_loading import import_string __all__ = [ - 'UploadFileException', 'StopUpload', 'SkipFile', 'FileUploadHandler', - 'TemporaryFileUploadHandler', 'MemoryFileUploadHandler', 'load_handler', - 'StopFutureHandlers' + "UploadFileException", + "StopUpload", + "SkipFile", + "FileUploadHandler", + "TemporaryFileUploadHandler", + "MemoryFileUploadHandler", + "load_handler", + "StopFutureHandlers", ] @@ -21,6 +24,7 @@ class UploadFileException(Exception): """ Any error having to do with uploading files. """ + pass @@ -28,6 +32,7 @@ class StopUpload(UploadFileException): """ This exception is raised when an upload must abort. """ + def __init__(self, connection_reset=False): """ If ``connection_reset`` is ``True``, Django knows will halt the upload @@ -38,15 +43,16 @@ class StopUpload(UploadFileException): def __str__(self): if self.connection_reset: - return 'StopUpload: Halt current upload.' + return "StopUpload: Halt current upload." else: - return 'StopUpload: Consume request data, then halt.' + return "StopUpload: Consume request data, then halt." class SkipFile(UploadFileException): """ This exception is raised by an upload handler that wants to skip a given file. """ + pass @@ -55,6 +61,7 @@ class StopFutureHandlers(UploadFileException): Upload handlers that have handled a file and do not want future handlers to run should raise this exception instead of returning None. """ + pass @@ -62,7 +69,8 @@ class FileUploadHandler: """ Base class for streaming upload handlers. """ - chunk_size = 64 * 2 ** 10 # : The default chunk size is 64 KB. + + chunk_size = 64 * 2**10 # : The default chunk size is 64 KB. def __init__(self, request=None): self.file_name = None @@ -72,7 +80,9 @@ class FileUploadHandler: self.content_type_extra = None self.request = request - def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None): + def handle_raw_input( + self, input_data, META, content_length, boundary, encoding=None + ): """ Handle the raw input from the client. @@ -90,7 +100,15 @@ class FileUploadHandler: """ pass - def new_file(self, field_name, file_name, content_type, content_length, charset=None, content_type_extra=None): + def new_file( + self, + field_name, + file_name, + content_type, + content_length, + charset=None, + content_type_extra=None, + ): """ Signal that a new file has been started. @@ -109,7 +127,9 @@ class FileUploadHandler: Receive data from the streamed upload parser. ``start`` is the position in the file of the chunk. """ - raise NotImplementedError('subclasses of FileUploadHandler must provide a receive_data_chunk() method') + raise NotImplementedError( + "subclasses of FileUploadHandler must provide a receive_data_chunk() method" + ) def file_complete(self, file_size): """ @@ -118,7 +138,9 @@ class FileUploadHandler: Subclasses should return a valid ``UploadedFile`` object. """ - raise NotImplementedError('subclasses of FileUploadHandler must provide a file_complete() method') + raise NotImplementedError( + "subclasses of FileUploadHandler must provide a file_complete() method" + ) def upload_complete(self): """ @@ -139,12 +161,15 @@ class TemporaryFileUploadHandler(FileUploadHandler): """ Upload handler that streams data into a temporary file. """ + def new_file(self, *args, **kwargs): """ Create the file object to append to as data is coming in. """ super().new_file(*args, **kwargs) - self.file = TemporaryUploadedFile(self.file_name, self.content_type, 0, self.charset, self.content_type_extra) + self.file = TemporaryUploadedFile( + self.file_name, self.content_type, 0, self.charset, self.content_type_extra + ) def receive_data_chunk(self, raw_data, start): self.file.write(raw_data) @@ -155,7 +180,7 @@ class TemporaryFileUploadHandler(FileUploadHandler): return self.file def upload_interrupted(self): - if hasattr(self, 'file'): + if hasattr(self, "file"): temp_location = self.file.temporary_file_path() try: self.file.close() @@ -169,7 +194,9 @@ class MemoryFileUploadHandler(FileUploadHandler): File upload handler to stream uploads into memory (used for small files). """ - def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None): + def handle_raw_input( + self, input_data, META, content_length, boundary, encoding=None + ): """ Use the content_length to signal whether or not this handler should be used. @@ -204,7 +231,7 @@ class MemoryFileUploadHandler(FileUploadHandler): content_type=self.content_type, size=file_size, charset=self.charset, - content_type_extra=self.content_type_extra + content_type_extra=self.content_type_extra, ) |