2008-07-01 15:10:51 +00:00
|
|
|
"""
|
|
|
|
Upload handlers to test the upload API.
|
|
|
|
"""
|
|
|
|
|
2020-09-28 08:09:29 +00:00
|
|
|
from django.core.files.uploadhandler import (
|
|
|
|
FileUploadHandler, StopUpload, TemporaryFileUploadHandler,
|
|
|
|
)
|
2008-07-01 15:10:51 +00:00
|
|
|
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2008-07-01 15:10:51 +00:00
|
|
|
class QuotaUploadHandler(FileUploadHandler):
|
|
|
|
"""
|
|
|
|
This test upload handler terminates the connection if more than a quota
|
|
|
|
(5MB) is uploaded.
|
|
|
|
"""
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
QUOTA = 5 * 2 ** 20 # 5 MB
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2008-07-01 15:10:51 +00:00
|
|
|
def __init__(self, request=None):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__(request)
|
2008-07-01 15:10:51 +00:00
|
|
|
self.total_upload = 0
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2008-07-01 15:10:51 +00:00
|
|
|
def receive_data_chunk(self, raw_data, start):
|
|
|
|
self.total_upload += len(raw_data)
|
|
|
|
if self.total_upload >= self.QUOTA:
|
|
|
|
raise StopUpload(connection_reset=True)
|
|
|
|
return raw_data
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2008-07-01 15:10:51 +00:00
|
|
|
def file_complete(self, file_size):
|
2008-08-30 19:56:14 +00:00
|
|
|
return None
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2020-09-28 08:09:29 +00:00
|
|
|
class StopUploadTemporaryFileHandler(TemporaryFileUploadHandler):
|
|
|
|
"""A handler that raises a StopUpload exception."""
|
|
|
|
def receive_data_chunk(self, raw_data, start):
|
|
|
|
raise StopUpload()
|
|
|
|
|
|
|
|
|
2008-08-30 19:56:14 +00:00
|
|
|
class CustomUploadError(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2008-08-30 19:56:14 +00:00
|
|
|
class ErroringUploadHandler(FileUploadHandler):
|
|
|
|
"""A handler that raises an exception."""
|
|
|
|
def receive_data_chunk(self, raw_data, start):
|
|
|
|
raise CustomUploadError("Oops!")
|