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