PageRenderTime 11ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/file_uploads/uploadhandler.py

https://code.google.com/p/mango-py/
Python | 34 lines | 18 code | 8 blank | 8 comment | 1 complexity | 904a6ea9d8ff569b36755ac9c9517156 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Upload handlers to test the upload API.
  3. """
  4. from django.core.files.uploadhandler import FileUploadHandler, StopUpload
  5. class QuotaUploadHandler(FileUploadHandler):
  6. """
  7. This test upload handler terminates the connection if more than a quota
  8. (5MB) is uploaded.
  9. """
  10. QUOTA = 5 * 2**20 # 5 MB
  11. def __init__(self, request=None):
  12. super(QuotaUploadHandler, self).__init__(request)
  13. self.total_upload = 0
  14. def receive_data_chunk(self, raw_data, start):
  15. self.total_upload += len(raw_data)
  16. if self.total_upload >= self.QUOTA:
  17. raise StopUpload(connection_reset=True)
  18. return raw_data
  19. def file_complete(self, file_size):
  20. return None
  21. class CustomUploadError(Exception):
  22. pass
  23. class ErroringUploadHandler(FileUploadHandler):
  24. """A handler that raises an exception."""
  25. def receive_data_chunk(self, raw_data, start):
  26. raise CustomUploadError("Oops!")